So print(profiler.output_text(unicode=True, color=True)) prints the desired output to the terminal (#2 in screenhost above)
yet its streamlit transalation via st.write -> st.write(profiler.output_text(unicode=True, color=True)) doesn’t print properly in Streamlit, see screenshot below::
As profiler.output_text() is meant to be printed in a console, when you enable colors, it uses ANSI color codes. You have two ways to display such output:
1. Strip ANSI codes and display the result with st.code()
import re
# ... your code ...
profiler.stop()
profiler_output = profiler.output_text(unicode=True, color=True)
# Regex source: https://stackoverflow.com/a/14693789
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
result = ansi_escape.sub("", profiler_output)
st.code(result)
With this last method, you’ll have to fix a height.
If fixing the height is an issue, another solution would be to create a real component and use a specific javascript library to automatically adjust the height as I do in all my components now.