okld/streamlit-ace

Getting inputs and output the result of the code.

HasarinduPerera opened this issue · 2 comments

Hi, is there any way to get the value of the streamlit widget as an input to the code inside the Ace editor and execute the code (entered by the user) and print out the result of executed code?

Example:

# Get user input
user_input = st.slider(
    'Input:',
    0.0, 100.0
)

# Spawn a new Ace editor
content = st_ace()

# Printout the result
output = exec(content)
st.text(content)

I tried using the exec() function but as it only returns a None I cannot print out the results. I also tried the eval() but it gives me an error.

Thanks you!

Mi-La commented

Hi, I think this is not related to streamlit ace, to catch the output you can use either redirect_stdout, or you can run it e.g. as a subprocess - see subprocess.run.

You can find some inspiration here.

Note that it's not easy to prevent users from doing bad things when you allow them to execute their own code...

Thank you!