Skip to content

Instantly share code, notes, and snippets.

View treuille's full-sized avatar

Adrien Treuille treuille

View GitHub Profile
@treuille
treuille / pagination.py
Created October 2, 2019 19:05
Answer: A simple paginator in Streamlit
import streamlit as st
import numpy as np
def paginator(label, items, items_per_page=10, on_sidebar=True):
"""Lets the user paginate a set of items.
Parameters
----------
label : str
The label to display over the pagination widget.
@treuille
treuille / render_svg.py
Last active September 28, 2023 14:13
Workaround: Displaying SVG images in Streamlit
import streamlit as st
import base64
import textwrap
def render_svg(svg):
"""Renders the given svg string."""
b64 = base64.b64encode(svg.encode('utf-8')).decode("utf-8")
html = r'<img src="data:image/svg+xml;base64,%s"/>' % b64
st.write(html, unsafe_allow_html=True)
@treuille
treuille / confirm_button_hack.py
Last active August 25, 2023 14:38
Hack to implement a confirm_button in Streamlit v0.35
"""
THIS HAS BEEN DEPRECATED!
We propose you use st.form() instead:
https://blog.streamlit.io/introducing-submit-button-and-forms/
"""
import streamlit as st
import collections
@treuille
treuille / cache_example.py
Created September 30, 2019 18:52
This demonstrates the st.cache function
import streamlit as st
import pandas as pd
# Reuse this data across runs!
read_and_cache_csv = st.cache(pd.read_csv)
BUCKET = "https://streamlit-self-driving.s3-us-west-2.amazonaws.com/"
data = read_and_cache_csv(BUCKET + "labels.csv.gz", nrows=1000)
desired_label = st.selectbox('Filter to:', ['car', 'truck'])
st.write(data[data.label == desired_label])
@treuille
treuille / caching_DAG_example.py
Last active May 30, 2023 17:18
This demonstrates how piping cached functions into one another automatically sets up an efficient directed acyclic computational graph.
import streamlit as st
import pandas as pd
@st.cache
def load_metadata():
DATA_URL = "https://streamlit-self-driving.s3-us-west-2.amazonaws.com/labels.csv.gz"
return pd.read_csv(DATA_URL, nrows=1000)
@st.cache
def create_summary(metadata, summary_type):
@treuille
treuille / selecting_rows.py
Created October 30, 2019 22:18
Workaround: Selecting rows from a dataframe.
import streamlit as st
import pandas as pd
# Load some example data.
DATA_URL = \
"http://s3-us-west-2.amazonaws.com/streamlit-demo-data/uber-raw-data-sep14.csv.gz"
data = st.cache(pd.read_csv)(DATA_URL, nrows=1000)
# Select some rows using st.multiselect. This will break down when you have >1000 rows.
st.write('### Full Dataset', data)
@treuille
treuille / streamlit_app.py
Created November 22, 2022 02:46
Example of how to access widget state in a callback.
import streamlit as st
with st.echo():
def on_change():
st.write(f"my_selection **(1)**: `{st.session_state.my_selection}`")
my_selection = st.selectbox(
"Select something", ["a", "b"], on_change=on_change, key="my_selection"
)
st.write(f"my_selection **(2)**: `{st.session_state.my_selection}`")
@treuille
treuille / image_paginator.py
Created October 2, 2019 19:44
Answer: Applying paginator to images
import streamlit as st
import itertools
def paginator(label, items, items_per_page=10, on_sidebar=True):
"""Lets the user paginate a set of items.
Parameters
----------
label : str
The label to display over the pagination widget.
@treuille
treuille / fancy_cache.py
Last active June 5, 2021 16:15
Workaround: Adding ttl and sesssion uniqueness to @st.cache
import streamlit as st
import streamlit.ReportThread as ReportThread
from streamlit.server.Server import Server
import time
import functools
import random
import string
# Copied from tvst's great gist:
@treuille
treuille / spacy_example.py
Created September 21, 2019 21:52
Provisional solution to @ines's Streamlit caching problem with Spacy
import streamlit as st
import spacy
from spacy import displacy
SPACY_MODEL_NAMES = ["en_core_web_sm", "de_core_news_sm"]
DEFAULT_TEXT = "Mark Zuckerberg is the CEO of Facebook."
HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem">{}</div>"""
@st.cache(ignore_hash=True)