From b7f7fc0b1e9e647a0f8b652990b31b33d93b7ebe Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Tue, 9 Jul 2024 18:47:26 -0500 Subject: [PATCH] Abstract out express_core_preview() internals into a app_preview_code() helper so that shiny-dev-center can use it (#172) --- docs/helpers.py | 74 +++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/docs/helpers.py b/docs/helpers.py index 9b5fee6..f7f561f 100644 --- a/docs/helpers.py +++ b/docs/helpers.py @@ -207,43 +207,57 @@ def shinylive_app_preview( def express_core_preview( - app_express: str | None = None, - app_core: str | None = None, - files: list[str] | str | None = None, - div_attrs=".shiny-mode-tabset", - group="shiny-app-mode", - language="py", - **kwargs, + app_express: str, + app_core: str, + div_attrs: str = '.shiny-mode-tabset group="shiny-app-mode"', ) -> None: - if app_express is None and app_core is None: - return - - if files is None: - files = [] - elif isinstance(files, str): - files = [files] + app_preview_code( + { + "Express": app_express, + "Core": app_core, + }, + div_attrs=div_attrs, + ) - header_attrs = ".panel-tabset" - header_attrs += " " + div_attrs if div_attrs else "" - header_attrs += f" group='{group}'" if group else "" - block = QuartoPrint(["::: {" + header_attrs + "}"]) +def app_preview_code( + app_files: str | dict[str, str], + files: list[str] | str | None = None, + language: Literal["auto", "py", "r"] = "auto", + div_attrs: str = "", +) -> None: - apps = zip([app_express, app_core], ["Express", "Core"]) + is_tabset = isinstance(app_files, dict) - for app_file, tab_name in apps: - if app_file is None: - continue + if is_tabset: + div_attrs = ".panel-tabset " + div_attrs - sl_app = ShinyliveApp.from_local(app_file, files, language) - lang = "python" if language == "py" else "r" + block = QuartoPrint(["::: {" + div_attrs + "}"]) - block.append("### " + tab_name) - block.append( - f'```{{.{lang} .code-overflow-scroll shinylive="{sl_app.to_url()}"}}' - ) - block.append_file(app_file) - block.extend(["```", ""]) + if not is_tabset: + _add_code_chunk(block, app_files, files, language) + else: + for x in app_files: + block.append(f"## {x}") + _add_code_chunk(block, app_files[x], files, language) block.append(":::") print(block) + + +def _add_code_chunk( + block: QuartoPrint, + file: str, + files: list[str] | str | None = None, + language: Literal["auto", "py", "r"] = "auto", +): + if language == "auto": + language = "py" if file.endswith(".py") else "r" + + sl_app = ShinyliveApp.from_local(file, files=files, language=language) + + lang = "python" if language == "py" else "r" + + block.append(f'```{{.{lang} .code-overflow-scroll shinylive="{sl_app.to_url()}"}}') + block.append_file(file) + block.append("```")