To achieve real-time streaming of terminal command output in Streamlit while ensuring that the UI updates continuously, we can utilize Streamlit's st.empty() feature to create a placeholder that will be updated in a loop.
One important point is that Streamlit rerenders the whole script when there's a change in the UI. Thus, we will use a while loop to keep updating the output from the running command.
Here is the updated version of the Streamlit app that should enable real-time streaming of terminal output:
import subprocess
import streamlit as st
import time
def run_command(command):
"""Run a terminal command and yield its output line by line."""
# Start the command as a subprocess
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
try:
while True:
output = process.stdout.readline() # Read a line of output
if output == '' and process.poll() is not None: # Check if process is done
break
if output:
yield output.strip() # Yield the output line
finally:
process.kill() # Ensure the process is killed after finishing
# Streamlit app layout
st.title('Real-Time Terminal Output Stream')
st.write('This app streams the output of a terminal command in real time.')
# Input for the command
command = st.text_input("Enter the command to run:", "ping google.com -c 10")
if st.button('Run Command'):
st.write("Running command...")
# Create a placeholder for the output
output_placeholder = st.empty()
# Clear previous output (if any)
full_output = ""
# Stream the command output
for line in run_command(command):
full_output += line + "\n" # Append new output to full output
output_placeholder.text_area("Command Output:", value=full_output, height=300)
st.success("Command execution completed.")
Why This Should Work
- Continuous Output: The
run_command function will yield output line-by-line as the command executes.
- Placeholder Updates: The
st.empty() creates a container that we can update directly. By setting the value of the text area on every iteration, the UI will show the updated output continuously.
- Handling Completion: When the command has finished executing, the success message will inform the user that the command execution has completed.
Things to Note
- Long Output and Performance: Streaming output in real-time can lead to performance issues if the command generates a lot of output very quickly. You might want to implement some throttling or limit how much output is held in
full_output.
- Command Execution: Ensure that the command you enter is appropriate for your environment. The example command
ping google.com -c 10 works on UNIX-like systems. For Windows, use ping google.com -n 10.
- Output Handling: Depending on your use case, you might want to format the output better or handle errors from the command (by also yielding
stderr) for a more user-friendly experience.
This setup should stream the output of the command to the Streamlit interface in real time. If you still face issues with the real-time aspect, ensure that your command produces sufficient output quickly enough to see updates in the UI.