File Upload Component¶
Version
This feature was introduced in v0.6.0.
Enhanced file upload UI component with drag-and-drop support, animated progress indicators, and automatic encoding detection.
render_file_upload ¶
Render file upload UI with dropzone and animations.
Provides a polished file upload experience with: - Drag-and-drop dropzone interface - Animated progress indicators (minimum ~750ms) - Automatic encoding detection - File validation and error handling
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
st
|
object
|
Streamlit module object |
required |
accepted_types
|
list[str] | None
|
List of accepted file extensions (e.g., [".csv"]) Defaults to [".csv"] |
None
|
help_text
|
str | None
|
Optional help text to display |
None
|
show_file_info
|
bool
|
Whether to display file info after upload (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
object | None
|
Tuple of (uploaded_file, dataframe, encoding_used) |
object | None
|
|
str | None
|
|
tuple[object | None, object | None, str | None]
|
|
Source code in lavendertown/ui/upload.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
Overview¶
The render_file_upload() function provides a polished file upload experience for Streamlit applications. It includes:
- Drag-and-drop interface: Enhanced styling for intuitive file uploads
- Animated progress indicators: Multi-stage progress animations (minimum ~750ms) for visual feedback
- Automatic encoding detection: Tries multiple encodings (UTF-8, Latin-1, ISO-8859-1, CP1252) automatically
- File validation: Validates file format and provides clear error messages
- File size display: Shows file information and warnings for large files
Basic Usage¶
import streamlit as st
from lavendertown.ui.upload import render_file_upload
# Basic usage with defaults
uploaded_file, df, encoding_used = render_file_upload(st)
if uploaded_file is not None:
if df is not None:
st.write(f"File loaded successfully with {encoding_used} encoding")
st.dataframe(df)
else:
st.error("Could not read the uploaded file")
Customization¶
Custom Accepted File Types¶
uploaded_file, df, encoding_used = render_file_upload(
st,
accepted_types=[".csv", ".txt", ".tsv"]
)
Custom Help Text¶
uploaded_file, df, encoding_used = render_file_upload(
st,
help_text="Upload your data file here. Supported formats: CSV, TSV"
)
Hide File Info Display¶
Return Values¶
The function returns a tuple of three values:
uploaded_file: The uploaded file object (orNoneif no file uploaded)dataframe: Pandas DataFrame if file was successfully read (orNoneif read failed)encoding_used: The encoding that successfully decoded the file (orNoneif read failed)
Encoding Detection¶
The component automatically tries multiple encodings in order:
- UTF-8 (most common)
- Latin-1 (ISO-8859-1)
- ISO-8859-1
- CP1252 (Windows-1252)
The first encoding that successfully decodes the file is used. If all encodings fail, the function returns None for the dataframe and shows an error message.
Progress Animation¶
The component includes a multi-stage progress animation:
- Upload Stage (0-20%): "Reading file..."
- Validation Stage (20-40%): "Validating format..."
- Processing Stage (40-60%): "Processing data..."
- Encoding Detection (60-80%): "Preparing analysis..."
- Ready Stage (80-100%): "Ready!"
Each stage has a minimum delay to ensure users see feedback even for very fast operations.
File Size Warnings¶
For files larger than 10MB, the component automatically displays a warning suggesting data sampling for faster analysis.
Error Handling¶
The component handles various error cases gracefully:
- Empty files: Shows clear error message
- Invalid CSV format: Displays parsing error
- Encoding failures: Tries multiple encodings before failing
- File read errors: Returns file object but
Nonefor dataframe
Example: Complete Upload Workflow¶
import streamlit as st
from lavendertown import Inspector
from lavendertown.ui.upload import render_file_upload
st.title("Data Quality Inspector")
# Upload file with enhanced UI
uploaded_file, df, encoding_used = render_file_upload(
st,
accepted_types=[".csv"],
help_text="Upload a CSV file to analyze for data quality issues",
show_file_info=True
)
if uploaded_file is not None:
if df is None:
st.error("Could not read the CSV file. Please check the file format.")
st.stop()
# Show success message
st.success(f"✅ File loaded successfully (encoding: {encoding_used})")
# Display dataset preview
st.header("Dataset Preview")
st.dataframe(df.head(10))
# Run inspection
inspector = Inspector(df)
inspector.render()
else:
st.info("👆 Please upload a CSV file to get started.")
Integration with Inspector¶
The upload component is designed to work seamlessly with LavenderTown's Inspector:
from lavendertown import Inspector
from lavendertown.ui.upload import render_file_upload
uploaded_file, df, encoding_used = render_file_upload(st)
if df is not None:
inspector = Inspector(df)
inspector.render() # Full data quality analysis
Styling¶
The component includes enhanced CSS styling for a modern dropzone appearance:
- Dashed border with hover effects
- Smooth transitions
- Visual feedback on file selection
- Consistent with Streamlit's design system
Performance Considerations¶
- Small files (<1MB): Near-instantaneous processing
- Medium files (1-10MB): Fast processing with progress feedback
- Large files (>10MB): Shows warning and suggests sampling
For very large files, consider implementing data sampling before analysis.
See Also¶
- Basic Usage Guide for general usage patterns
- Examples for complete examples
- Inspector API for data quality analysis