Shinylive: Shiny + WebAssembly

Shinylive allows you to run Shiny applications entirely in a web browser, without the need for a separate server running Python.

The traditional way of deploying Shiny involves in a separate server and client: the server runs Python and Shiny, and clients connect via the web browser. Each client keeps an open websocket connection as long as they are using the application.

Traditional Shiny deployment

When an application is deployed with Shinylive, Python and Shiny run in the web browser: the browser is effectively both the client and server for the application. There is a web server that serves files, but it does not run Python or Shiny—it can be a “dumb” static web server.

Shinylive deployment

If you’ve looked at any of the documentation on this web site, or have played with any of the examples at shinylive.io, you have already used Shinylive. The examples on this site (with a handful of exceptions) and the shinylive.io examples all run using Shinylive, meaning that they run in your web browser.

This is all possible because of the magic of WebAssembly and Pyodide.

Applications deployed with Shinylive have some advantages and disadvantages compared to a traditional Shiny deployment. The advantages include:

Some of the disadvantages of using Shinylive deployments compared to traditional Shiny deployments:

For certain types of Shiny applications, some of the limitations can be worked around by pre-processing a data set and including it with the application.

One important difference between traditional Shiny and Shinylive deployments is that compute power is shifted from the server to the client. In many cases, the client browser will have more compute power than a server, especially since the compute power of the user’s machine is not shared across multiple users. However, in other cases, this can be a roadblock, such as when a powerful server is needed to perform very intensive computations or requires access to a private data store.

Sharing and deploying Shinylive applications

In this document, we’ll use the terms sharing and deploying Shiny applications. When we talk about sharing, we’re referring to a method of encoding the application in a URL so that others can run the application if they simply have the URL. Sharing an application via a URL does not require you to have a server—you can simply use the server at shinylive.io.

When we talk about deploying applications, we mean creating a set of files which are to be served up by a web server. This does require you to have a web server. For a traditional Shiny deployment, this means having a server that runs R or Python. For a Shinylive deployment, this only requires a server that can serve static files—it can be a “dumb” web server which does not run Python. For example you could deploy your application to GitHub Pages or Netlify.

Sharing Shinylive applications

The easiest way to share an application is to create it on the Shinylive editor, and then click on the “Create share link” button. This will encode the application in a URL, which you can then share with others.

Share button

The dialog box that appears will provide two links: one for the application in the Shinylive editor, and one with for the application running standalone.

Share URLs

Here is an example of a Shiny application that is encoded in a share URL. This will lead to the application with an editor and Python console:

https://shinylive.io/py/editor/#code=NobwRAdghgtgpmAXGKAHVA6VBPMAa…

If you want to share just the Shiny application, without the editor and console, use the other link, which contains /app/ instead of /editor/:

https://shinylive.io/py/app/#code=NobwRAdghgtgpmAXGKAHVA6VBPMAa…

These URLs have a hash that includes #code=.... The code for the entire application is encoded in that hash. Notably, web browsers do not send the hash to the web server, so the server actually never sees the content of the Shiny application.

The sharing dialog shows how long the URL is, in bytes. If you want to share a link on Twitter, the maximum length of a URL is about 4000 bytes, and it will be shortened using their t.co service. If you use bit.ly, the maximum length is about 2000 bytes. These link shorteners redirect the user to the longer URL.

Sharing with gists

Another way of sharing Shinylive applications is by using a GitHub gist. For example, the gist here:

https://gist.github.com/wch/e62218aa28bf26e785fc6cb99efe8efe

Can be run with Shinylive here:

Notice that the #gist=... part of the URL simply uses the ID of the gist.

To create a gist, you can go to gist.github.com/, or you can use GitHub’s gh command-line tool to create a gist from files on disk. To do that, first install gh, then use gh gist create:

gh gist create --public app.py

Sharing via gists has some important differences from sharing via encoded-app URL. If you use a gist, you can modify the gist, and the sharing URL will stay the same. If you are sharing an encoded-app URL, the URL itself contains the application code, so if you want modify the code, you will have to generate a new URL and share that.

