Skip to content

Commit

Permalink
Add support for shiny express apps
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Nov 10, 2023
1 parent a6b47e1 commit 8222f49
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ packages: clean-packages \
$(PACKAGE_DIR)/$(HTMLTOOLS_WHEEL): $(PYBIN) $(PACKAGE_DIR)/py-htmltools
# Remove any old copies of the package
rm -f $(PACKAGE_DIR)/htmltools*.whl
$(PYBIN)/pip install -e $(PACKAGE_DIR)/py-htmltools[dev,test]
. $(PYBIN)/activate && cd $(PACKAGE_DIR)/py-htmltools && make install-editable
. $(PYBIN)/activate && cd $(PACKAGE_DIR)/py-htmltools && make dist && mv dist/*.whl ../

$(PACKAGE_DIR)/$(SHINY_WHEEL): $(PYBIN) $(PACKAGE_DIR)/py-shiny
Expand Down
2 changes: 1 addition & 1 deletion packages/py-htmltools
2 changes: 1 addition & 1 deletion packages/py-shiny
14 changes: 7 additions & 7 deletions shinylive_lock.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"htmltools": {
"name": "htmltools",
"version": "0.4.1",
"filename": "htmltools-0.4.1-py3-none-any.whl",
"version": "0.4.1.9000",
"filename": "htmltools-0.4.1.9000-py3-none-any.whl",
"sha256": null,
"url": null,
"depends": [
Expand All @@ -15,16 +15,16 @@
},
"shiny": {
"name": "shiny",
"version": "0.6.0",
"filename": "shiny-0.6.0-py3-none-any.whl",
"version": "0.6.0.9001",
"filename": "shiny-0.6.0.9001-py3-none-any.whl",
"sha256": null,
"url": null,
"depends": [
{"name": "typing-extensions", "specs": [[">=", "4.0.1"]]},
{"name": "uvicorn", "specs": [[">=", "0.16.0"]]},
{"name": "starlette", "specs": [[">=", "0.17.1"]]},
{"name": "python-multipart", "specs": []},
{"name": "htmltools", "specs": [[">=", "0.4.0"]]},
{"name": "htmltools", "specs": []},
{"name": "click", "specs": [[">=", "8.1.4"]]},
{"name": "markdown-it-py", "specs": [[">=", "1.1.0"]]},
{"name": "mdit-py-plugins", "specs": [[">=", "0.3.0"]]},
Expand Down Expand Up @@ -96,7 +96,7 @@
},
"mizani": {
"name": "mizani",
"version": "0.0.post20+g57f00ad",
"version": "0.8.1.post24+g57f00ad",
"filename": "mizani-0.0.0-py3-none-any.whl",
"sha256": null,
"url": null,
Expand All @@ -113,7 +113,7 @@
},
"plotnine": {
"name": "plotnine",
"version": "0.0.post20+g64295e1",
"version": "0.10.1.post98+g64295e1a",
"filename": "plotnine-0.0.0-py3-none-any.whl",
"sha256": null,
"url": null,
Expand Down
31 changes: 25 additions & 6 deletions src/hooks/usePyodide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,24 +346,43 @@ async def _format_py_code(x: str):
`
_shiny_app_registry = {}
# The purpose of this class is for Shiny express apps to have the same "shape"
# as regular Shiny apps which are loaded with:
# importlib.import_module(f"{app_name}.app")
# Essentially we are wrapping the express app in an object that looks like a
# Python (not Shiny) module. The express app has the name "app" in that module.
class ShinyExpressAppModule:
def __init__(self, app_path):
import shiny.express
self.app = shiny.express.wrap_express_app(app_path)
async def _start_app(app_name, scope = _shiny_app_registry):
import sys
import importlib
import shiny.express
from pathlib import Path
app_path = f"/home/pyodide/{app_name}"
sys.path.insert(0, app_path)
app_dir = f"/home/pyodide/{app_name}"
sys.path.insert(0, app_dir)
await _install_requirements_from_dir(app_path)
await _install_requirements_from_dir(app_dir)
await _load_packages_from_dir(app_path)
await _load_packages_from_dir(app_dir)
# This prevents random occurrences of ModuleNotFoundError.
importlib.invalidate_caches()
app_obj = importlib.import_module(f"{app_name}.app")
class ModuleApp:
app = None
if shiny.express.is_express_app("app.py", app_dir):
app_obj = ShinyExpressAppModule(Path(app_dir) / "app.py")
else:
app_obj = importlib.import_module(f"{app_name}.app")
scope[app_name] = app_obj
sys.path.remove(app_path)
sys.path.remove(app_dir)
async def _stop_app(app_name, scope = _shiny_app_registry):
Expand Down

0 comments on commit 8222f49

Please sign in to comment.