Shiny for Python Shinylive: How to Run Shiny for Python Apps Without a Python Server

This article was first published on Appsilon | Enterprise R Shiny Dashboards , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

Writing Shiny for Python applications is fun and games, but what about deploying them? Well, it’s the same story. Shiny for Python Shinylive saves the day by offering a beginner-friendly ways (plural) to share Shiny apps. Today you’ll learn a bunch of them. By the end, you’ll have your app running on Shinylive through the editor, GitHub, and also deployed locally.

Keep in mind that Shinylive is still an experimental feature, and some features might change in the future. We recommend following the official Shinylive documentation to stay up to date.

We recently launched a Shiny for Python framework for production-ready applications, Tapyr. Learn more about Tapyr and how you can set it up in this blog post.

Table of contents:


Introduction to Shiny for Python Shinylive

So, what exactly is Shinylive? It’s an experimental feature that allows your Shiny for Python applications to run in a web browser. This means you don’t need a separate server for running Python.

With Shinylive, Python and Shiny run in the web browser, which stands for both the client and the server of the application.

This was all made possible because of two technologies:

  • WebAssembly – This is a binary format for compiled programs that runs in a web browser at near-native speeds.
  • Pyodide – A port of Python and many packages compiled into WebAssembly.

The main advantage of a Shinylive deployment is that the computations are done on the client. In many cases, that’s beneficial to you as a Shiny developer, since the client browser typically has more power than the server. That’s especially the case when you scale the app to dozens or hundreds of concurrent users.

On the other side, you can also consider this to be a disadvantage. For example, sometimes you need to perform intensive computations that require a powerful server. In this case, it’s not the best idea to shift the computations to the client.

We’ll explore some additional advantages and disadvantages below.

Pros of Using Shinylive

Here’s a list of clear, no-brainer advantages that come with Shinylive deployment:

  • No installation: You don’t need to install Python or Shiny on a computer.
  • Super easy sharing: Share your applications by generating a shareable URL.
  • Simple deployment: You can use Shinylive to deploy your apps to any static web hosting service (more on this at the end of the article).
  • Scalability: Apps are essentially just static files, so it’s easy to scale them.
  • Bullet-proof security: The app isn’t running on a server, and hence eliminates almost all security risks.

Cons of Using Shinylive

There’s a disadvantage for every advantage, and Shinylive is no exception. Let’s go over a couple:

  • Python package limitation: Not all Python packages are available on Pyodide. Here’s a list of currently available ones.
  • Download size: Packages required to run Shiny apps via Python and Pyodide take around 13MB of data. Other packages also have a tool (Numpy = 7.5MB, Pandas = 13MB, Matplolib = 11.5MB). Keep in mind that this data will be cached, so only the first run will be slower.
  • No secrets: Application data and code are sent to the browser, which means the user can inspect them with ease.

You now know some advantages and drawbacks of using Shinylive. Up next, we’ll get practical!

Shiny for Python works wonders with databases – Here’s how to get started with MySQL and Postgres.

How to Use Shinylive Editor for Sharing Shiny for Python Apps

This section will walk you through using and sharing your Shiny for Python apps via the Shinylive editor.

Writing the Shiny for Python Application

First things first – we need an app. We’ll leverage the one you’ve seen in our previous Shiny for Python articles. It essentially plots a histogram of values drawn from a normal distribution. The user can change the number of bins, and the chart updates automatically.

Here’s the code for the entire application:

from shiny import ui, render, App
import numpy as np
import matplotlib.pyplot as plt


app_ui = ui.page_fluid(
    # Sidebar layout
    ui.layout_sidebar( 
        # Sidebar with the input controls
        ui.panel_sidebar(
            ui.h2("Page Charts"),
            ui.hr(),
            ui.input_slider(id="slider", label="Number of bins:", min=5, max=25, value=15)
        ),
        # Main area with the chart
        ui.panel_main(
            ui.output_plot(id="histogram")
        )
    )
)

def server(input, output, session):
    @output
    @render.plot
    def histogram():
        # 500 samples from a normal distribution centered around 100
        x = 100 + np.random.randn(500)
        plt.title("A histogram", size=20)
        plt.hist(x=x, bins=input.slider(), color="gray", ec="black")



app = App(ui=app_ui, server=server)

You can paste this code into the Shinylive editor. Simply replace the already existing code and hit the run button.

This is what you’ll see on the right-hand side:

Image 1 - Shiny for Python editor (1)

Image 1 – Shiny for Python editor (1)

As promised, you can change the number of bins by playing around with the slider in the sidebar:

Image 2 - Shiny for Python editor (2)

Image 2 – Shiny for Python editor (2)

And that’s our app. Nothing fancy, but it’ll do for the rest of the article.

How to Share Shiny for Python Apps

Have you noticed the “Share” button in the top right corner of the Shinylive editor? If not, here’s what it looks like:

Image 3 - Creating a shareable link

Image 3 – Creating a shareable link

Clicking on it will open up a modal window, similar to this one:

