posit-dev / py-shinylive Goto Github PK
View Code? Open in Web Editor NEWPython package for deploying Shinylive applications
Home Page: https://shiny.posit.co/py/docs/shinylive.html
License: MIT License
Python package for deploying Shinylive applications
Home Page: https://shiny.posit.co/py/docs/shinylive.html
License: MIT License
Export the commands that power the CLI so they can be called in Python scripts.
I'm working on a toy application to show how a shinylive app could make API calls. It's turning out to be harder than I thought.
It seems like networking in general does not work very well with pyodide (and thus shinylive). I'm still learning here, but the issue looks to be accessing sockets.
A few posts (e.g., here) suggest that you should be able to do this with javascript. I see the tags reference in support, but I'm a bit lost in how to implement this in practice.
My toy app is calling a published API that was built on the palmerpenguins data set. Here is the code that works as a standalone shiny app but not with shinylive:
from shiny import *
import urllib.request
import json
app_ui = ui.page_fluid(
ui.layout_sidebar(
ui.panel_sidebar(
ui.input_select("species", "Penguin Species",
{"Gentoo":"Gentoo", "Chinstrap":"Chinstrap", "Adelie":"Adelie"}),
ui.input_action_button("go", "Predict", width="100%")),
ui.panel_main(ui.h2(ui.output_text("txt"))))
)
def server(input, output, session):
@output
@render.text
@reactive.event(input.go)
def txt():
url = "http://penguin.eastus.azurecontainer.io:8000/predict"
payload = [{"species":input. Species(),
"bill_length_mm":40.5,
"bill_depth_mm":18.9,
"flipper_length_mm":180,
"body_mass_g":3950}]
headers = {"Content-Type": "application/json"}
data = json.dumps(payload).encode("utf-8")
headers = {k: v.encode("utf-8") for k, v in headers. Items()}
request = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(request)
data = response. Read()
response_data = json.loads(data. Decode("utf-8"))
prediction = response_data[0]
class_pred = prediction[".pred_class"]
return f"The {input.species()} 🐧 is predicted to be {class_pred}."
app = App(app_ui, server)
Is there a known workaround with the python route that I'm unable to find? If not, any suggestions on how to insert a javascript component that can take the dynamic inputs from the UI to be used for the prediction?
Request:
Hmm, shinylive appears to scan every .py file in the directory to figure out what pyodide needs to install, even scripts that aren't used at runtime. Like sometimes I'll have a helper script that builds a database (in this case, using sqlalchemy) but the actual shiny app only needs built-in sqlite3.
I hacked around this by making a stripped-down branch and exporting that, but it could be nice if either the build process honored requirements.txt (in a minimalistic sense) or if somehow it only scanned app.py and what it imports? or some other solution to ignore certain files when figuring this out.
My reply:
... correct. py-shinylive currently scans every that
app.json
provides. (Which is every file.) LinkIt seems fair to me that if a
requirements.txt
is found, then only those packages are inspected.
Putting Jinja2
in a requirements.txt
file, as suggested e.g. here, doesn't seem to be enough for the shinylive
CLI tool to actually bundle the Jinja2 wheel in the output dir. But if I explicitly import jinja2
in app.py
, the wheel gets included and rendering tables works.
Is this intentional? If so, it should probably be documented?
As an aside, thank you for bringing Shiny to Python in great style!
I couldn't understand from documentation how can I make a local file accessible to the application, so that it would be possible to read_csv and do things like that?
Should I copy it to some location in shinylive
folder? An what path should I provide for the script to work?
Some files in project directory can be accessed by the py-shiny app which works fine, but shinylive app gives file_not_found_error if I build a shinylive app from exactly the same project.
Hi there,
I was trying out shinylive on a virtualenv on 3.9.14 and could not get it to work:
pip install shiny
pip install shinylive
shiny create myapp
shinylive export myapp site
Creating site/
Copying base Shinylive files from /Users/XX/Library/Caches/shinylive/shinylive-0.0.8/ to site/
Copying imported packages from /Users/XX/Library/Caches/shinylive/shinylive-0.0.8/shinylive/pyodide/ to site/shinylive/pyodide/
Traceback (most recent call last):
File "/usr/local/bin/shinylive", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1130, in __call__
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1055, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.9/site-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/shinylive/_main.py", line 79, in export
_export.export(
File "/usr/local/lib/python3.9/site-packages/shinylive/_export.py", line 101, in export
copy_fn(src_path, dest_path)
File "/usr/local/lib/python3.9/site-packages/shinylive/_utils.py", line 93, in copy_fn
verbose_print(f"Skipping {dst}", file=sys.stderr)
TypeError: verbose_print() got an unexpected keyword argument 'file'
Any idea what might be going on? Really looking forward to playing around with this!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.