Skip to content

Instantly share code, notes, and snippets.

View asehmi's full-sized avatar

Arvindra Sehmi asehmi

View GitHub Profile
@asehmi
asehmi / mplfinance_streamlit_demo.py
Last active January 12, 2024 03:06
How to use mplfinance in Streamlit
from datetime import date, datetime
import streamlit as st
import pandas as pd
import mplfinance as mpf
from pandas_datareader import data as pdr
@st.experimental_memo(persist='disk')
def get_historical_data(symbol, start_date = None):
df = pdr.get_data_yahoo(symbol, start=start_date, end=datetime.now())
for col in df.columns:
@asehmi
asehmi / multifile_uploader.py
Created November 21, 2021 22:43
Uploading and saving multiple files in Streamlit
### **data.py**
import streamlit as st
import pandas as pd
@st.experimental_memo(persist='disk')
def csv_to_df(excel_file):
df = pd.read_csv(excel_file)
return df
@asehmi
asehmi / pandas_profiler.py
Created November 29, 2021 00:50
Using pandas profiling with streamlit_pandas_profiling component in Streamlit
import streamlit as st
import pandas_profiling
from streamlit_pandas_profiling import st_profile_report
import pandas as pd
st.set_page_config(
page_title='Data Profiler',
layout='wide',
page_icon='🔍'
)
@asehmi
asehmi / streamlit_debug.py
Last active February 20, 2024 02:31
Handy script I made to help streamline Streamlit debugging
# How to use:
#
# [1] Ensure you have `debugpy` installed:
#
# > pip install debugpy
#
# [2] In your main streamlit app:
#
# import streamlit_debug
# streamlit_debug.set(flag=True, wait_for_client=True, host='localhost', port=8765)
@asehmi
asehmi / streamlit_remove_top_of_page_whitespace.py
Last active April 29, 2022 13:19
It is possible to get rid of that expanse of whitespace at the top of Streamlit apps
#
# It's a bit of a CSS hack but it is possible to get rid of that
# large amount of whitespace at the top of Streamlit apps!
#
# If this hack stops working, it's likely the Streamlit app CSS
# classes have changed. Open up dev tools and find the new names
# to update the CSS below. :-)
#
# UPDATED 29/4/2022 with new classes
#
@asehmi
asehmi / multiform_chaining.py
Created December 14, 2021 15:18
Chaining Streamlit forms and passing values using session state API
import streamlit as st
from streamlit_pandas_profiling import st_profile_report
from streamlit import session_state as session
import numpy as np
import pandas as pd
from pandas_profiling import ProfileReport
if 'data_uploader_submitted' not in session:
session.data_uploader_submitted = False
# ISSUE: Stylistically, I prefer to have a top-level controller and then
# form "views" which get laid out on the page in the order they were called.
# With just two forms, though, callback chaining is manageable and won’t result in a tangle,
# but the layout order is inverted, and in this callbacks solution the profile report and forms
# disappear when `cluster_duplicates` is finally called.
#
# (See this thread: https://discuss.streamlit.io/t/selectbox-bug-on-chaining-forms/19927)
#
# For a better solution see: multiform_chaining.py
@asehmi
asehmi / multipage_session_state.py
Created December 14, 2021 15:30
Maintaining widget state across a multi-page Streamlit app
# Since widgets are created anew on each page their values will be initialized to their default values.
# I’ve created a separate ‘value cache’ to store values between pages.
# (Note, when a widget has a key and you initialize its value with a session state key value other than the widget’s own key value,
# Streamlit will complain. Therefore, the widgets don't have a key.)
import streamlit as st
# Instantiate the Session State Variables
if 'cache' not in st.session_state:
st.session_state.cache = {'name': '', 'age': '', 'gender': ''}
@asehmi
asehmi / clear_container.py
Created December 14, 2021 15:39
Showing/hiding Streamlit panel containers
import streamlit as st
import numpy as np
c1, c2 = st.columns([1,1])
show_p1 = c1.checkbox('Show panel 1', True)
show_p2 = c2.checkbox('Show panel 2', True)
panel1 = c1.empty()
panel2 = c2.empty()
@asehmi
asehmi / df_flexi_query.py
Last active February 13, 2024 16:49
Flexible data filtering UI with declarative query builder [Streamlit, Pandas]
import streamlit as st
import pandas as pd
st.header('Flexible Data Filtering UI')
data = [
{
'name':'name1',
'nickname':'nickname1',
'date':2010,