question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Forward stdout to some widget

See original GitHub issue

Problem

I tried to use streamlit for the interactive clustering app. In my case clustering is long. And the only way to see any progress is to use verbose=1. In such a case, sklearn algorithm put some information to stdout. But I see no possibility to show this information with streamlit.


Community voting on feature requests enables the Streamlit team to understand which features are most important to our users.

If you’d like the Streamlit team to prioritize this feature request, please use the 👍 (thumbs up emoji) reaction in response to the initial post.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:8
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
schaumbcommented, Apr 9, 2021

I created a gist from this problem https://gist.github.com/schaumb/037f139035d93cff3ad9f4f7e5f739ce with a simplified syntax, and with realtime output:

from streamlit.redirect as rd

with rd.stdout:
  print('blablabla')
  time.sleep(1)
  print('Hello from code block')

It can be configure easily:

st.sidebar.text("Standard output message here:")
to_out = st.sidebar.empty()
...
with rd.stdout(to=to_out, format='markdown'):
  print('**Hello** from markdown')

It can be nesting:

garbage = st.empty()

with rd.stdout:
  print('important message')
  with rd.stdout(to=garbage):
    print("some garbage")
  garbage.write()
  print("another important message")

What do you think?

3reactions
jesserobertsoncommented, Aug 11, 2020

I built on zblz’s workaround and turned it into a wrapper that might be useful for some people:

import contextlib
from functools import wraps
from io import StringIO

import streamlit as st

def capture_output(func):
    """Capture output from running a function and write using streamlit."""

    @wraps(func)
    def wrapper(*args, **kwargs):
        # Redirect output to string buffers
        stdout, stderr = StringIO(), StringIO()
        try:
            with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
                return func(*args, **kwargs)
        except Exception as err:
            st.write(f"Failure while executing: {err}")
        finally:
            if _stdout := stdout.getvalue():
                st.write("Execution stdout:")
                st.code(_stdout)
            if _stderr := stderr.getvalue():
                st.write("Execution stderr:")
                st.code(_stderr)

    return wrapper

Then you can do something like

stprint = capture_output(print)

stprint(some_variables)

which makes print debugging easier

edit: added relevant import statements for code snippet

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to redirect stdout to a Tkinter Text widget - Stack Overflow
Inorder to help ya'll if you struggling with this, I've made a script to redirect stdout to a Tkinter Text widget, see it...
Read more >
Tkinter - Redirecting stdout / stderr - Mouse Vs Python
The first class we see is called RedirectText. It takes a text control widget as its parameter. we create a write method that...
Read more >
redirect stdout to tkinter text widget : r/learnpython - Reddit
Hi, I have created a tool for web automation with selenium and have build a GUI with tkinter. The program works fine, but...
Read more >
Re: redirecting stdout to gtk_text widget - GNOME Mail Services
I need help on how to redirect stdout to a text widget. ... Creates a new text view with a text buffer "Hello,...
Read more >
Thread: [question]redirecting stdout to display via widget?
I'm very new to PyQt4 (its my fifth day) and I would like some help with this ... I would like to redirect...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found