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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.