Exec() in streamlit

Summary

Can’t run EXEC(“some string function”)
If that function have streamlit syntax
I have string function that printing dataframe(st.dataframe)

Steps to reproduce

func = """
from pathlib import Path
from pandas import Series, DataFrame
import numbers
import numpy as np
from pathlib import Path
import pandas as pd
import streamlit as st

st.set_page_config(layout='wide', page_title='Lab-Kit')

def get_n_extreme(data: DataFrame, sort_by: numbers, key ,n = 5, zero_included = True,thresh = 0):
    """
some calculation...
"""
    return final_df

data_test =get_n_extreme(test ,sort_by, key , n = 5,zero_included=False,thresh=-999)
st.dataframe(data_test) # show DataFrame
"""



exec("""{func}""")

I have file with some function that reads SQL and do some pandas calculation , than Im just st.dataframe the result

I read the file as string and using EXEC(file_function)

Expected behavior:

To print the dataframe in the same page

Actual behavior:

Refresh of the page with error
Set_page_config can only be called once
If I remove this line it doesn’t react at all

Debug info

  • Python version: 3.7
  • Using PyEnv 3.7

May I ask what’s in the “some calculation” that’s leading you down the path to exec()?

i need the ability to read string(full function block) and display the result
for the matter it can be anything the function can be just temp_df = pd.read_sql(“some table”) and do st.dataframe( temp_df)

I would highly recommend either removing exec altogether, or at a minimum isolating it down to just the minimal amount of code that you actually need.

If you’re reading a separate file in as a string, you should just be able to import that file to run it. Or else, you can put the code you want to run into a function inside that file, import the function, and run that.

1 Like

I agree with @blackary. The reason I asked about what you were trying to accomplish is there may be a better way than using exec.

The error message is telling you why your expected behavior cannot occur and hints at possible ways to fix the problem. Is there something you don’t understand?

Hey, I want to show a function and than execute and show the result on the same page
That is what I try to achieve

The string contains variables specific to execute the function, and the function itself
Let’s say I have 2,3 function that I want to show and than execute the function using EXEC()
Is it nor possible to achieve in streamlit?

Use the functions inline with the Streamlit code (assuming you are in control of, or own these functions). If you can’t control or bind the variable values in these functions within the Streamlit app, then build a simple API wrapper to execute the functions and return results (this is just half a dozen lines of code using FastAPI). Then call this API from your Streamlit app. Because the execution lifecycle of Streamlit apps and the lifecycle of a separate executing processes or thread (your exec fork) is not a natural fit, you ought to separate them, rather than try to glue them together. That’s the simplest way to do this IMO.