%%{init: {'theme': 'neutral', 'themeVariables': { 'fontFamily': 'monospace', "fontSize":"14px"}}}%%
flowchart TB
SystemPython[("System Python<br/><code>/usr/bin/python3</code>")]
subgraph VenvFlow["venv (standard library)"]
direction TB
VenvCmd["<code>python -m venv .venv</code>"]
VenvDir[".venv/<br/>├── bin/python (symlink)<br/>├── bin/activate<br/>└── lib/site-packages/"]
VenvPip["<code>pip install shiny</code>"]
VenvPkgs["Packages downloaded<br/>from PyPI"]
VenvCmd --> VenvDir
VenvDir --> VenvPip
VenvPip --> VenvPkgs
VenvPkgs --> VenvDir
end
ProjectA["Project A<br/>needs shiny 1.5.5"]
VenvDir -.->|"isolates"| ProjectA
SystemPython --> VenvCmd
style VenvDir text-align:left
classDef system fill:#2d3748,stroke:#48bb78,color:#fff
classDef venvStyle fill:#2c5282,stroke:#63b3ed,color:#fff
classDef project fill:#553c9a,stroke:#b794f4,color:#fff
class SystemPython system
class VenvCmd,VenvDir,VenvPip,VenvPkgs venvStyle
class ProjectA project
2 Shiny for Python
This chapter introduces Shiny for Python using the Core API. We’ll cover the App, ui, server, and render components to build the example application we’ll use throughout the rest of the book.
2.1 Create app directory
We’ll create and move into a new directory (pap, for Python app-package),
mkdir pap
cd pap2.2 Virtual environments
In Python, it’s a good idea to use a virtual environment. This practice might be new to R developers, so I’ll give a quick overview.1
2.2.1 How virtual environments work
Python virtual environments isolate a project’s dependencies, so any packages installed for one project don’t conflict with other projects. Two common tools used in this approach are illustrated below:
Python’s built-in virtual environment tool (venv) ships with Python. The general workflow is:
- Create virtual environment (
python -m venv)
- Use the environment (
activate)
- Install dependencies (
pip install)
venv doesn’t have a built-in lockfile, which also means we’ll need to also run pip freeze to store the dependencies in requirements.txt.
uv is similar, but has a few more benefits:
%%{init: {'theme': 'neutral', 'themeVariables': { 'fontFamily': 'monospace', "fontSize":"14px"}}}%%
flowchart TB
SystemPython[("System Python<br/><code>/usr/bin/python3</code>")]
subgraph UvFlow["uv (Rust-based, fast)"]
direction TB
UvCmd["<code>uv venv</code>"]
UvDir[".venv/<br/>├── bin/python<br/>└── lib/site-packages/"]
UvAdd["<code>uv add shiny</code>"]
UvCache[("Global cache<br/><code>~/.cache/uv/</code>")]
UvLock["<code>uv.lock</code><br/>(reproducible)"]
UvCmd --> UvDir
UvAdd --> UvCache
UvCache -->|"hardlink<br/>(near-instant)"| UvDir
UvAdd --> UvLock
end
ProjectB["Project B<br/>needs shiny 1.6.1"]
SystemPython --> UvCmd
UvDir -.->|"isolates"| ProjectB
style UvDir text-align:left
classDef system fill:#2d3748,stroke:#48bb78,color:#fff
classDef uvStyle fill:#744210,stroke:#f6ad55,color:#fff
classDef project fill:#553c9a,stroke:#b794f4,color:#fff
class SystemPython system
class UvCmd,UvDir,UvAdd,UvCache,UvLock uvStyle
class ProjectB project
uv requires a separate install command, 2 but also includes a global cache with hardlinks. During installation, uv reuses downloaded dependencies across projects, which is typically faster than copying/downloading them each time (as venv does). uv add also installs new dependencies in one step, and the uv.lock file automatically tracks exact versions.
Both tools create a .venv/ folder with an isolated Python interpreter and its own site-packages/ directory. When activated, the shell points to that Python instead of the System Python.— So, pip install shiny only installs shiny in this project.
2.2.2 Virtual environment setup
Install uv with:
macOS/Linux:
curl -LsSf https://astral.sh/uv/install.sh | shAfter installation, restart your terminal (or run source ~/.local/bin/env on macOS/Linux) so the uv binary is on your PATH.
Windows (PowerShell)
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.sh | iex"Create a virtual environment, defaults to .venv:
uv venvIf you don’t want to install/use uv, you can use Python’s built-in venv:
python -m venv .venvActivate the virtual environment:
source .venv/bin/activateThe terminal prompt will prepend with (pap).
Windows (PowerShell):
.venv\Scripts\Activate.ps1 2.2.3 Installing dependencies in virtual environments
Now we can install Shiny into the venv:
uv pip install shinyPython environments are a similar concept to the
renvpackage in R.↩︎Install
uvfrom the Terminal with:curl -LsSf https://astral.sh/uv/install.sh | sh↩︎