Git Product home page Git Product logo

py-shinylive's Introduction

Shinylive Python package

Build and test PyPI Latest Release

Documentation site

This repository contains a Python package for exporting Shiny applications as Shinylive applications.

This repository is not the same as the https://github.com/posit-dev/shinylive repository. That repository is used to generate the Shinylive assets distribution, which is a bundle containing HTML, JavaScript, CSS, and wasm files. The Python package in this repository downloads the assets and uses them to create Shinylive applications.

Installation

pip install shinylive

Usage

(Optional) Create a basic shiny application in a new directory myapp/:

shiny create myapp

Once you have a Shiny application in myapp/ and would like turn it into a Shinylive app in site/:

shinylive export myapp site

Then you can preview the application by running a web server and visiting it in a browser:

python3 -m http.server --directory site --bind localhost 8008

At this point, you can deploy the site/ directory to any static web hosting service.

Multiple applications

If you have multiple applications that you want to put on the same site, you can export them to subdirectories of the site, so that they can all share the same Shinylive assets. You can do this with the --subdir option:

shinylive export myapp1 site --subdir app1
shinylive export myapp2 site --subdir app2

Shinylive asset management

Each version of the Shinylive Python package is associated with a particular version of the Shinylive web assets. (See the releases here.)

To see which version of this Python package you have, and which version of the web assets it is associated with, simply run shinylive at the command prompt:

$ shinylive
Usage: shinylive [OPTIONS] COMMAND [ARGS]...

  shinylive Python package version: 0.1.0
  shinylive web assets version:     0.2.1
...

The web assets will be downloaded and cached the first time you run shinylive export. Or, you can run shinylive assets download to fetch them.

$ shinylive assets download
Downloading https://github.com/posit-dev/shinylive/releases/download/v0.2.1/shinylive-0.2.1.tar.gz...
Unzipping to /Users/username/Library/Caches/shinylive/

To see what versions you have installed, run shinylive assets info:

$ shinylive assets info
    Local cached shinylive asset dir:
    /Users/username/Library/Caches/shinylive

    Installed versions:
    /Users/username/Library/Caches/shinylive/shinylive-0.2.1
    /Users/username/Library/Caches/shinylive/shinylive-0.0.6

You can remove old versions with shinylive assets cleanup. This will remove all versions except the one that the Python package wants to use:

$ shinylive assets cleanup
Keeping version 0.2.1
Removing /Users/username/Library/Caches/shinylive/shinylive-0.0.6

If you want to force it to remove a specific version, use the shinylive assets remove xxx:

$ shinylive assets remove 0.2.1
Removing /Users/username/Library/Caches/shinylive/shinylive-0.2.1

py-shinylive's People

Contributors

wch avatar schloerke avatar gadenbuie avatar cpsievert avatar dlukes avatar

Stargazers

Fernando da Silva avatar Ned Letcher avatar Dongho Park avatar Xianying Tan avatar Gaurav avatar Ali Mhdian avatar Ben Hammond avatar treecko avatar  avatar ypa y yhm avatar Inayet Hadi  avatar Kamil Mielczarek avatar Robert Garcia Ventura avatar Lukas Augustin avatar Bobo Jamson avatar liudonghua avatar Tom Mock avatar Jacques Booysen avatar Rishi Yadav avatar  avatar Alexander Clarke avatar Trang Le avatar antx avatar Ozgur avatar Sam Parmar avatar alek avatar  avatar Brancen Gregory avatar James Wade avatar Páll Haraldsson avatar Pawel Cwiek avatar Marcelo Albuquerque avatar  avatar Ilja avatar Function avatar Michael Sumner avatar Ariane Hayana avatar Yousuf Ali avatar  avatar

Watchers

Andrew Holz avatar  avatar Neal Richardson avatar Tareef Kawaf avatar  avatar  avatar

py-shinylive's Issues

If a `requirements.txt` is found, only inspect those packages for dependencies

Request:

Hmm, shinylive appears to scan every .py file in the directory to figure out what pyodide needs to install, even scripts that aren't used at runtime. Like sometimes I'll have a helper script that builds a database (in this case, using sqlalchemy) but the actual shiny app only needs built-in sqlite3.

