cfdlbrz/shell
1
1import streamlit as st 2import os3import sys4import subprocess5import shlex6from datetime import datetime7 8def save(s: subprocess.CompletedProcess):9 if 'outputs' not in st.session_state:10 st.session_state['outputs'] = []11 st.session_state['outputs'].append({12 'returned_at': datetime.now(),13 'args': s.args,14 'stdout': str(s.stdout),15 'return_code': s.returncode,16 'stderr': str(s.stderr),17 })18 19def list_files(startpath):20 l = []21 for root, dirs, files in os.walk(startpath):22 level = root.replace(startpath, '').count(os.sep)23 indent = ' ' * 4 * (level)24 l.append('{}{}/'.format(indent, os.path.basename(root)))25 subindent = ' ' * 4 * (level + 1)26 for f in files:27 l.append('{}{}'.format(subindent, f))28 print('{}{}'.format(subindent, f))29 return '\n'.join(l)30 31st.title("๐ Streamlit Shell ๐")32with st.expander("diagnostic"):33 st.code(list_files(os.getcwd()))34 st.code(f"sys.path: {sys.path}")35 st.code(f"sys.argv: {sys.argv}" )36 st.code(f"sys.executable: {sys.executable}")37 st.code(f"sys.flags: {sys.flags}")38 39cmd_txt = st.text_input("command input: ", help='This will call a simple subprocess.run(<input>). The input is best effort parsed by shlex')40if cmd_txt and cmd_txt != "":41 try:42 s = subprocess.run(shlex.split(cmd_txt), capture_output=True)43 save(s)44 if s.returncode!= 0: 45 st.warning(f'non-zero return: {s.returncode}', icon="โ ๏ธ")46 if s.stdout:47 st.code(s.stdout.decode())48 if s.stderr:49 st.code(s.stderr.decode())50 except Exception as inst:51 st.error(inst)52 53 54if 'outputs' in st.session_state:55 st.caption('hint: double click on long outputs')56 st.dataframe(st.session_state['outputs'])