Skip to content

Conversation

@jcheng5
Copy link
Collaborator

@jcheng5 jcheng5 commented Feb 4, 2022

Currently, just the shiny run command is implemented. You use it like this (the following examples are all equivalent):

shiny run --reload examples.myapp.app:app
shiny run --reload examples.myapp.app
shiny run --reload examples/myapp
shiny run --reload examples/myapp/app.py
shiny run --reload examples/myapp/app.py:app
(cd examples/myapp && shiny run)

You can also replace shiny with python3 -m shiny.

You can also do:
python3 -m shiny [OPTIONS] [APP]
@jcheng5
Copy link
Collaborator Author

jcheng5 commented Feb 4, 2022

BTW I think we also get the equivalent of shiny::runExamples for free, if we ship some examples under shiny.examples. Like this would work from anywhere:

shiny run shiny.examples.01_hello

@jcheng5 jcheng5 requested review from cpsievert and wch February 4, 2022 00:22
@cpsievert
Copy link
Collaborator

I'm seeing

$ shiny run --reload examples.myapp.app
WARNING:  You must pass the application as an import string to enable 'reload' or 'workers'.

@cpsievert
Copy link
Collaborator

cpsievert commented Feb 4, 2022

ship some examples under shiny.examples

Interesting. In #42, I was thinking of putting examples in a Dict[str, str] so that could both run them and place them in the docstring, but this seems like a better way to do it (although, that way, we'd have to read at least a couple dozen files from disk at runtime, which might be more costly than it's worth?).

@jcheng5
Copy link
Collaborator Author

jcheng5 commented Feb 4, 2022

@cpsievert "You must pass the application as an import string" I'll get on that right now

You must pass the application as an import string to enable 'reload' or 'workers'.
@jcheng5
Copy link
Collaborator Author

jcheng5 commented Feb 4, 2022

@cpsievert

we'd have to read at least a couple dozen files from disk at runtime

Is that true? None of the rest of the code in prism would reference the examples modules. Also, we don't necessarily need to put a ton of examples in there, just ones we want to ship with the core package for whatever reason.

@wch
Copy link
Collaborator

wch commented Feb 4, 2022

we'd have to read at least a couple dozen files from disk at runtime

I think that's true only if it's directly or indirectly imported in shiny's __init__.py file (and the user does import shiny).

@jcheng5
Copy link
Collaborator Author

jcheng5 commented Feb 4, 2022

The web browser launching is a little more complicated than I thought. I'm going to save it for a separate PR, but here are my notes so far.

Python has a webbrowser module in the standard library that does an awesome job opening browser windows. If you open the same URL multiple times, it will even reuse the existing tab, and bring it to the front. I really like this behavior, but, it's not trivial to get this working with --reload (which is where it'd be the most useful).

From the shiny main.py, we just call uvicorn.run(reload=True), which blocks forever--we don't have any callbacks to alert us that a reload has occurred. (If we did, we could call webbrowser.open() in that callback.) So I tried launching the browser from inside the shiny.App object, in the lifespan() function (it's actually perfect for that, as we know that the web server is actually listening at that point). But each time reload happens, a new child Shiny process is launched, so we lose the nice "reuse the existing tab" behavior. The other downside is that the shiny.App object does not know its own URL, at least, not before the first request is served; in my experimenting, I passed the URL via an environment variable but that seems sketchy.

To solve this properly, I can think of two ways forward. 1) Don't use uvicorn.run(), instead implement our own, which would give us the callbacks we need. 2) Launch a background thread in the shiny run process that does some kind of RPC with the child processes--this can be very simple, as the child just has to tell the parent it's time to launch the browser, nothing else.

@jcheng5 jcheng5 merged commit f25d839 into main Feb 4, 2022
@jcheng5 jcheng5 deleted the shiny-command branch February 4, 2022 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants