Write responsive web apps in full python
—
Secure file upload and download system using bucket containers with configurable limits, automatic URL generation, and integration with view lifecycle.
Container for managing file uploads and downloads with security controls.
class Bucket:
def __init__(self, request, max_files: int = None, max_size: int = None,
index: bool = True, on_add=None, on_delete=None):
"""
Create a file bucket.
Args:
request: Request object
max_files (int): Maximum number of files
max_size (int): Maximum total size in bytes
index (bool): Whether to create file index
on_add: Callback for file additions
on_delete: Callback for file deletions
"""
def get_path(self, file_name: str = '') -> str:
"""
Get file system path.
Args:
file_name (str): Specific file name
Returns:
str: File system path
"""
def get_url(self, file_name: str = '') -> str:
"""
Get web URL for file.
Args:
file_name (str): Specific file name
Returns:
str: Web accessible URL
"""
def get_file_names(self) -> list:
"""
Get list of file names.
Returns:
list: List of file names in bucket
"""
def get_size(self) -> int:
"""
Get total size of files.
Returns:
int: Total size in bytes
"""
def get_add_url(self) -> str:
"""
Get upload URL.
Returns:
str: URL for uploading files
"""
def get_delete_url(self, file_name: str) -> str:
"""
Get deletion URL for file.
Args:
file_name (str): File to delete
Returns:
str: URL for deleting the file
"""from lona import App, View, Bucket
from lona.html import HTML, H1, P, A, Ul, Li
app = App(__file__)
@app.route('/files')
class FileManagerView(View):
def handle_request(self, request):
# Create bucket with limits
bucket = Bucket(
request,
max_files=10,
max_size=10 * 1024 * 1024 # 10MB
)
# Display current files
file_list = Ul()
for filename in bucket.get_file_names():
file_url = bucket.get_url(filename)
delete_url = bucket.get_delete_url(filename)
file_list.append(
Li(
A(filename, href=file_url),
' - ',
A('Delete', href=delete_url)
)
)
upload_url = bucket.get_add_url()
html = HTML(
H1('File Manager'),
P(f'Total size: {bucket.get_size()} bytes'),
P(f'Upload files: {upload_url}'),
file_list
)
return htmlfrom typing import Optional, Callable, List
FileName = str
FilePath = str
FileSize = int
FileCallback = Callable[[str], None]Install with Tessl CLI
npx tessl i tessl/pypi-lona