Skip to content

Shiny and Shinylive

Understanding Shiny apps

Before introducing our software, which is merely a helper function to produce a Shiny app template from a Python function in a user defined module, let us briefly introduce to this framework. Shiny is a web framework for developing interactive web apps for Python (and is also available in R) using a client-server architecture

sequenceDiagram
    Client (no computation. Sends user input) ->> Server (computes and stores user input): Send parameters through browser
      Server (computes and stores user input)->>Client (no computation. Sends user input): Return results
The client performs no computations and just connects to the client to query a computation which is done on the server and later returned in a formated way to be read in the client. Note that client and server can be, and usually are, the same machine and not remote machines. This is the fastest way to run interactive apps using letitshine. There are services to allow the deployment of these apps directly like shinyapps.io which offer different free and paid plans.

The origional motivation to develop letitshine was the need of sharing custom simple interactive apps with students without the need of setting a remote computing server. Instead, the solution was to set a static website where the client does not compute anything and simply returns the full app to the client which performs the computation in the sandboxed environment of the browser. This can be achieved through the wonderful Shinilive for Python which lets Python apps run directly in the browser.

sequenceDiagram
  Client (computation and user input in browser) ->> Static Server (no computation & no user input stored): Ask for Shinylive app
    Static Server (no computation & no user input stored)->>Client (computation and user input in browser): Return Shinlylive app

Using and installing Shiny

The official documentation of Shiny is the best way to get started and describes the install process. Assuming that you have successfully installed Shiny for Python, let us quickly show how to create your first app. A fresh install of Shiny includes a utitlity to create simple apps:

shiny create --dir firstapp

This will create a directory where the necessary app.pyPython file of the app, the requirements.txt and any other file necessary to run the app will be placed. When you run the above command, a dialog will appear to assist you with its creation.

Let us first show this in practice to create and deploy the simplest app produced by the dialog. The first of this dialogues asks you for which kind of app you want, and we choose the basic app, which is the simplest possible

 Which template would you like to use?: (Use arrow keys)
 » Basic app
   Sidebar layout
   Basic dashboard
   Intermediate dashboard
   Navigating multiple pages/panels
   Custom JavaScript component ...
   Choose from the Shiny Templates website
   [Cancel]

Secondly, we are asked about which framework, Core or Express, we want to use for our app. We chose "Core", which is more familiar with the original R version, according to Shiny website it also has a number of advantages

? Which template would you like to use?: Basic app
? Would you like to use Shiny Express? (Use arrow keys)
   Yes
 » No
    Back
   [Cancel]

The dialog now ends with the following self-explanatory message:

? Which template would you like to use?: Basic app
? Would you like to use Shiny Express? No
 Created Shiny app at simplest

 Next steps:
- Open and edit the app file: simplest/app.py
- Run the app with `shiny run app.py` from the app directory.

 Just getting started with Shiny?
 Learn more at https://shiny.posit.co/py/docs/overview.html

The newly created simplestdirectory has only one file

simplest/
└── app.py

and no requirements.txt which means that there is no need for any external packages. Assuming that you have a working Python environment satisfying requirements.txt (none in this example), you can set up a minimal server to interact with the app runnning shiny run app.py from the app directory and you can connect to the server (usually at http://127.0.0.1:8000/) through your browser:

Shiny example app

Deploying to a static server

Any Shiny app like the one produced before can be converted to a static website ready for deployment. Check at the documentation of Shinylive on how to install it and the basic ideas round it. In our case, in the app directory we would run

$ shinylive export simplest site

to create a directory called site which can be uploaded to any server for hosting and interaction with users. This directory has the following structure

site/
├── app.json
├── edit/
├── index.html
├── shinylive/
└── shinylive-sw.js

and is around 100Mb of size. To preview this file, you cannot simple open it with a browser, but, running a http server. For instance, you can use a minimal server as suggested by the output of the previous command:

...
Run the following to serve the app:
  python3 -m http.server --directory site --bind localhost 8008

and then a browser can be pointed at http://localhost:8008 to preview the site. . Note the reactive character of the app: computations are done whenever a parameter changes and there is no need to press a special compute button.

To deploy the website to be openly accessible to the Internet, simply copy the site/'directory to a directory of a web server that is accessible or check any of the other options described in the documentation. For example, for a web server like www.my_site.com, you can simply copy the directory to the public_html/and then it will be accessible at the address www.my_site.com/site.