Skip to content

Instantly share code, notes, and snippets.

View tvst's full-sized avatar
🦙

Thiago Teixeira tvst

🦙
View GitHub Profile
@tvst
tvst / SessionState.py
Last active April 14, 2024 20:24
DO NOT USE!!! Try st.session_state instead.
"""Hack to add per-session state to Streamlit.
Usage
-----
>>> import SessionState
>>>
>>> session_state = SessionState.get(user_name='', favorite_color='black')
>>> session_state.user_name
''
# ---
# From https://gist.github.com/tvst/036da038ab3e999a64497f42de966a92
import streamlit.ReportThread as ReportThread
from streamlit.server.Server import Server
class SessionState(object):
def __init__(self, **kwargs):
"""A new SessionState object.
import streamlit as st
import pandas as pd
@st.cache
def get_data():
return pd.read_csv('https://datahub.io/core/gdp/r/gdp.csv')
'# World GDP'
import os
import signal
import subprocess
import sys
# Parse command-line arguments.
if len(sys.argv) > 1:
folder = os.path.abspath(sys.argv[1])
else:
folder = os.path.abspath(os.getcwd())
import streamlit as st
import os
import sys
import importlib.util
# Parse command-line arguments.
if len(sys.argv) > 1:
folder = os.path.abspath(sys.argv[1])
else:
folder = os.path.abspath(os.getcwd())
@tvst
tvst / session_id.py
Last active May 18, 2023 12:05
Hack to get a session-specific ID in Streamlit. See https://discuss.streamlit.io/t/session-specific-caching/271
import streamlit.ReportThread as ReportThread
from streamlit.server.Server import Server
def get_session_id():
# Hack to get the session object from Streamlit.
ctx = ReportThread.get_report_ctx()
this_session = None
@tvst
tvst / thread_safe_st.py
Created October 8, 2019 03:38
Hack to write to Streamlit app from another thread. See: https://discuss.streamlit.io/t/live-plot-from-a-thread/247
import streamlit.ReportThread as ReportThread
from streamlit.server.Server import Server
class ThreadSafeSt():
def __init__(self):
"""Object that write to a Streamlit app outside the main thread.
This object must be created from the main thread, and then passed
around to other threads so they can write to the Streamlit app.
@tvst
tvst / opt_echo.py
Created October 10, 2019 05:54
Hack to make st.echo show a checkbox to toggle code display. See https://discuss.streamlit.io/t/show-code-optional-wrapper/329
import streamlit as st
import traceback
class opt_echo:
"""Replacement for st.echo that includes a checkbox saying "show code".
Usage
-----
>>> with opt_echo():
@tvst
tvst / gmail_inbox_bookmarklet
Last active October 11, 2019 04:41
Make Gmail more Inbox-like (RIP)
javascript:(() => {
/* Autohide header */
const header = document.querySelector('.nH.w-asV.aiw');
hide = () => {
header.style.transition = 'opacity 300ms 300ms, transform 300ms 300ms';
header.style.opacity = 0;
header.style.transform = 'translateY(-50px)';
@tvst
tvst / github_projects_bookmarklet
Created October 11, 2019 04:58
Make Github Projects a little easier to read
javascript:(() => {
const css = document.createElement('style');
css.innerText = `
/* Change background */
.js-project-header {
background: #23527b!important;
}
.project-columns {
background: #23527b!important;