How to use type-hints in Streamlit function definitions?

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:

  1. Only run the function if a valid Streamlit container is given.
  2. 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 you’ll want to use DeltaGenerator, like this https://github.com/arnaudmiribel/streamlit-extras/blob/main/src/streamlit_extras/stylable_container/__init__.py

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: The streamlit_app.py script still “works” and does what I want it to do. The issue is that when editing and modifying my_functions.py I am flying “blind” since pylance cannot give me suggestions.

Streamlit containers are instances of streamlit.delta_generator.DeltaGenerator.

1 Like

Ah! Ok, now I understand the previous answer by @blackary
Thank you both very much!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.