Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for shiny express apps #521

Merged
merged 6 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions rsconnect/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
produce_bootstrap_output,
parse_client_response,
)
from .shiny_express import is_express_app

server_store = ServerStore()
future_enabled = False
Expand Down Expand Up @@ -1366,16 +1367,41 @@ def deploy_app(
no_verify: bool = False,
):
set_verbosity(verbose)
kwargs = locals()
kwargs["entrypoint"] = entrypoint = validate_entry_point(entrypoint, directory)
kwargs["extra_files"] = extra_files = validate_extra_files(directory, extra_files)
entrypoint = validate_entry_point(entrypoint, directory)
extra_files = validate_extra_files(directory, extra_files)
environment = create_python_environment(
directory,
force_generate,
python,
)

ce = RSConnectExecutor(**kwargs)
if is_express_app(entrypoint + ".py", directory):
env_vars["SHINY_EXPRESS_APP_FILE"] = entrypoint + ".py"
entrypoint = "shiny.express.app"

extra_args = dict(
directory=directory,
server=server,
exclude=exclude,
new=new,
app_id=app_id,
title=title,
visibility=visibility,
disable_env_management=disable_env_management,
env_vars=env_vars,
)

ce = RSConnectExecutor(
name=name,
api_key=api_key,
insecure=insecure,
cacert=cacert,
account=account,
token=token,
secret=secret,
**extra_args,
)

(
ce.validate_server()
.validate_app_mode(app_mode=app_mode)
Expand Down
76 changes: 76 additions & 0 deletions rsconnect/shiny_express.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# The contents of this file are copied from:
# https://github.com/posit-dev/py-shiny/blob/feb4cb7f872922717c39753514ae2d7fa32f10a1/shiny/express/_is_express.py

from __future__ import annotations

import ast
from pathlib import Path

__all__ = ("is_express_app",)


def is_express_app(app: str, app_dir: str | None) -> bool:
"""Detect whether an app file is a Shiny express app

Parameters
----------
app
App filename, like "app.py". It may be a relative path or absolute path.
app_dir
Directory containing the app file. If this is `None`, then `app` must be an
absolute path.

Returns
-------
:
`True` if it is a Shiny express app, `False` otherwise.
"""
if not app.lower().endswith(".py"):
return False

if app_dir is not None:
app_path = Path(app_dir) / app
else:
app_path = Path(app)

if not app_path.exists():
return False

try:
# Read the file, parse it, and look for any imports of shiny.express.
with open(app_path) as f:
content = f.read()
tree = ast.parse(content, app_path)
detector = DetectShinyExpressVisitor()
detector.visit(tree)

except Exception:
return False

return detector.found_shiny_express_import


class DetectShinyExpressVisitor(ast.NodeVisitor):
def __init__(self):
super().__init__()
self.found_shiny_express_import = False

def visit_Import(self, node: ast.Import):
if any(alias.name == "shiny.express" for alias in node.names):
self.found_shiny_express_import = True

def visit_ImportFrom(self, node: ast.ImportFrom):
if node.module == "shiny.express":
self.found_shiny_express_import = True
elif node.module == "shiny" and any(
alias.name == "express" for alias in node.names
):
self.found_shiny_express_import = True

# Visit top-level nodes.
def visit_Module(self, node: ast.Module):
super().generic_visit(node)

# Don't recurse into any nodes, so the we'll only ever look at top-level nodes.
def generic_visit(self, node: ast.AST):
pass
Loading