Skip to content

Commit

Permalink
Enable dev mode error console for Python
Browse files Browse the repository at this point in the history
This enables the dev mode error console for Py Shiny apps,
except when the mode is viewer-only.
  • Loading branch information
wch committed Apr 9, 2024
1 parent 5626639 commit 5290904
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ export function App({
<Viewer
proxyHandle={proxyHandle}
setViewerMethods={setViewerMethods}
devMode={true}
/>
</ResizableGrid>
</>
Expand Down Expand Up @@ -410,6 +411,7 @@ export function App({
<Viewer
proxyHandle={proxyHandle}
setViewerMethods={setViewerMethods}
devMode={true}
/>
</ResizableGrid>
</>
Expand Down Expand Up @@ -496,7 +498,11 @@ export function App({
appEngine={appEngine}
/>
</React.Suspense>
<Viewer proxyHandle={proxyHandle} setViewerMethods={setViewerMethods} />
<Viewer
proxyHandle={proxyHandle}
setViewerMethods={setViewerMethods}
devMode={true}
/>
</ResizableGrid>
);
} else if (appMode === "viewer") {
Expand All @@ -519,6 +525,7 @@ export function App({
<Viewer
proxyHandle={proxyHandle}
setViewerMethods={setViewerMethods}
devMode={false}
/>
</div>
</>
Expand Down
3 changes: 3 additions & 0 deletions src/Components/Viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ async function resetRAppFrame(
export function Viewer({
proxyHandle,
setViewerMethods,
devMode = false,
}: {
proxyHandle: ProxyHandle;
setViewerMethods: React.Dispatch<React.SetStateAction<ViewerMethods>>;
devMode?: boolean;
}) {
const viewerFrameRef = React.useRef<HTMLIFrameElement>(null);
const [appRunningState, setAppRunningState] = React.useState<
Expand Down Expand Up @@ -268,6 +270,7 @@ export function Viewer({
await pyodideproxy.callPyAsync({
fnName: ["_start_app"],
args: [appName],
kwargs: { dev_mode: devMode },
});

viewerFrameRef.current.src = appInfo.urlPath;
Expand Down
10 changes: 9 additions & 1 deletion src/hooks/usePyodide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,8 @@ class ShinyExpressAppModule:
import shiny.express
self.app = shiny.express.wrap_express_app(app_path)
async def _start_app(app_name, scope = _shiny_app_registry):
async def _start_app(app_name, scope = _shiny_app_registry, dev_mode = False):
import os
import sys
import importlib
import shiny.express
Expand All @@ -537,11 +538,18 @@ async def _start_app(app_name, scope = _shiny_app_registry):
class ModuleApp:
app = None
if dev_mode:
# Enable shiny dev mode for error console
os.environ["SHINY_DEV_MODE"] = "1"
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")
if dev_mode:
os.environ.pop("SHINY_DEV_MODE")
scope[app_name] = app_obj
sys.path.remove(app_dir)
Expand Down

0 comments on commit 5290904

Please sign in to comment.