Skip to content

Commit

Permalink
Refactor: Shinylive URL encode/decode (#23)
Browse files Browse the repository at this point in the history
* Refactor `url_{encode,decode}` to return a ShinyliveIoApp object

* Add +/- methods for ShinyliveIoAppLocal

* docs: document all the things

* feat(url_decode): Track mode/header in object

* tests: Add some very basic tests

* docs: update news

* fix: Missed setting language

* fix: chunk engine is `shinylive-python` not `shinylive-py`

* fix: Use snakecase for `viewer_height`

* several improvements

* Implement `__add__` and `__sub__` for all ShinyliveIoApp objects
* +/- now return copies
* `root_dir` can be None, new files are added "flattened"
* More documentation

* feat: Add `add_dir()` method

* chore: ShinyLive -> shinylive

* remove some unnecessary pythonic fanciness

* Make mode, header, host public and document

* Add alternate constructors

* Rename class ShinyliveApp

* update `url_encode()` to return a string

* flip order of logical section, to prioritize True

* export ShinyliveApp

* simplify setting language attribute

* update CLI to use new ShinyliveApp constructors

* tests: fix tests

* fix setting header in constructor

* Add `.remove_file()` method and call in `__sub__`

* Allow method chaining in ShinyliveApp methods

* rename methods `.url()` -> `.to_url()`

also for json, chunk, chunk_contents

* docs: update changelog

* docs: shinylive -> Shinylive

This seems to be the preferred capitalization from other docs

* remove trailing slashes from host

* Update reference to ShinyliveIoApp
  • Loading branch information
gadenbuie authored Jan 24, 2024
1 parent c56417f commit 55031e0
Show file tree
Hide file tree
Showing 5 changed files with 718 additions and 195 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [UNRELEASED]

* Added `shinylive url encode` and `shinylive url decode` commands to encode local apps into a shinylive.io URL or decode a shinylive.io URL into local files. These commands are accompanied by `encode_shinylive_url()` and `decode_shinylive_url()` functions for programmatic use. (#20)
* Added `shinylive url encode` and `shinylive url decode` commands to encode local apps into a [shinylive.io](https://shinylive.io) URL or decode a [shinylive.io](https://shinylive.io) URL into local files. These commands are accompanied by `url_encode()` and `url_decode()` functions for programmatic use. They are supported by the new `ShinyliveIoApp` class which provides methods to get the app URL, save the app locally, or create a [Shinylive quarto chunk](https://quarto-ext.github.io/shinylive/) from the app's files. (#20, #23)

## [0.1.3] - 2024-12-19

Expand Down
4 changes: 2 additions & 2 deletions shinylive/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""A package for packaging Shiny applications that run on Python in the browser."""

from ._url import decode_shinylive_url, encode_shinylive_url
from ._url import ShinyliveApp, url_decode, url_encode
from ._version import SHINYLIVE_PACKAGE_VERSION

__version__ = SHINYLIVE_PACKAGE_VERSION

__all__ = ("decode_shinylive_url", "encode_shinylive_url")
__all__ = ("ShinyliveApp", "url_decode", "url_encode")
43 changes: 15 additions & 28 deletions shinylive/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@
import click

from . import _assets, _deps, _export
from ._url import (
create_shinylive_bundle_file,
create_shinylive_bundle_text,
create_shinylive_chunk_contents,
create_shinylive_url,
decode_shinylive_url,
detect_app_language,
write_files_from_shinylive_io,
)
from ._url import ShinyliveApp, detect_app_language, url_decode
from ._utils import print_as_json
from ._version import SHINYLIVE_ASSETS_VERSION, SHINYLIVE_PACKAGE_VERSION

Expand Down Expand Up @@ -562,30 +554,25 @@ def encode(
else:
lang = detect_app_language(app_in)

if "\n" in app_in:
bundle = create_shinylive_bundle_text(app_in, files, lang)
if app == "-":
sl_app = ShinyliveApp.from_text(
app_in, files=files, language=lang, mode=mode, header=not no_header
)
else:
bundle = create_shinylive_bundle_file(app_in, files, lang)
sl_app = ShinyliveApp.from_local(
app_in, files=files, language=lang, mode=mode, header=not no_header
)

if json:
print_as_json(bundle)
print(sl_app.to_json(indent=None))
if not view:
return

url = create_shinylive_url(
bundle,
lang,
mode=mode,
header=not no_header,
)

if not json:
print(url)
print(sl_app.to_url())

if view:
import webbrowser

webbrowser.open(url)
sl_app.view()


@url.command(
Expand Down Expand Up @@ -622,16 +609,16 @@ def decode(url: str, dir: Optional[str] = None, json: bool = False) -> None:
url_in = sys.stdin.read()
else:
url_in = url
bundle = decode_shinylive_url(str(url_in))
sl_app = url_decode(url_in)

if json:
print_as_json(bundle)
print(sl_app.to_json(indent=None))
return

if dir is not None:
write_files_from_shinylive_io(bundle, dir)
sl_app.write_files(dir)
else:
print(create_shinylive_chunk_contents(bundle))
print(sl_app.to_chunk_contents())


# #############################################################################
Expand Down
Loading

0 comments on commit 55031e0

Please sign in to comment.