diff --git a/streamlit-file-upload-template/app.py b/streamlit-file-upload-template/app.py new file mode 100644 index 00000000..aacf6eed --- /dev/null +++ b/streamlit-file-upload-template/app.py @@ -0,0 +1,38 @@ +import streamlit as st +from utils import get_file_metadata + +st.title('File Upload Template') + +# File uploader with no type restriction +uploaded_file = st.file_uploader("Choose a file", type=None) + +if uploaded_file is not None: + # Display file metadata + metadata = get_file_metadata(uploaded_file) + + st.subheader('File Metadata') + + # Create three columns for better layout + col1, col2, col3 = st.columns(3) + + with col1: + st.metric("Filename", metadata['filename']) + st.metric("File Size", metadata['size']) + + with col2: + st.metric("File Type", metadata['type']) + st.metric("Extension", metadata['extension']) + + with col3: + st.metric("Last Modified", metadata['last_modified']) + + # Display file contents preview if it's a text file + if metadata['type'] and 'text' in metadata['type'].lower(): + + try: + st.subheader('File Preview') + content = uploaded_file.read().decode() + st.text_area("Content Preview", content[:1000] + ("..." if len(content) > 1000 else ""), + height=200) + except: + None \ No newline at end of file diff --git a/streamlit-file-upload-template/app.yaml b/streamlit-file-upload-template/app.yaml new file mode 100644 index 00000000..4c9f07c6 --- /dev/null +++ b/streamlit-file-upload-template/app.yaml @@ -0,0 +1,8 @@ +command: + - "streamlit" + - "run" + - "app.py" + +env: +- name: STREAMLIT_BROWSER_GATHER_USAGE_STATS + value: "false" \ No newline at end of file diff --git a/streamlit-file-upload-template/requirements.txt b/streamlit-file-upload-template/requirements.txt new file mode 100644 index 00000000..47ba14f8 --- /dev/null +++ b/streamlit-file-upload-template/requirements.txt @@ -0,0 +1 @@ +streamlit==1.38.0 \ No newline at end of file diff --git a/streamlit-file-upload-template/utils.py b/streamlit-file-upload-template/utils.py new file mode 100644 index 00000000..9c73ab40 --- /dev/null +++ b/streamlit-file-upload-template/utils.py @@ -0,0 +1,14 @@ +import os +import mimetypes +from datetime import datetime + +def get_file_metadata(file): + """Extract metadata from uploaded file""" + metadata = { + 'filename': file.name, + 'size': f"{file.size / 1024:.2f} KB", + 'type': mimetypes.guess_type(file.name)[0] or 'Unknown', + 'extension': os.path.splitext(file.name)[1] or 'No extension', + 'last_modified': datetime.fromtimestamp(os.path.getmtime(file.name) if os.path.exists(file.name) else 0).strftime('%Y-%m-%d %H:%M:%S') + } + return metadata \ No newline at end of file