Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can it support to keep displaying the information from a file which keeps changing? #36

Open
chiehpower opened this issue Apr 13, 2022 · 1 comment

Comments

@chiehpower
Copy link

Hi,
Thanks for providing this awesome widgets!!
I have read the issue #25 and #28.

My idea is to apply the streamlit-ace to display the log file, and the log file will keep changing (i.e., It will keep adding some information into the log file.)

Here is my current code which was referred from #25.

        with open('/var/log/out.log') as f:
            lines = f.read()
            content = st_ace(value=lines)

Is it possible to keep displaying the log file information this goal?

Thank you!!

BR,
Chieh

@NichtJens
Copy link

NichtJens commented Jul 10, 2023

This is inherently not how streamlit (i.e., the whole framework, not only this editor component) works. Streamlit reruns your whole script every time a widget is somehow changed. What you want is to trigger the rerun from within the script, if the log file changes. Like this:

from time import sleep
import streamlit as sl

if "contents" not in sl.session_state:
    sl.session_state.contents = ""

sl.text(sl.session_state.contents)

while True:
    with open("log.txt") as f:
        current = f.read()
    if current != sl.session_state.contents:
        sl.session_state.contents = current
        sl.experimental_rerun()
    sleep(1)

The first half is just how streamlit works, if there's contents in the session state, display it as text*.

The second half is an infinite loop that prevents the current run of the script from ending. It reads the file every second, and if the contents of the file changed, it updates the session state and asks streamlit to rerun.

If this is all you want to achieve, it might be an OK solution.

A better solution might be this: https://github.com/kmcgrady/streamlit-autorefresh

*I say text here, because using an editor for displaying a read-only (and relatively free-form) file like a log does not seem like a great fit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants