Shiny and Shinylive
Before introducing our software, which is merely a helper function to produce an app template from a Python function in a user defined module, let us briefly present a quick introduction to this framework. The installation of the shinylive environment for Python requires the shinylive Python package to be installed Shinilive for Python. Through this tutorial we will use version Python versions above 3. The starting point of this tutorial is that the user already has a working function within a module that produces the desired output from a set of input arguments. As in any project, it is advisable to use a specific enviroment for each project to avoid [REF], so we assume that the user has a dedicated Python environment where the function and module van be correctly run and loaded.
The latest version of Shinylive can be obtained through
pip install shinylive
which will install the required packages.
The shinylive command includes a series of handy functions to speed up the process of creating an app. Running
shiny create --dir firstapp
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
You may need to install packages with: `pip install -r requirements.txt`
The newly created simplestdirectory has only one file
simplest/
└── app.py
and no requirements.txtwhich means that there is no need for any external packages. In the next sections, we will see that this produces a very useful file for reproducibility of the workflow. The app can now be converted to a static website ready for deployment running
$ shinylive export simplest site
and will 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, 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 (see Figure 2). Simply moving the slider produces the output and a simple "Hello Shiny!" text. 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. 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.