Comments (2)
The behavior for relative imports can be a little confusing. It stems from how uvicorn handles relative imports (or doesn't, depending on how you launch the app).
First, when you use shiny run
, there are four different ways you could launch an app::
shiny run app.py
: launching it as a file from the app's directoryshiny run myapp/app.py
: launching it as a file from the app's parent directoryshiny run app:app
: launching it as a module from the app's directoryshiny run myapp.app:app
: launching it as a module from the app's parent directory
For the first two, shiny run
internally does something like the following: it goes to the app directory, and then runs uvicorn run app:app
. For the latter two, it basically just calls uvicorn run app:app
or uvicorn run myapp.app:app
.
With that background, for Shiny Core apps:
- If you launch it from the app directory with
uvicorn app:app
, then the relative import won't work. - If you launch it from the parent directory with
uvicorn myap.app:app
, then the relative import will work.
Shiny Express are a bit different, because they are actually wrapped into a Shiny Core app. For Shiny Express, we recently added the ability to use relative imports. However, for Shiny Express apps, you can only launch apps as files, not as modules. So you can do (1) shiny run app.py
and (2)shiny run myapp/app.py
, but not (3) shiny run app:app
or (4) shiny run myapp.app:app
. The reason for this is because Shiny Express needs to evaluate the code in a special way to capture the result of each line, similar to how Jupyter notebooks evaluate the code. We can't just pass app:app
to uvicorn and have it work; we actually need to wrap up the code in a special evaluator inside of a Shiny Core app, and then pass that Shiny Core app to uvicorn.
If you want to launch a Shiny Express app from Python (instead of from the command line), it is possible, but I won't go into detail right here.
The PR that added this functionality to Shiny Express is #1464. That PR has some other information that may be useful.
This spreadsheet shows in which conditions you can do which kind of import (apologies if it is somewhat confusing to read): https://docs.google.com/spreadsheets/d/1jIVoEDr234uo_ALOPiilSIoU-bj18L-R6YP_39ORXwM/edit?gid=0#gid=0
![image](https://private-user-images.githubusercontent.com/86978/353229157-d5854ed5-83bd-413a-b3ee-93b23578ef58.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjM4ODY1ODUsIm5iZiI6MTcyMzg4NjI4NSwicGF0aCI6Ii84Njk3OC8zNTMyMjkxNTctZDU4NTRlZDUtODNiZC00MTNhLWIzZWUtOTNiMjM1NzhlZjU4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODE3VDA5MTgwNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTA3ZWI3OTcyYmU1NzNkMzRkZTBiZWE0ZTNjYjEzMDI1OWExMWJhYTMwNjRmM2JiNjRlNGY5MzgwOTg0ODc0M2ImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.cIs_H6Vsv53Z12QOJLfkwTEvuf58TKdwnpIwwuCku1Y)
It looks like kind of a mess, and again, that's in part because of how uvicorn handles relative imports.
We generally expect people to:
- Use global imports like
import utils
, instead of relative/submodule imports, likefrom . import utils
. - Launch their apps as files, with
shiny run app.py
orshiny run myapp/app.py
.
If you do those things, the imports should work.
from py-shiny.
Okay, I see then, why it works with run and not with the app fixture.
Is there any other way to create the fixture from running the module instead of linking to the py file?
If not then just using the absolute path might work to have a testable package.
from py-shiny.
Related Issues (20)
- request: Pyodide support for `polars`; Use `polars` in shinylive examples
- shiny 1.0.0 breaks app in Firefox HOT 7
- `Chat()` doesn't work inside a module
- Possible to add ui.update_input_file?? HOT 2
- Progress message assertions HOT 2
- shiny doesn't work along with the latest ipython version HOT 2
- Version 1.0.0 not stable with sidebar HOT 5
- the doc on the styles argument of render.DataGrid() is incorrect: snake-case works HOT 1
- Starting `Chat()` messages don't appear when ui is dynamically rendered
- For Chat component, user input should be rendered as markdown HOT 1
- In JS, calling `Shiny.setInputValue('foo', x, {priority: "event"})` does not always trigger reactive invalidation
- data frame not showing using ui.output_data_frame HOT 4
- docs: ui.Theme docs missing quotation marks
- ui.Theme has odd behavior when Shiny files are co-mingled with other system files (e.g. not in a standalone folder) HOT 1
- Animated Plotly Graphs Not Rendering
- Add smoke tests for `shiny.playwright.NavPanel` HOT 1
- Is there support for LangChain agents?
- Template triggers runtime error HOT 1
- DataGrid.data_view raises an IndexError when underlying dataframe is made shorter
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from py-shiny.