2  Shiny for Python

Published

2026-05-08

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 pap

2.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:

%%{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
    

Python built-in virtual environments

Python’s built-in virtual environment tool (venv) ships with Python. The general workflow is:

  1. Create virtual environment (python -m venv)
  2. Use the environment (activate)
  3. 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 virtual environments

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 | sh

After 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 venv

If you don’t want to install/use uv, you can use Python’s built-in venv:

python -m venv .venv

Activate the virtual environment:

source .venv/bin/activate

The 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 shiny

  1. Python environments are a similar concept to the renv package in R.↩︎

  2. Install uv from the Terminal with: curl -LsSf https://astral.sh/uv/install.sh | sh↩︎