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 st.markdown(""" """, 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) if result.stdout: st.subheader("Output:") st.markdown(f'
{result.stdout}
', unsafe_allow_html=True) if result.stderr: st.subheader("Error:") st.markdown(f'
{result.stderr}
', unsafe_allow_html=True) except Exception as e: st.error(f"An error occurred: {e}") # Layout with columns col1, col2 = st.columns([2, 1]) # Command input and button in the first column with col1: 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.") # Command history display in the second column with col2: st.subheader("Command History") for cmd in reversed(st.session_state.history): if st.button(f"Run: {cmd}", key=cmd): run_command(cmd) # 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. """)