I hacked around this by making a stripped-down branch and exporting that, but it could be nice if either the build process honored requirements.txt (in a minimalistic sense) or if somehow it only scanned app.py and what it imports? or some other solution to ignore certain files when figuring this out.

My reply:

... correct. py-shinylive currently scans every that app.json provides. (Which is every file.) Link

It seems fair to me that if a requirements.txt is found, then only those packages are inspected.

Error copying packages to `site/shinylive/pyodide/`

Hi there,

I was trying out shinylive on a virtualenv on 3.9.14 and could not get it to work:

pip install shiny

pip install shinylive

shiny create myapp

shinylive export myapp site

Creating site/
Copying base Shinylive files from /Users/XX/Library/Caches/shinylive/shinylive-0.0.8/ to site/
Copying imported packages from /Users/XX/Library/Caches/shinylive/shinylive-0.0.8/shinylive/pyodide/ to site/shinylive/pyodide/
Traceback (most recent call last):
  File "/usr/local/bin/shinylive", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/shinylive/_main.py", line 79, in export
    _export.export(
  File "/usr/local/lib/python3.9/site-packages/shinylive/_export.py", line 101, in export
    copy_fn(src_path, dest_path)
  File "/usr/local/lib/python3.9/site-packages/shinylive/_utils.py", line 93, in copy_fn
    verbose_print(f"Skipping {dst}", file=sys.stderr)
TypeError: verbose_print() got an unexpected keyword argument 'file'

Any idea what might be going on? Really looking forward to playing around with this!

Jinja2 wheel not bundled unless explicitly imported in the app

Putting Jinja2 in a requirements.txt file, as suggested e.g. here, doesn't seem to be enough for the shinylive CLI tool to actually bundle the Jinja2 wheel in the output dir. But if I explicitly import jinja2 in app.py, the wheel gets included and rendering tables works.

Is this intentional? If so, it should probably be documented?

As an aside, thank you for bringing Shiny to Python in great style!

How to make API calls with shinylive?

I'm working on a toy application to show how a shinylive app could make API calls. It's turning out to be harder than I thought.
It seems like networking in general does not work very well with pyodide (and thus shinylive). I'm still learning here, but the issue looks to be accessing sockets.

A few posts (e.g., here) suggest that you should be able to do this with javascript. I see the tags reference in support, but I'm a bit lost in how to implement this in practice.

My toy app is calling a published API that was built on the palmerpenguins data set. Here is the code that works as a standalone shiny app but not with shinylive:

from shiny import *
import urllib.request
import json

app_ui = ui.page_fluid(
    ui.layout_sidebar(
    ui.panel_sidebar(
    ui.input_select("species", "Penguin Species",
                    {"Gentoo":"Gentoo", "Chinstrap":"Chinstrap", "Adelie":"Adelie"}),
    ui.input_action_button("go", "Predict", width="100%")),
    ui.panel_main(ui.h2(ui.output_text("txt"))))
)

def server(input, output, session):
    @output
    @render.text
    @reactive.event(input.go)
    def txt():
       url = "http://penguin.eastus.azurecontainer.io:8000/predict"
       payload = [{"species":input. Species(),
                   "bill_length_mm":40.5,
                   "bill_depth_mm":18.9,
                   "flipper_length_mm":180,
                   "body_mass_g":3950}]
       headers = {"Content-Type": "application/json"}
       data = json.dumps(payload).encode("utf-8")
       headers = {k: v.encode("utf-8") for k, v in headers. Items()}
       request = urllib.request.Request(url, data, headers)
       response = urllib.request.urlopen(request)
       data = response. Read()
       response_data = json.loads(data. Decode("utf-8"))
       prediction = response_data[0]
       class_pred = prediction[".pred_class"]
       return f"The {input.species()} 🐧 is predicted to be {class_pred}." 
  
app = App(app_ui, server)

Is there a known workaround with the python route that I'm unable to find? If not, any suggestions on how to insert a javascript component that can take the dynamic inputs from the UI to be used for the prediction?

loading a local *.csv file

I couldn't understand from documentation how can I make a local file accessible to the application, so that it would be possible to read_csv and do things like that?
Should I copy it to some location in shinylive folder? An what path should I provide for the script to work?

Some files in project directory can be accessed by the py-shiny app which works fine, but shinylive app gives file_not_found_error if I build a shinylive app from exactly the same project.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.