terminal4 / app.py
skanaujiya's picture
Update app.py
00de18e verified
raw
history blame
3.32 kB
import streamlit as st
import subprocess
# Set page configuration
st.set_page_config(page_title="Advanced Terminal Emulator", layout="wide")
# Custom CSS for output and error background colors with transparency
st.markdown("""
<style>
.output {
background-color: rgba(76, 175, 80, 0.2); /* Light green with transparency for output */
padding: 10px;
border-radius: 5px;
border: 1px solid rgba(76, 175, 80, 0.5); /* Semi-transparent green border */
}
.error {
background-color: rgba(244, 67, 54, 0.2); /* Light red with transparency for error */
padding: 10px;
border-radius: 5px;
border: 1px solid rgba(244, 67, 54, 0.5); /* Semi-transparent red border */
}
</style>
""", unsafe_allow_html=True)
# Main title
st.title("🎨 Advanced Terminal Emulator")
# Instructions and description
st.markdown("""
This enhanced terminal emulator lets you run commands and view outputs in a clean and modern interface.
You can access previous commands and see results in a well-organized manner.
""")
# Command history management
if 'history' not in st.session_state:
st.session_state.history = []
# Function to run the command and capture output
def run_command(command):
try:
result = subprocess.run(command, shell=True, capture_output=True, text=True)
st.session_state.output = result.stdout
st.session_state.error = result.stderr
except Exception as e:
st.session_state.error = str(e)
st.session_state.output = None
# Layout with three columns
col1, col2, col3 = st.columns([1, 2, 1])
# Command history display in the first column
with col1:
st.subheader("Command History")
for cmd in reversed(st.session_state.history):
if st.button(f"Run: {cmd}", key=cmd):
run_command(cmd)
# Command input in the second column
with col2:
command = st.text_area("Enter terminal command", placeholder="e.g., ls, pwd, echo 'Hello World'", height=100)
if st.button("Run Command"):
if command:
run_command(command)
# Add to command history
if command not in st.session_state.history:
st.session_state.history.append(command)
else:
st.warning("Please enter a command to run.")
# Output display in the third column
with col3:
if 'output' in st.session_state and st.session_state.output:
st.subheader("Output:")
st.markdown(f'<div class="output">{st.session_state.output}</div>', unsafe_allow_html=True)
if 'error' in st.session_state and st.session_state.error:
st.subheader("Error:")
st.markdown(f'<div class="error">{st.session_state.error}</div>', unsafe_allow_html=True)
# Optional file upload (if needed)
uploaded_file = st.file_uploader("Upload a file to process with command:", type=["txt", "csv"])
if uploaded_file:
file_content = uploaded_file.read().decode("utf-8")
st.subheader("Uploaded File Content")
st.text_area("File Content", value=file_content, height=200)
# Footer with additional information
st.markdown("""
---
**Note:** This is a simple terminal emulator built with Streamlit. Use it for running commands and managing outputs efficiently. For advanced usage, consider additional functionalities.
""")