This tool automatically generates standardized screenshots of Streamlit elements for use in the API reference documentation.
The screenshot generator consists of:
- A Streamlit app (
capture_app.py
) that displays the Streamlit elements to be captured. - A screenshot capture script (
take_screenshots.py
) that automates browser interactions and captures screenshots. - A configuration file (
config.yaml
) that defines elements and settings. - A preview app (
preview_app.py
) that displays all captured screenshots in a grid.
pip install -r requirements.txt
playwright install
-
Run the screenshot capture script:
python take_screenshots.py
This will start a Streamlit server displaying
capture_app.py
, access it with a headless Playwright browser, take screenshots of all elements defined inconfig.yaml
, and save them to thescreenshots
directory. Note that all existing screenshots in thescreenshots
directory will be deleted. -
Preview all screenshots:
streamlit run preview_app.py
This will start a Streamlit app that displays all screenshots in a grid. The order of the screenshots is the same as in
config.yaml
.
When running the screenshot script (take_screenshots.py
), you can add the following
command line options:
--headed
: Run the browser in headed mode (visible) instead of headless. This is great for debugging.--only element1 element2
: Only capture specific elements.
Example:
python take_screenshots.py --headed --only button text_input
All settings are defined in config.yaml
. It contains:
- Some global settings at the top of the file, such as the dimensions and padding of the screenshots.
- A list of elements to capture. Each element can have the following properties:
name
: The name of the element (required). This must map to thekey
property of anst.container
incapture_app.py
(see below for details).padding
: The padding around the element. If not given, thedefault_padding
setting will be used. You can use this to make smaller elements (e.g. buttons) not take up the entire screenshot.overlay
: The path to an overlay image to apply on top of the screenshot. Supports SVG and PNG files. The overlay image must be the same size as the screenshot. The overlay is only applied if the global settingenable_overlays
istrue
.
All elements are displayed through the Streamlit app capture_app.py
. For every element,
this app has an st.container
with a key
parameter equal to the element name defined
in config.yaml
. The content of this container will be screenshotted.
To edit an existing element:
- Simply edit whatever is in the associated
st.container
incapture_app.py
. You can also add supporting code that should not be captured outside of the container (e.g. CSS hacks).
To add a new element:
- Add the new element to
config.yaml
. - Add a new
st.container
tocapture_app.py
with akey
parameter equal to the element name.
- The script launches a Streamlit server running
capture_app.py
- It uses Playwright to automate a browser session
- For each element defined in
config.yaml
:- It locates the
st.container
with itskey
equal to the element name on the page - Performs any special handling (e.g., clicking dropdowns)
- Takes a screenshot
- Processes the image (trims whitespace, applies padding, adds overlays)
- It locates the
- All screenshots are saved to the
screenshots
directory
Some elements are handled specially by take_screenshots.py
. E.g. selectbox,
multiselect, date input, and color picker are clicked to show their dropdowns. Or data
editor is clicked multiple times to show its editing mode. For details, see the
take_screenshots.py
code.
- If elements aren't found, check the key attributes in
capture_app.py
- For rendering issues, try running in headed mode with
--headed
- If overlays don't appear, check that
enable_overlays
istrue
and paths are correct