With the advent of WASM (Web Assembly), it is possible to run Python in the browser. We can write logic for interactive apps using Python instead of JavaScript.
Shiny is the most popular interactive web app framework for R (another popular data science language) and recently became available for Python.
To learn more about how it compares to other popular options, check out this 16 min video on YouTube.
If you used VS Code to clone your repository/repo, it may still be open to this repo folder. If not, open VS Code and open the folder containing your repo.
In VS Code, use Terminal / New Terminal to open a terminal window. Use PowerShell on Windows, Terminal on Mac/Linux. Type the following command in the terminal and hit Enter to run it.
python --version
If that does not work, try python3 --version
or py --version
.
Use the command that works instead of python
in the following instructions.
đ Rocket Tip: Modify this README.md file to reflect the commands that work on your machine.
Install some additional content into your global Python for best results.
Open a terminal (PowerShell on Windows, Terminal on Mac/Linux) and run the two commands below to install these to the default Python.
Wait for each command to finish before running the next one.
We need the current rsconnect-python
to deploy our app to shinyapps.io.
python -m pip install --upgrade pip wheel
python -m pip install --upgrade git+https://github.com/rstudio/rsconnect-python.git
đ© You must have reconnect-python
installed before continuing.
Shiny offers a free service for hosting Shiny apps. Itâs pretty easy to use. Thereâs this one-time setup to authorize your machine to use it. Itâs not bad - just follow these instructions.
Weâll set up a local Python environment with the packages we need for this project. We create a local virtual environment, activate it, and install the packages. Hereâs the short version for PowerShell. Run one command at at time and wait for each to finish before running the next one.
python.venv\Scripts\activate -m venv .venv
.venv\Scripts\activate
python -m pip install --upgrade pip wheel
python -m pip install --upgrade -r requirements.txt
đ Rocket Tip: Read the instructions below for more details.
The command python -m venv .venv
creates a virtual environment in a folder named .venv.
If it works correctly, you will see a new folder named .venv.
A leading dot is typically used for items hidden by default.
As an analyst and dashboard developer, be sure you can see hidden folders and files on your machine.
When VS Code asks if it should create the virtual environment, click yes.
đ Rocket Tip: Always read VS Code suggestions, they are often very good. Consider using them.
Activate the virtual environment just created. After activating it, notice how the prompt changes to show the active virtual environment (.venv).
.venv\Scripts\activate
source .venv/bin/
Install the additional free code we need into the active virtual environment. Run on command at a time, waiting for each to finish before running the next one.
python -m pip install --upgrade pip wheel
python -m pip install --upgrade -r requirements.txt
It may take a while. Donât worry too much, these are VERY common activities. Weâll use them often, and after several projects, theyâll become routine.
With your virtual environment activated, run the app with the following command:
shiny run --reload app.py
Open the app by following the instructions provided in the terminal. For example, try CTRL CLICK (hit both at the same time) on the URL displayed (http://127.0.0.1:8000).
Hit CTRL c (at the same time) to quit the app. If it wonât stop, close the terminal window. Reopen the terminal window and be sure the virtual environment is activated before running the app again.
Congratulations! Youâve created your first Shiny app in Python.
When youâre ready, see SHINYAPPS.md to deploy it to shinyapps.io so others can use it.
đ Rocket Tips:
In the VS Code Terminal, to get the last command you ran, hit the up arrow key.
In the VS Code Terminal, hit the right arrow key to accept a suggested command, then ENTER to run it.
If you see ModuleNotFoundError: No module named âshinyswatchâ or similar, verify youâve created your .venv folder and it is currently activated (it will appear in the terminal prompt).