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

In Express, set session to None when importing shared #1082

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions shiny/express/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
from .expressify_decorator._func_displayhook import _expressify_decorator_function_def
from .expressify_decorator._node_transformers import (
DisplayFuncsTransformer,
ImportSharedSessionContextTransformer,
expressify_decorator_func_name,
session_context_func_name,
)

__all__ = ("wrap_express_app",)
Expand Down Expand Up @@ -67,6 +69,7 @@ def run_express(file: Path) -> Tag | TagList:

tree = ast.parse(content, file)
tree = DisplayFuncsTransformer().visit(tree)
tree = ImportSharedSessionContextTransformer("shared").visit(tree)
tree = ast.fix_missing_locations(tree)

ui_result: Tag | TagList = TagList()
Expand All @@ -87,6 +90,7 @@ def set_result(x: object):
var_context: dict[str, object] = {
"__file__": file_path,
expressify_decorator_func_name: _expressify_decorator_function_def,
session_context_func_name: session_context,
"input": InputNotImportedShim(),
}

Expand Down
31 changes: 31 additions & 0 deletions shiny/express/expressify_decorator/_node_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

sys_alias = "__auto_displayhook_sys__"
expressify_decorator_func_name = "_expressify_decorator_function_def"
session_context_func_name = "__shiny_session_context__"


class TopLevelTransformer(ast.NodeTransformer):
Expand Down Expand Up @@ -125,3 +126,33 @@ def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> object:
0, ast.Name(id=expressify_decorator_func_name, ctx=ast.Load())
)
return node


class ImportSharedSessionContextTransformer(TopLevelTransformer):
def __init__(self, module_name: str):
self.module_name = module_name

def visit_Import(self, node: ast.Import) -> ast.AST:
if any(alias.name == self.module_name for alias in node.names):
return self._create_with_block(node)
return node

def visit_ImportFrom(self, node: ast.ImportFrom) -> ast.AST:
if node.module == self.module_name:
return self._create_with_block(node)
return node

def _create_with_block(self, original_node: ast.Import | ast.ImportFrom):
with_node = ast.With()
with_node.items = [
ast.withitem(
context_expr=ast.Call(
func=ast.Name(id=session_context_func_name, ctx=ast.Load()),
args=[ast.Constant(value=None)],
keywords=[],
),
optional_vars=None,
)
]
with_node.body = [original_node]
return with_node
Loading