patx/micropie
fix the multipart form issue, uses the `multipart` by defnull. dependency optional
Commit c1108be · patx · 2025-02-02T11:00:42-05:00
Comments
No comments yet.
Diff
diff --git a/MicroPie.py b/MicroPie.py
index d505062..3e5ddbb 100644
--- a/MicroPie.py
+++ b/MicroPie.py
@@ -41,7 +41,11 @@ from urllib.parse import parse_qs
import uuid
import contextvars
-from multipart import PushMultipartParser, MultipartSegment
+try:
+ from multipart import PushMultipartParser, MultipartSegment
+ MULTIPART_INSTALLED = True
+except ImportError:
+ MULTIPART_INSTALLED = False
try:
from jinja2 import Environment, FileSystemLoader
@@ -236,9 +240,12 @@ class Server:
async def _parse_multipart(self, reader: asyncio.StreamReader, boundary: bytes):
"""
- Demonstrates handling multipart/form-data in a more streaming-friendly manner.
+ Multipart/form-data in a more streaming-friendly manner.
For large files, data is written to disk instead of stored in memory.
"""
+ if not MULTIPART_INSTALLED:
+ raise ImportError("Multipart form data not supported. Install `multipart` via pip.")
+
with PushMultipartParser(boundary) as parser:
current_field_name = None
current_filename = None
@@ -434,7 +441,7 @@ class Server:
Async-compatible template rendering using Jinja2.
"""
if not JINJA_INSTALLED:
- raise ImportError("Jinja2 is not installed.")
+ raise ImportError("`_render_template` not available. Install `jinja2` via pip.")
def render_sync():
return self.env.get_template(name).render(kwargs)
diff --git a/README.md b/README.md
index 006a52a..fe4d13c 100644
--- a/README.md
+++ b/README.md
@@ -19,16 +19,16 @@ Install MicroPie via pip:
```bash
pip install micropie
```
-This will install MicroPie along with `jinja2` for template rendering. Jinja2 is optional but recommended for using the `render_template` method.
+This will install MicroPie along with `jinja2` for template rendering and `multipart` for parsing multipart form data.
### **Minimal Setup**
For an ultra-minimalistic approach, download the standalone script:
[MicroPie.py](https://raw.githubusercontent.com/patx/micropie/refs/heads/main/MicroPie.py)
-Place it in your project directory, and you are good to go. Note that Jinja2 must be installed separately to use templates, but this *is* optional:
+Place it in your project directory, and you are good to go. Note that `jinja2` must be installed separately to use templates and/or `multipart` for handling file uploads, but this *is* optional:
```bash
-pip install jinja2
+pip install jinja2 multipart
```
### **Install an ASGI Web Server**
@@ -165,7 +165,7 @@ The best way to get an idea of how MicroPie works is to see it in action! Check
- Form handling and POST requests
- And more
-*Please note these are examples, showing the MicroPie API and routing, they are not meant for producton!*
+*Please note these are examples, showing the MicroPie API, they are not meant for producton!*
## **Why ASGI?**
ASGI is the future of Python web development, offering:
- **Concurrency**: Handle thousands of simultaneous connections efficiently.