Skip to content

Instantly share code, notes, and snippets.

Avatar

Adrien Treuille treuille

View GitHub Profile
@treuille
treuille / colored_deck_gl.py
Created Oct 30, 2019
Answer: Adding color to a DeckGL 'ScatterplotLayer' in Streamlit
View colored_deck_gl.py
import streamlit as st
import pandas as pd
import numpy as np
n_points = 1000
sf_lat, sf_lon = 37.76, -122.4
map_data = pd.DataFrame({
'lat': np.random.randn(n_points) / 50 + sf_lat,
'lon': np.random.randn(n_points) / 50 + sf_lon,
'colorR': np.random.uniform(size=n_points, high=255.0),
@treuille
treuille / selecting_rows.py
Created Oct 30, 2019
Workaround: Selecting rows from a dataframe.
View selecting_rows.py
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 / partial_cache.py
Created Oct 19, 2019
Workaround: Partial Caching
View partial_cache.py
import streamlit as st
import functools
def partial_cache(func=None, **cache_kwargs):
"""Like st.cache, but if the function returns None, then nothing is cached.
Parameters
----------
func : Callable
@treuille
treuille / display_dataframe_quickly.py
Created Oct 19, 2019
Workaround: Displaying a dataframe quickly by slicing it.
View display_dataframe_quickly.py
import streamlit as st
import pandas as pd
import numpy as np
def display_dataframe_quickly(df, max_rows=5000, **st_dataframe_kwargs):
"""Display a subset of a DataFrame or Numpy Array to speed up app renders.
Parameters
----------
df : DataFrame | ndarray
@treuille
treuille / render_svg.py
Last active Dec 18, 2019
Workaround: Displaying SVG images in Streamlit
View render_svg.py
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 / fancy_cache.py
Last active Aug 26, 2020
Workaround: Adding ttl and sesssion uniqueness to @st.cache
View fancy_cache.py
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 / latex.py
Last active Nov 11, 2019
Workaround: Rendering LaTeX in Streamlit
View latex.py
import streamlit as st
from io import BytesIO
import matplotlib.pyplot as plt
def render_latex(formula, fontsize=12, dpi=300):
"""Renders LaTeX formula into Streamlit."""
fig = plt.figure()
text = fig.text(0, 0, '$%s$' % formula, fontsize=fontsize)
fig.savefig(BytesIO(), dpi=dpi) # triggers rendering
@treuille
treuille / powers_in_dash.py
Last active Jul 10, 2020
Comparing Dash and Streamlit
View powers_in_dash.py
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
@treuille
treuille / matplotlib_animation.py
Created Oct 4, 2019
Answer: Animating a Matplotlib chart
View matplotlib_animation.py
import matplotlib.pyplot as plt
import numpy as np
import streamlit as st
import time
fig, ax = plt.subplots()
max_x = 5
max_rand = 10
@treuille
treuille / image_paginator.py
Created Oct 2, 2019
Answer: Applying paginator to images
View image_paginator.py
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.
You can’t perform that action at this time.