It is quite possible that I am misunderstanding how Streamlit works, but I still want to ask.
Summary
For the sake of my sanity while working on my (somewhat big) Streamlit app, I want to use a separate python file that contains functions that run some Streamlit code. I can then call these functions to create segments of my app.
For example:
from my_functions import my_function
col1,col2 = st.columns(2)
my_function(col1) # ----> runs a set of st commands in col1
So that if I change the argument, streamlit will run the commands on a different container (col2, for example).
The problem
In order to avoid unexpected behavior, I want to include type-hints in my function definitions. There are 2 motives behind this:
Only run the function if a valid Streamlit container is given.
When editing the functions, allow Pylance to give method suggestions
Expected behavior:
I expected to be able to do this:
# my_functions.py
import streamlit as st
def my_function(column : st.Container_Type):
column.write("Foo")
column.markdown("# Bar")
column.button("Click me to do a thing")
# etc
However, there is no such st.Container_Type that I could find. I tried checking in the documentation as well as in the Streamlit GitHub, but I am not super experienced and I donāt know what to look for.
Any help on how to do this, or any alternatives are greatly appreciated.
I think this may be a ācurse of knowledgeā type of situationā¦
TL;DR
How do I tell my function, which is in a separate .py file :
Hey function! Just so you know, the variable named ācontainerā is a Streamlit Container"
Explanation
Is there an example on how to use st.delta_generator across multiple scripts ?
A lot of this is very new to me. I donāt understand how Delta Generator is supposed to be used in this scenario. I can see you can use it to style containers, but i donāt want to style them, I want to create elements inside a given container.
in a separate script:
# my_functions.py
import streamlit as st
from pandas import DataFrame
def do_a_thing(container, df : DataFrame):
data = df.value_counts() # Method recognized
# .value_counts() is recognized as a valid method for a pd.DataFrame object
container.write(data) # Method not recognized
# .write() is not recognized because the script has no idea what "container" is supposed to be
in the main script:
# streamlit_app.py
import streamlit as st
from my_functions import do_a_thing()
import pandas as pd
...
...
... # at some point in the script...
columns = st.columns(2)
do_a_thing(columns[0] , my_dataframe)
In essence, how do I tell my function:
Hey function! Just so you know, the variable named ācontainerā is a Streamlit Container"
⦠FYI: Thestreamlit_app.pyscript still āworksā and does what I want it to do. The issue is that when editing and modifyingmy_functions.pyI am flying āblindā since pylance cannot give me suggestions.