Skip to content

Commit

Permalink
Abstract out express_core_preview() internals into a app_preview_code…
Browse files Browse the repository at this point in the history
…() helper so that shiny-dev-center can use it (#172)
  • Loading branch information
cpsievert authored Jul 9, 2024
1 parent c3192e5 commit b7f7fc0
Showing 1 changed file with 44 additions and 30 deletions.
74 changes: 44 additions & 30 deletions docs/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("```")

0 comments on commit b7f7fc0

Please sign in to comment.