Sharing via GitHub gist may not be appropriate for all use cases, because the GitHub API has rate limits: for a given IP address, the GitHub API allows 60 requests per hour. So an end user would only be able to load Shinylive applications 60 times in an hour. And if there are many users behind a single IP address with network address translation, they collectively would have a limit of 60 requests per hour.

If you are using GitHub gist for sharing, you can see your remaining requests at https://api.github.com/rate_limit.

Note

The GitHub API has a much higher rate limit if the end user is authenticated, but Shinylive currently does not support authenticating with GitHub.

Deploying Shinylive applications

With Quarto websites

Note

The section below describes how to embed Shinylive applications in a Quarto document – they can be thought of as Shiny applets in that mode. As of November 2023, the pre-release version of Quarto can work in a different mode: it can generate dashboards where the entire page is a single Shiny application. See this repository for an example and more information about how they work and how to deploy them. This page will be updated soon with more information about this mode.

The easiest way to deploy Shinylive applications is using the quarto-shinylive extension. This extension allows you to embed Shiny apps into a quarto html document, and deploy those applications anywhere that can host quarto websites. Once you have the extension installed, you can insert shinylive-python code blocks into the document.

```{shinylive-python}
#| standalone: true

from shiny import *

app_ui = ui.page_fluid(
    ui.input_slider("n", "N", 0, 100, 40),
    ui.output_text_verbatim("txt"),
)

def server(input, output, session):
    @output
    @render.text
    def txt():
        return f"The value of n*2 is {input.n() * 2}"

app = App(app_ui, server)
```

Without Quarto

If you’re not using Quarto, you’ll need to export and deploy your application yourself. This involves:

  • Exporting the application: Create a directory of files that includes the Shinylive distribution and the application code.
  • Deploying: Upload that directory to a static web host.

There are many ways to deploy to a static web server. For example, you could deploy to Netlify or GitHub Pages, or use Posit Connect, as described later in this page.

First, install the shinylive package:

pip install shinylive

Next, create a directory with a Shiny application. We’ll use the shiny create command to create a basic application in a directory called myapp/.

shiny create --dir myapp

Pick a Shiny app template to create in the myapp directory. Next, create the distribution with shinylive:

shinylive export myapp site

The resulting site directory will contain the following files (among others that are not shown for brevity):

site
├── app.json          # The application's files serialized to JSON
├── index.html        # A web page for the application
├── edit
│   └── index.html    # A web page for an editor view of the application
├── shinylive-sw.js   # Shinylive service worker
└── shinylive         # Shinylive content
    └── pyodide       # Pyodide files

This directory can now be deployed to a static web hosting service.

You can preview the application by serving the files in the site directory:

python3 -m http.server --directory site 8008

This will serve the files in the site directory on port 8008. Then point your browser at http://localhost:8008/. You can also see the application with an online editor by pointing your browser at http://localhost:8008/edit/. (Note that any changes to the files there are ephemeral—they won’t be saved to disk.)

Note

To run a Shinylive application, the files must be served with a web server; simply pointing your browser to the files on disk will not work. This is because security restrictions in web browsers require some assets to be retrieved from a web server instead of from disk.

If you have multiple applications, you may want to export them in 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

The site/shinylive/pyodide/ directory will contain a Pyodide distribution containing just the Python packages needed to run the exported application(s). There are some cases where you may want to include other packages. For example, if you want users who visit the edit/ URL to be able to load more packages. In order to include extra packages, you have two options:

  • Add a requirements.txt file to an application which lists the extra packages.
  • Run shinylive export myapp site --full-shinylive. This will cause it to include all of the Python packages from the Shinylive distribution.
Note

The Shinylive distribution is under rapid development, and the files in the distribution will change. The shinylive export command automatically downloads and caches a a copy of the Shinylive distribution on your computer. To make sure you are up to date, run:

pip install shinylive --upgrade
shinylive assets remove   # Remove old cached shinylive files

Then the next time you run shinylive export, it will download the latest version.

Deploying to Posit Connect

After creating the directory with the application and Shinylive bundle, you can deploy it to many different of static web hosting services. Posit Connect is one of those options, and allows you to control over who can access the application.

