Skip to content

Add dev_mode param to App() #1297

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

Closed
wants to merge 2 commits 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
23 changes: 17 additions & 6 deletions shiny/_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ class App:
----------
ui
The UI definition for the app (e.g., a call to :func:`~shiny.ui.page_fluid` or
similar, with layouts and controls nested inside). You can
also pass a function that takes a :class:`~starlette.requests.Request` and
returns a UI definition, if you need the UI definition to be created dynamically
for each pageview.
similar, with layouts and controls nested inside). You can also pass a function
that takes a :class:`~starlette.requests.Request` and returns a UI definition,
if you need the UI definition to be created dynamically for each pageview.
server
A function which is called once for each session, ensuring that each session is
independent.
Expand All @@ -66,6 +65,12 @@ class App:
that mount point.
debug
Whether to enable debug mode.
dev_mode
Whether to enable development mode. This enables an error console in the client
which displays JavaScript errors. If ``None`` (the default), then the default is
to check if the environment variable ``SHINY_DEV_MODE`` is set to `"1"` and if
so, enable dev mode. If ``True`` or ``False``, then that value will be used and
the environment variable will be ignored.

Examples
--------
Expand Down Expand Up @@ -115,6 +120,7 @@ def __init__(
*,
static_assets: Optional[str | Path | Mapping[str, str | Path]] = None,
debug: bool = False,
dev_mode: bool | None = None,
) -> None:
# Used to store callbacks to be called when the app is shutting down (according
# to the ASGI lifespan protocol)
Expand All @@ -134,6 +140,9 @@ def __init__(
)

self._debug: bool = debug
if dev_mode is None:
dev_mode = os.getenv("SHINY_DEV_MODE") == "1"
self._dev_mode: bool = dev_mode

# Settings that the user can change after creating the App object.
self.lib_prefix: str = LIB_PREFIX
Expand Down Expand Up @@ -438,7 +447,9 @@ def _render_page(self, ui: Tag | TagList, lib_prefix: str) -> RenderedHTML:
ui_res = copy.copy(ui)
# Make sure requirejs, jQuery, and Shiny come before any other dependencies.
# (see require_deps() for a comment about why we even include it)
ui_res.insert(0, [require_deps(), jquery_deps(), *shiny_deps()])
ui_res.insert(
0, [require_deps(), jquery_deps(), *shiny_deps(dev_mode=self._dev_mode)]
)
rendered = HTMLDocument(ui_res).render(lib_prefix=lib_prefix)
self._ensure_web_dependencies(rendered["dependencies"])
return rendered
Expand All @@ -449,7 +460,7 @@ def _render_page_from_file(self, file: Path, lib_prefix: str) -> RenderedHTML:

doc = HTMLTextDocument(
page_html,
deps=[require_deps(), jquery_deps(), *shiny_deps()],
deps=[require_deps(), jquery_deps(), *shiny_deps(dev_mode=self._dev_mode)],
deps_replace_pattern='<meta name="shiny-dependency-placeholder" content="">',
)

Expand Down
6 changes: 2 additions & 4 deletions shiny/html_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from __future__ import annotations

import os

from htmltools import HTMLDependency


def shiny_deps() -> list[HTMLDependency]:
def shiny_deps(dev_mode: bool = False) -> list[HTMLDependency]:
deps = [
HTMLDependency(
name="shiny",
Expand All @@ -15,7 +13,7 @@ def shiny_deps() -> list[HTMLDependency]:
stylesheet={"href": "shiny.min.css"},
)
]
if os.getenv("SHINY_DEV_MODE") == "1":
if dev_mode:
deps.append(
HTMLDependency(
"shiny-devmode",
Expand Down
Loading