Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Winston Chang <[email protected]>
  • Loading branch information
gadenbuie and wch authored Jan 6, 2024
1 parent 331f958 commit 2b9d3ce
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
9 changes: 3 additions & 6 deletions shinylive/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ def url() -> None:
def encode(
app: str,
files: Optional[tuple[str, ...]] = None,
mode: str = "editor",
mode: Literal["editor", "app"] = "editor",
language: Optional[str] = None,
no_header: bool = False,
view: bool = False,
Expand All @@ -551,7 +551,6 @@ def encode(
import webbrowser

webbrowser.open(url)
return


@url.command(
Expand All @@ -577,7 +576,7 @@ def encode(
"--json",
is_flag=True,
default=False,
help="Prints the decoded shinylive bundle as JSON to stdout, ignoring --out.",
help="Prints the decoded shinylive bundle as JSON to stdout, ignoring --dir.",
)
@click.argument("url", type=str, nargs=1, default="-")
def decode(url: str, dir: Optional[str] = None, json: bool = False) -> None:
Expand All @@ -595,7 +594,7 @@ def decode(url: str, dir: Optional[str] = None, json: bool = False) -> None:
import os

out_dir = Path(dir)
os.makedirs(out_dir, exist_ok=True)
out_dir.mkdir(parents=True, exist_ok=True)
for file in bundle:
with open(out_dir / file["name"], "w") as f_out:
f_out.write(
Expand All @@ -610,8 +609,6 @@ def decode(url: str, dir: Optional[str] = None, json: bool = False) -> None:
print("")
print(file["content"].encode("utf-8", errors="ignore").decode("utf-8"))

return


# #############################################################################
# ## Deprecated commands
Expand Down
26 changes: 9 additions & 17 deletions shinylive/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import re
from pathlib import Path
from typing import List, Literal, Optional, TypedDict, cast
from typing import Literal, Optional, TypedDict, cast


class FileContentJson(TypedDict):
Expand All @@ -16,9 +16,9 @@ class FileContentJson(TypedDict):

def encode_shinylive_url(
app: str | Path,
files: Optional[tuple[str | Path, ...]] = None,
mode: str = "editor",
language: Optional[str] = None,
files: Optional[str | Path | Sequence[str | Path]] = None,
mode: Literal["editor", "app"] = "editor",
language: Optional[Literal["py", "r"]] = None,
header: bool = True,
) -> str:
"""
Expand All @@ -35,11 +35,11 @@ def encode_shinylive_url(
these files will be stored relative to the main `app` file. If an entry in files
is a directory, then all files in that directory will be included, recursively.
mode
The mode of the application. Defaults to "editor".
The mode of the application, either "editor" or "app". Defaults to "editor".
language
The language of the application. Defaults to None.
The language of the application, or None to autodetect the language. Defaults to None.
header
Whether to include a header. Defaults to True.
Whether to include a header bar in the UI. This is used only if ``mode`` is "app". Defaults to True.
Returns
-------
Expand All @@ -57,7 +57,6 @@ def encode_shinylive_url(
{
"name": f"app.{'py' if language == 'py' else 'R'}",
"content": app,
"type": "text",
}
]
else:
Expand All @@ -78,11 +77,7 @@ def encode_shinylive_url(
read_file(file, root_dir) for file in file_list if Path(file) != app_path
]

if language in ["py", "python"]:
language = "py"
elif language in ["r", "R"]:
language = "r"
else:
if language not in ["py", "r"]:
raise ValueError(
f"Language '{language}' is not supported. Please specify one of 'py' or 'r'."
)
Expand Down Expand Up @@ -110,8 +105,7 @@ def detect_app_language(app: str | Path) -> str:
else:
raise ValueError(err_not_detected)

if not isinstance(app, Path):
app = Path(app)
app = Path(app)

if app.suffix.lower() == ".py":
return "py"
Expand Down Expand Up @@ -171,8 +165,6 @@ def decode_shinylive_url(url: str) -> List[FileContentJson]:
raise ValueError(
f"Invalid shinylive URL: unexpected file type '{file['type']}' in '{file['name']}'."
)
elif "type" not in file:
file["type"] = "text"
if not all(isinstance(value, str) for value in file.values()): # type: ignore
raise ValueError(
f"Invalid shinylive URL: not all items in '{file['name']}' were strings."
Expand Down

0 comments on commit 2b9d3ce

Please sign in to comment.