Skip to main content

A collection of custom components and helper functions for Streamlit that are focused on providing advanced styling functionality.

Project description

st_stylish

A collection of custom components and helper functions for Streamlit that are focused on providing advanced styling functionality.

The primary functionality in this repo is in the /st_stylish/helpers.py module. This module provides functions that inject css via the st.markdown hack that target elements in a more reliable way than the typical generated ids approach. Readable class names that should change much less frequently, DOM structure, and tag attribute names are used to target elements.

Setup

Local

  1. Create a virtual environment
  2. Activate your virtual environment
  3. pip install -r requirements.txt

Helper functions

Use cases

  1. Full page component Note: Make sure to set layout='wide' with st.set_page_config(layout='wide')

Examples

Full page component

  1. Single page - full page iframe /examples/singlepage.py

    Run it: streamlit run ./examples/singlepage.py

  2. Multi page - full page iframe /examples/multipage/__init__.py

    Run it: streamlit run ./examples/multipage/home.py

  3. Full page component using convenience function /examples/fullpage_component.py

    Run it: streamlit run ./examples/fullpage_component.py

  4. Using the StyleInjector context manager /examples/with_StyleInjector.py

    Run it: streamlit run ./examples/with_StyleInjector.py

Custom components

  1. iframe The only added functionality here is the loading indicator while the iframe content loads.

    To run this custom component's frontend locally:

    cd st_stylish/iframe/frontend
    npm install
    npm start
    

    Then you can use it in a streamlit file. From project root: streamlit run examples/stylish_iframe.py

Using the StyleInjector

If you want to style multiple components (i.e. .element-containers) you can use the StyleInjector class as a context manager like so:

with StyleInjector() as injector:
  injector.set_appview_padding(0)

The StyleInjector will keep track of the components.markdown calls. When you call the StyleInjector's set_n_component_height_width the index of the streamlit component will be offset by the number of previously injected markdown components.

So to style the first streamlit component that you call in the script, call set_n_component_height_width with an index argument of 0.

See /examples/with_StyleInjector.py for example code.

Enhancements

  1. StylesManager class (Done) I want to create a StylesManager class that controls the injection of <style> tags via st.markdown and keeps an index of .element-containers so we can target specific .element-containers to style independently. Right now the set_component_height_width function just targets the last streamlit element. This works great for a full page iframe, for example, but you then cannot add any more components below that iframe.

TODO

  • convenience function for a full page component
  • add docstrings to all helper functions
  • implement StylesManager concept
  • deploy to PyPi

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

st_stylish-1.0.0.tar.gz (42.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

st_stylish-1.0.0-py3-none-any.whl (30.5 kB view details)

Uploaded Python 3

File details

Details for the file st_stylish-1.0.0.tar.gz.

File metadata

  • Download URL: st_stylish-1.0.0.tar.gz
  • Upload date:
  • Size: 42.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for st_stylish-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bae663a97df0958a9857e61972c3977769d82b4388c44c928a5b2335ba9a9669
MD5 b90e796a922d77a6531c5fc69c509da2
BLAKE2b-256 bb43ff4058db734f4e22b4980534b7925af7c99ea5ac65f5b858aecc57255994

See more details on using hashes here.

File details

Details for the file st_stylish-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: st_stylish-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 30.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for st_stylish-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e83d8ef9793adbc558d24bec6e0eea1ea753e01285ea06d5e6e9fcff3282ba12
MD5 9aa56b7322ffcd64821b80708a21bb79
BLAKE2b-256 c8b2d264606bf5bda92d0dc9a2968c4f46a36f4ae56ce1475a7073d3312c2dae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page