Skip to content
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

Refactor: Shinylive URL encode/decode #23

Merged
merged 30 commits into from
Jan 24, 2024
Merged

Conversation

gadenbuie
Copy link
Contributor

@gadenbuie gadenbuie commented Jan 22, 2024

#20 introduced shinylive url commands and a set of functions to create and decode shinylive.io URLs. In practice, I've found the functions to be very helpful. But the functional design I used in #20 introduces some friction when I need to work with an app bundle and format it or manipulate it.

This PR refactors the shinylive.io url encoding and decoding to use a central ShinyliveApp class that implements a number of useful methods. This makes it much easier to use the app bundle built with url_encode() or url_decode() in a variety of settings and to produce several outputs from the same set of app files, without having to reach into the shinylive._url module.

As part of this, I renamed encode_shinylive_url() and decode_shinylive_url() to url_encode() and url_decode(), since they'll most often be called as shinylive.url_encode().

url_decode() will now return a ShinyliveApp, while url_encode() still returns the Shinylive URL as a string.

The ShinyliveApp class implements:

  • .to_url() to return the shinylive.io URL
  • .to_chunk_contents() to create the shinylive chunk contents for a quarto shinylive chunk
  • .to_chunk() to create a complete shinylive chunk, including the header and options, from the app
  • .to_json() to return the JSON representation of the app bundle
  • .write_files() to write the app bundle to a directory
  • .view() to open the shinylive.io URL in a web browser

The ShinyliveApp constructor takes a ready-to-go app bundle of list[FileContentJson], but most users will instead use class methods to construct the ShinyliveApp from various sources:

  • .from_url() decodes a URL into a ShinyliveApp
  • .from_local() creates a ShinyliveApp from a set of local files
  • .from_text() creates a ShinyliveApp from a string with the contents of app.py or app.R (optionally with support of local files).

Once created, there are additional methods to help add to or remove from the app bundle. These methods all allow for method chaining.

  • .add_file(), add_files() or .add_file_contents() (i.e. text files with a given name),
  • .remove_file() to take a file, by name, out of the bundle.

Additionally, I implemented + and - operators for the local app class to add and remove files from the bundle.

@gadenbuie gadenbuie requested a review from wch January 22, 2024 18:06
@gadenbuie
Copy link
Contributor Author

@wch this is ready for final review. I updated the main PR post to reflect the changed behavior after your review yesterday. Some quick callouts:

  • Now only one ShinyliveApp class with alternate constructors .from_url(), .from_local() and .from_text().
  • url_encode() returns the URL string but the ShinyliveApp is now exported.
  • I renamed the methods .url(), .json(), etc. to .to_url(), .to_json()...
  • Simplified class attributes and made them public by including them as params of __init__.
  • host is now a class attribute that defaults to https://shinylive.io but is parsed from the url in .from_url().

shinylive/_url.py Outdated Show resolved Hide resolved
Co-authored-by: Winston Chang <winston@stdout.org>
@gadenbuie gadenbuie merged commit 55031e0 into main Jan 24, 2024
5 checks passed
@gadenbuie gadenbuie deleted the shinylive-bundle-class branch January 24, 2024 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants