Skip to content

Instantly share code, notes, and snippets.

@tvst

tvst/opt_echo.py

Created Oct 10, 2019
Embed
What would you like to do?
Hack to make st.echo show a checkbox to toggle code display. See https://discuss.streamlit.io/t/show-code-optional-wrapper/329
import streamlit as st
import traceback
class opt_echo:
"""Replacement for st.echo that includes a checkbox saying "show code".
Usage
-----
>>> with opt_echo():
... a = 1337
"""
def __init__(self):
self.checkbox = st.checkbox("show code")
# This is a mega-hack!
# And it's also not thread-safe. Don't use this if you have threaded
# code that depends on traceback.extract_stack
self.orig_extract_stack = traceback.extract_stack
if self.checkbox:
traceback.extract_stack = lambda: self.orig_extract_stack()[:-2]
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)
# For some reason I need to import this again.
import traceback
traceback.extract_stack = self.orig_extract_stack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.