streamlit-ace icon indicating copy to clipboard operation
streamlit-ace copied to clipboard

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

Open chiehpower opened this issue 2 years ago • 1 comments

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

chiehpower avatar Apr 13 '22 07:04 chiehpower

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.

NichtJens avatar Jul 10 '23 19:07 NichtJens