Hi @Richard_SL, welcome to the Streamlit community!!
One of our community members, @BugzTheBunny, has created an amazing working example of a logging app. While it may not be exactly what you’re looking for, do you reckon you could repurpose the code to build your logging text box?
I’m still having a bit of trouble picturing exactly what you want to create. It would be great if you could break it down more or elaborate if @BugzTheBunny’s example isn’t what you have in mind?
Hello @snehankekre , thank you very much for your fast reply. The example is indeed an interesting approach, however I would like to create it in a bit different way (unfortunately I am also still in a learning curve regarding Python and Streamlit).
The advantage of the example (code) is that you will always see the last lines of the text, the disadvantage is that the text box is growing, when more text is added (I would like to have a fixed size with scroll bars). I don’t know if that can be added…
The general idea is to create a fixed size “text box” on a webpage:
import streamlit as st
import time
st.set_page_config(layout="wide")
st.title('Logging in Text Box')
# creating a placeholder for the fixed sized textbox
logtxtbox = st.empty()
logtxt = 'start'
logtxtbox.text_area("Logging: ",logtxt, height = 500)
end_of_loop = False
counter = 1
while (end_of_loop==False):
logtxt += 'Counter [' + str(counter) + '] \n'
logtxtbox.text_area("Logging: ", logtxt, height=500)
counter += 1
if (counter > 100):
end_of_loop = True
time.sleep(0.2)
In this example the text box will stay fixed, however you will only see the first lines of the logging. Of course you can scroll to the end, but I would prefer to see the last added lines and be able to scroll back to the beginning (like for example a text editor where you scroll down till the end of the document).
An extra feature, as a next step, would be to also have the possibility to be able to override the text on the last line (for example a counter which is counting on the same line…
An example of that I made here below (but the combination is still another issue…):
import streamlit as st
import time
t = st.empty()
end_of_loop = False
counter = 1
while (end_of_loop==False):
t.write('Counter: ' + str(counter))
if (counter % 10 ==0) :
t = st.empty()
counter += 1
if (counter > 100) :
end_of_loop = True
time.sleep(0.2)
And in the end I would like to be able to get the complete content of this text box and write it to a text file.
If I find a solution in the meantime I will post it here, but all help is still very welcome.
The placeholder code in the while loop is likely not needed. Using st.write or st.markdown would likely work fine. I just wanted to show that IF you use this while look approach, put it at the end of your streamlit script. Even then, watch how the time is interrupted when the button state is updated in the code above the while loop. Thus, you can update later due to another later operation OR update continuously. Just put the continuous update (eternal while loop) stuff at the end.
import streamlit as st
from datetime import datetime
import time
# Section 1
button = st.button('Button')
button_placeholder = st.empty()
button_placeholder.write(f'button = {button}')
time.sleep(2)
button = False
button_placeholder.write(f'button = {button}')
# Section 2
time_placeholder = st.empty()
while True:
timenow = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
time_placeholder.write(timenow)
time.sleep(1)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.