-
Notifications
You must be signed in to change notification settings - Fork 75
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
Add support for Quarto integration #746
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
cbde3a9
Basic quarto extension functionality
wch 7e36025
Inject HTML dependencies using placeholder
wch 4eafba6
Bump htmltools requirement
wch f941f5a
Use placeholder dependency
wch 488f880
Use new cell export format
wch f6df69d
Move code to quarto.py
wch 3e44c88
Remove output_shim
wch 50a7a73
Get html file name from json
wch b9142a7
Add app file argument
wch 0a53476
Specify reload_excludes
wch 8320d1e
Add --reload-includes option
wch e086c1a
Bump version to 0.5.1.9002
wch 0f2a3e7
Add support for server-global and server-session contexts
wch b5c00b4
Bump version to 0.5.1.9003
wch 5bbc1a6
Formatting update
wch 08a0d8f
Remove old comment
wch e889575
Remove default for output py file
wch 0a62b6b
Use new context names
wch 996e56c
Revert output_fn changes
wch 1c683aa
Get py-htmltools from github
wch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
"""Tools for parsing ipynb files.""" | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
|
||
__all__ = ("convert_code_cells_to_app_py", "get_shiny_deps") | ||
|
||
from typing import Literal, cast | ||
|
||
from ._typing_extensions import NotRequired, TypedDict | ||
|
||
QuartoShinyCodeCellClass = Literal["python", "r", "cell-code", "hidden"] | ||
QuartoShinyCodeCellContext = Literal["ui", "server", "server-setup"] | ||
|
||
|
||
class QuartoShinyCodeCell(TypedDict): | ||
text: str | ||
context: list[QuartoShinyCodeCellContext] | ||
classes: list[QuartoShinyCodeCellClass] | ||
|
||
|
||
class QuartoShinyCodeCells(TypedDict): | ||
schema_version: int | ||
cells: list[QuartoShinyCodeCell] | ||
html_file: str | ||
|
||
|
||
def convert_code_cells_to_app_py(json_file: str | Path, app_file: str | Path) -> None: | ||
"""Parse an code cell JSON file and output an app.py file.""" | ||
import json | ||
from textwrap import indent | ||
|
||
json_file = Path(json_file) | ||
app_file = Path(app_file) | ||
|
||
with open(json_file, "r") as f: | ||
data = cast(QuartoShinyCodeCells, json.load(f)) | ||
|
||
if data["schema_version"] != 1: | ||
raise ValueError("Only schema_version 1 is supported.") | ||
|
||
cells = data["cells"] | ||
|
||
session_code_cell_texts: list[str] = [] | ||
global_code_cell_texts: list[str] = [] | ||
|
||
for cell in cells: | ||
if "python" not in cell["classes"]: | ||
continue | ||
|
||
if "server-setup" in cell["context"]: | ||
global_code_cell_texts.append(cell["text"] + "\n\n# " + "=" * 72 + "\n\n") | ||
elif "server" in cell["context"]: | ||
session_code_cell_texts.append( | ||
indent(cell["text"], " ") + "\n\n # " + "=" * 72 + "\n\n" | ||
) | ||
|
||
app_content = f""" | ||
from pathlib import Path | ||
from shiny import App, Inputs, Outputs, Session, ui | ||
|
||
{ "".join(global_code_cell_texts) } | ||
|
||
|
||
def server(input: Inputs, output: Outputs, session: Session) -> None: | ||
{ "".join(session_code_cell_texts) } | ||
|
||
app = App( | ||
Path(__file__).parent / "{ data["html_file"] }", | ||
server, | ||
static_assets=Path(__file__).parent, | ||
) | ||
""" | ||
|
||
with open(app_file, "w") as f: | ||
f.write(app_content) | ||
|
||
|
||
# ============================================================================= | ||
# HTML Dependency types | ||
# ============================================================================= | ||
class QuartoHtmlDepItem(TypedDict): | ||
name: str | ||
path: str | ||
attribs: NotRequired[dict[str, str]] | ||
|
||
|
||
class QuartoHtmlDepServiceworkerItem(TypedDict): | ||
source: str | ||
destination: str | ||
|
||
|
||
class QuartoHtmlDependency(TypedDict): | ||
name: str | ||
version: NotRequired[str] | ||
scripts: NotRequired[list[str | QuartoHtmlDepItem]] | ||
stylesheets: NotRequired[list[str | QuartoHtmlDepItem]] | ||
resources: NotRequired[list[QuartoHtmlDepItem]] | ||
meta: NotRequired[dict[str, str]] | ||
serviceworkers: NotRequired[list[QuartoHtmlDepServiceworkerItem]] | ||
|
||
|
||
def placeholder_dep() -> QuartoHtmlDependency: | ||
return { | ||
"name": "shiny-dependency-placeholder", | ||
"version": "9.9.9", | ||
"meta": {"shiny-dependency-placeholder": ""}, | ||
} | ||
|
||
|
||
def get_shiny_deps() -> str: | ||
import json | ||
|
||
return json.dumps([placeholder_dep()], indent=2) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jcheng5 What do you think about taking a
Path
object to indicate that it should read the UI from a file?