Image 4 - Editor and application URLs

Image 4 – Editor and application URLs

This is where all the magic happens. You can share the Editor URL, meaning the user will have access to the code and will be able to change it, or you can share the Application URL, meaning the user will only see the rendered application.

We recommend you click on the “Hide header” checkbox when sharing the application URL, so that the blue Shiny for Python header isn’t shown to the user.

You can copy either of these URLs and open them up in a new browser window. Here’s what you’ll get for the Application URL:

Image 5 - Shared Shiny for Python application

Image 5 – Shared Shiny for Python application

Impressive, isn’t it? Keep in mind that these URLs differ only in one thing – the first one has the /editor/ part, while the latter has /app/. You can guess where each one leads to.

Up next, let’s explore the option of Shinylive deployment from GitHub.

How to Share Shiny for Python Applications with Shinylive and GitHub

The answer is simple – GitHub gists. We’ll now walk you through the process of creating one and using it in Shinylive.

The main advantage of using Gists is that you can change the code at any time, but your sharing URL will stay the same.

Creating a GitHub Gist

If you have a GitHub account, you can create a new Gist by clicking on the plus button in the top right corner. Here’s how yours should look like once created:

Image 6 - Shiny for Python app GitHub gist

Image 6 – Shiny for Python app GitHub gist

The only thing we’ve changed is the histogram bar color – from gray to red. It’s a non-essential change but will prove to us the GitHub gist connection is actually working.

Sharing Shiny for Python App Through a GitHub Gist

Take a look at the gist URL. You only need the ID part, or the part after your username:

Image 7 - GitHub gist URL

Image 7 – GitHub gist URL

You can copy this ID part and paste it at the end of the following URL:

https://shinylive.io/py/app/#gist=

In our case, the complete URL would be:

https://shinylive.io/py/app/#gist=6316216a6150963a1553d55cee93b758

You can now open this URL to verify the connection works:

Image 8 - Shared Shiny for Python application (2)

Image 8 – Shared Shiny for Python application (2)

And it does – we now have a Shiny for Python application with red histogram bars.

Once again, the main advantage of using gists with Shinylive is that you can update the code and reuse the Shinylive application URL. Feel free to test it out.

Deploy Shiny for Python Apps Anywhere – Experimental

Shinylive also allows you to deploy your Shiny for Python applications to static web hosts. This feature is currently experimental, so the details might change in the future. Refer to the official documentation if you have any trouble running the code.

This deployment option will essentially allow you to take your app and host it on services such as Netlify or GitHub pages. There’s also a Posit Connect option, but these services require an entire article to cover in detail.

Configuring a Shinylive Local Distribution

The first step is to install the shinylive Python package. You can do so by running the following command from the Terminal:

pip install shinylive

This is the output you will see:

Image 9 - Installing Shinylive Python package

Image 9 – Installing Shinylive Python package

Once installed, you can create a new Shiny for Python application with the shiny create command. We’ll name ours histogram_app

shiny create histogram_app

You should see the following output:

Image 10 - Creating a Shiny for Python app

Image 10 – Creating a Shiny for Python app

And finally, you can now use shinylive to export the Shiny for Python application:

shinylive export histogram_app site

This will create a new site directory with the static app contents:

Image 11 - Exporting Shiny for Python app for static serving

Image 11 – Exporting Shiny for Python app for static serving

We have the app exported now, so the only thing left to do is to run it via the web server.

Serving the Local Application

Provided your exported Shiny for Python application is stored in a site folder, you can run it by executing the following shell command:

python -m http.server --directory site 8008

If everything went well, you’ll see a message that the app is running locally on port 8008:

Image 12 - Serving the Shiny for Python app

Image 12 – Serving the Shiny for Python app

You can now open localhost:8008 and see your application running locally:

Image 13 - Served local Shiny for Python app

Image 13 – Served local Shiny for Python app

You can take things a step further by opening a local Shinylive editor. To do so, just visit localhost:8008/edit, and you’ll see a familiar-looking window:

Image 14 - Local Shinylive editor

Image 14 – Local Shinylive editor

And that’s the basic of Shinylive! Let’s make a brief recap next.


Summing up Shiny for Python Shinylive

Shinylive makes sharing Shiny for Python apps easier than ever. You can do everything from the online editor, or opt for GitHub gists for more control. Also, you can serve the app locally or on any static site hosting service, such as GitHub pages.

And that’s exactly what we’ll cover next – taking local Shiny for Python applications online through GitHub pages, Netlify, Cloudflare, and Posit Connect. Make sure to stay tuned to the Appsilon blog to learn more.

What are your thoughts on Shinylive? Does it simplify your workflow for sharing Shiny for Python applications?  Let us know in our Shiny community.

Is Shiny for Python better than R Shiny? We did the comparison, you do the reading.

The post appeared first on appsilon.com/blog/.

To leave a comment for the author, please follow the link and comment on their blog: Appsilon | Enterprise R Shiny Dashboards .

Want to share your content on python-bloggers? click here.