I am trying to create an optional
st.echo that users of the app can look at the code whenever they are curious but hidden by default. To reduce the code for each of these occations, I though of a
with st.opt_echo(): context.
My first attempt is the following:
import streamlit as st class opt_echo: def __init__(self): self.checkbox = st.checkbox("show code") if self.checkbox: self.echo = st.echo() def __enter__(self): if self.checkbox: return self.echo.__enter__() def __exit__(self, type, value, traceback): if self.checkbox: self.echo.__exit__(type, value, traceback) with opt_echo(): st.write("hello.")
However, due to the way
st.echo is implemented, this outputs not ‘st.write(“hello”)’ to the app but the code of the
I somehow would have to modify
frame = _traceback.extract_stack()[-3] in
- What would be the solution that reuses most of the st.echo code?
- I am guessing this would be feature many users might be interested in so maybe this could even be added to the api?