If you would like to deploy to a Posit Connect server, install and configure the rsconnect-python package as described in the Deploy page. Then you can deploy the application as a static website:

rsconnect deploy html site

Python packages

The Shinylive distribution is built on Pyodide, and contains a number of additional packages on top of the standard Pyodide distribution.

It is also possible to use other Python packages, provided that they are packaged as wheels, and contain no compiled code. Additionally, they must not use features that aren’t available in Pyodide. For example, if a package has code that uses urllib.request, it won’t work in Pyodide.

Installed packages

The Shinylive distribution includes packages from Pyodide 0.26.3, as well as some additional Shiny-related packages. See this page for a list of packages included in Pyodide.

Shinylive includes the following packages. Most are part of the Pyodide distribution, and a few of them are added by Shinylive.

Package Version
aiohttp 3.9.5
aiohttp-tests 3.9.5
aiosignal 1.3.1
altair 5.3.0
annotated-types 0.6.0
annotated-types-tests 0.6.0
anyio 4.4.0
appdirs 1.4.4
asciitree 0.3.3
asgiref 3.8.1
astropy 6.0.1
astropy_iers_data 0.2024.4.22.0.29.50
astropy_iers_data-tests 0.2024.4.22.0.29.50
asttokens 2.4.1
async-timeout 4.0.3
atomicwrites 1.4.1
attrs 23.2.0
autograd 1.6.2
autograd-tests 1.6.2
awkward-cpp 33
b2d 0.7.4
bcrypt 4.1.2
beautifulsoup4 4.12.3
beautifulsoup4-tests 4.12.3
biopython 1.83
bitarray 2.9.2
bitarray-tests 2.9.2
bitstring 4.1.4
black 24.8.0
bleach 6.1.0
bokeh 3.4.1
boost-histogram 1.4.1
branca 0.7.2
brotli 1.1.0
buffer-test 0.1.1
cachetools 5.3.3
Cartopy 0.23.0
Cartopy-tests 0.23.0
cbor-diag 1.0.1
certifi 2024.2.2
cffi 1.16.0
cffi_example 0.1
cftime 1.6.3
charset-normalizer 3.3.2
clarabel 0.7.1
click 8.1.7
cligj 0.7.2
cloudpickle 3.0.0
cmyt 2.0.0
cmyt-tests 2.0.0
colorspacious 1.1.2
comm 0.2.2
contourpy 1.2.1
coolprop 6.6.0
coolprop-tests 6.6.0
coverage 7.4.4
cpp-exceptions-test 0.1
cpp-exceptions-test2 1.0
cramjam 2.8.3
crc32c 2.4
cryptography 42.0.5
cssselect 1.2.0
cvxpy-base 1.5.1
cvxpy-base-tests 1.5.1
cycler 0.12.1
cysignals 1.11.4
cytoolz 0.12.3
cytoolz-tests 0.12.3
decorator 5.1.1
demes 0.2.3
deprecation 2.1.0
distlib 0.3.8
docutils 0.21.1
duckdb 1.0.0
ewah_bool_utils 1.2.0
ewah_bool_utils-tests 1.2.0
exceptiongroup 1.2.1
executing 2.0.1
faicons 0.2.2
fastparquet 2024.2.0
fiona 1.9.5
fiona-tests 1.9.5
fonttools 4.51.0
fpcast-test 0.1.1
freesasa 2.2.1
frozenlist 1.4.1
fsspec 2024.3.1
fsspec-tests 2024.3.1
future 1.0.0
future-tests 1.0.0
galpy 1.9.2
gdal 3.8.3
gensim 4.3.2
gensim-tests 4.3.2
geopandas 0.14.3
geopandas-tests 0.14.3
geos 3.12.1
gmpy2 2.1.5
gsw 3.6.17
gsw-tests 3.6.17
h5py 3.11.0
h5py-tests 3.11.0
hashlib 1.0.0
html5lib 1.1
htmltools 0.6.0
idna 3.7
igraph 0.11.4
imageio 2.34.1
iniconfig 2.0.0
ipyleaflet 0.19.2
ipython 8.23.0
ipython-tests 8.23.0
ipywidgets 8.1.3
jedi 0.19.1
jedi-tests 0.19.1
Jinja2 3.1.3
joblib 1.4.0
joblib-tests 1.4.0
jsonschema 4.21.1
jsonschema_specifications 2023.12.1
jsonschema_specifications-tests 2023.12.1
jsonschema-tests 4.21.1
jupyter-core 5.7.2
jupyter-leaflet 0.19.2
jupyterlab-widgets 3.0.11
kiwisolver 1.4.5
lakers-python 0.3.0
lazy_loader 0.4
lazy_loader-tests 0.4
lazy-object-proxy 1.10.0
libcst 1.3.1
libcst-tests 1.3.1
libhdf5 1.12.1
libheif 1.12.0
libmagic 5.42
libnetcdf 4.9.2
libsass 0.23.0
lightgbm 4.3.0
linkify-it-py 2.0.3
logbook 1.7.0.post0
lxml 5.2.1
lzma 1.0.0
markdown-it-py 3.0.0
MarkupSafe 2.1.5
matplotlib 3.5.2
matplotlib-inline 0.1.7
matplotlib-pyodide 0.2.2
matplotlib-tests 3.5.2
mdit-py-plugins 0.4.1
mdurl 0.1.2
memory-allocator 0.1.4
micropip 0.6.0
mizani 0.11.4
mmh3 4.1.0
mne 1.7.0
mne-tests 1.7.0
more-itertools 10.2.0
mpmath 1.3.0
mpmath-tests 1.3.0
msgpack 1.0.8
msgspec 0.18.6
msprime 1.3.1
multidict 6.0.5
munch 4.0.0
mypy 1.9.0
mypy-extensions 1.0.0
mypy-tests 1.9.0
narwhals 1.12.1
netcdf4 1.6.5
networkx 3.3
networkx-tests 3.3
newick 1.9.0
nh3 0.2.17
nlopt 2.7.0
nltk 3.8.1
nltk-tests 3.8.1
numcodecs 0.11.0
numcodecs-tests 0.11.0
numpy 1.26.4
numpy-tests 1.26.4
openblas 0.3.26
opencv-python 4.9.0.80
openssl 1.1.1n
optlang 1.8.1
optlang-tests 1.8.1
orjson 3.10.1
packaging 23.2
palmerpenguins 0.1.4
pandas 2.2.0
pandas-tests 2.2.0
parso 0.8.4
pathspec 0.12.1
patsy 0.5.6
patsy-tests 0.5.6
peewee 3.17.3
peewee-tests 3.17.3
Pillow 10.2.0
pillow_heif 0.8.0
pkgconfig 1.5.5
platformdirs 4.2.2
plotly 5.23.0
plotnine 0.0.post1930+g64295e1
pluggy 1.5.0
pplpy 0.8.10
primecountpy 0.1.0
prompt_toolkit 3.0.43
protobuf 4.24.4
pure_eval 0.2.2
py 1.11.0
pyclipper 1.3.0.post5
pycparser 2.22
pycryptodome 3.20.0
pycryptodome-tests 3.20.0
pydantic 2.7.0
pydantic_core 2.18.1
pydecimal 1.0.0
pydoc_data 1.0.0
pyerfa 2.0.1.4
pyerfa-tests 2.0.1.4
pygame-ce 2.4.1
pygame-ce-tests 2.4.1
Pygments 2.17.2
pyheif 0.7.1
pyiceberg 0.6.0
pyinstrument 4.4.0
pynacl 1.5.0
pyodide-http 0.2.1
pyparsing 3.1.2
pypng 0.20220715.0
pyproj 3.6.1
pyrsistent 0.20.0
pysam 0.22.0
pyshp 2.3.1
pytest 8.1.1
pytest-asyncio 0.23.7
pytest-benchmark 4.0.0
python-dateutil 2.9.0.post0
python-flint 0.6.0
python-magic 0.4.27
python-multipart 0.0.9
python-sat 1.8.dev13
python_solvespace 3.0.8
pytz 2024.1
pywavelets 1.6.0
pywavelets-tests 1.6.0
pyxel 1.9.10
pyxirr 0.10.3
pyyaml 6.0.1
qrcode 7.4.2
rebound 3.24.2
reboundx 3.10.1
referencing 0.34.0
referencing-tests 0.34.0
regex 2024.4.16
regex-tests 2024.4.16
requests 2.31.0
retrying 1.3.4
rich 13.7.1
river 0.19.0
river-tests 0.19.0
RobotRaconteur 1.2.0
rpds-py 0.18.0
ruamel.yaml 0.18.6
rust-panic-test 1.0
scikit-image 0.23.2
scikit-image-tests 0.23.2
scikit-learn 1.4.2
scikit-learn-tests 1.4.2
scipy 1.12.0
scipy-tests 1.12.0
screed 1.1.3
screed-tests 1.1.3
seaborn 0.13.2
setuptools 69.5.1
shapely 2.0.2
shapely-tests 2.0.2
sharedlib-test 1.0
sharedlib-test-py 1.0
shiny 1.2.0
shinyswatch 0.8.0
shinywidgets 0.3.4
simplejson 3.19.2
simplejson-tests 3.19.2
sisl 0.14.3
sisl-tests 0.14.3
siuba 0.4.4
six 1.16.0
smart_open 7.0.4
sniffio 1.3.1
sortedcontainers 2.4.0
soupsieve 2.5
sourmash 4.8.8
sparseqr 1.2
sqlalchemy 2.0.29
sqlalchemy-tests 2.0.29
sqlite3 1.0.0
ssl 1.0.0
stack_data 0.6.3
starlette 0.38.1
statsmodels 0.14.2
statsmodels-tests 0.14.2
strictyaml 1.7.3
suitesparse 5.11.0
svgwrite 1.4.3
swiglpk 5.0.10
sympy 1.12
sympy-tests 1.12
tblib 3.0.0
tenacity 8.5.0
termcolor 2.4.0
test 1.0.0
texttable 1.7.0
threadpoolctl 3.4.0
tomli 2.0.1
tomli-w 1.0.0
toolz 0.12.1
toolz-tests 0.12.1
tqdm 4.66.2
traitlets 5.14.3
traitlets-tests 5.14.3
traits 6.4.3
traits-tests 6.4.3
traittypes 0.2.1
tskit 0.5.6
typing-extensions 4.11.0
tzdata 2024.1
uc-micro-py 1.0.3
uncertainties 3.1.7
uncertainties-tests 3.1.7
unyt 3.0.2
unyt-tests 3.0.2
urllib3 2.2.1
wcwidth 0.2.13
webencodings 0.5.1
wordcloud 1.9.3
wrapt 1.16.0
xarray 2024.3.0
xarray-tests 2024.3.0
xgboost 2.1.0.dev0
xlrd 2.0.1
xxhash 3.4.1
xyzservices 2024.4.0
xyzservices-tests 2024.4.0
yarl 1.9.4
yt 4.3.0
zarr 2.16.1
zarr-tests 2.16.1
zengl 2.4.1
zstandard 0.22.0

Testing whether a package is available

The Shinylive distribution includes many packages, but you may want to use one that is not included.

It is possible to install packages using Pyodide’s micropip package. To do that, simply visit the Shinylive examples page and run the following in the Python console:

import micropip
await micropip.install("mypackage")
import mypackage

If that works without errors, then your package is usable in a Shinylive application. (There are some exceptions, where a package will load but not be fully usable in Pyodide.)

The micropip.install command will install the package from PyPI by default. However, you can provide a URL that points directly to your package, like https://example.com/mypackage-1.0-py3-none-any.whl.

Requiring extra packages with requirements.txt

To use extra packages as part of your application, you can add a requirements.txt file to your application, as demonstrated in the extra packages example. The format of the requirements.txt file is similar to a “normal” requirements.txt file. For example, it could look like this:

isodate
attrs==21.4.0

Each time someone runs your Shiny application, their web browser will fetch those packages from PyPI. It will then install the packages to a virtual file system (VFS); when the user closes the page or navigates away from it, the VFS is discarded. If the user goes back and runs the application again, those files can be fetched from the browser cache instead of from PyPI.