diff --git a/python/fastapi/main.py b/python/fastapi/main.py index 4214abc1a0..43c9028b7b 100644 --- a/python/fastapi/main.py +++ b/python/fastapi/main.py @@ -1,6 +1,6 @@ from fastapi import FastAPI -from fastapi.staticfiles import StaticFiles -from fastapi.responses import FileResponse, HTMLResponse +from fastapi.responses import HTMLResponse + app = FastAPI( title="Vercel + FastAPI", @@ -9,14 +9,6 @@ ) -app.mount("/public", StaticFiles(directory="public"), name="public") - - -@app.get("/favicon.ico", include_in_schema=False) -async def favicon(): - return FileResponse("public/favicon.ico") - - @app.get("/api/data") def get_sample_data(): return { diff --git a/python/flask/README.md b/python/flask/README.md new file mode 100644 index 0000000000..0c2fb66cdb --- /dev/null +++ b/python/flask/README.md @@ -0,0 +1,31 @@ +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fvercel%2Ftree%2Fmain%2Fexamples%2Fflask&demo-title=Flask%20API&demo-description=Use%20Flask%20API%20on%20Vercel%20with%20Serverless%20Functions%20using%20the%20Python%20Runtime.&demo-url=https%3A%2F%2Fvercel-plus-flask.vercel.app%2F&demo-image=https://assets.vercel.com/image/upload/v1669994600/random/python.png) + +# Flask + Vercel + +This example shows how to use Flask on Vercel with Serverless Functions using the [Python Runtime](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python). + +## Demo + +https://vercel-plus-flask.vercel.app/ + +## How it Works + +This example uses the Web Server Gateway Interface (WSGI) with Flask to handle requests on Vercel with Serverless Functions. + +## Running Locally + +```bash +npm i -g vercel +python -m venv .venv +source .venv/bin/activate +uv sync # or alternatively pip install flask gunicorn +gunicorn main:app +``` + +Your Flask application is now available at `http://localhost:3000`. + +## One-Click Deploy + +Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=vercel-examples): + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fvercel%2Ftree%2Fmain%2Fexamples%2Fflask&demo-title=Flask%20API&demo-description=Use%20Flask%20API%20on%20Vercel%20with%20Serverless%20Functions%20using%20the%20Python%20Runtime.&demo-url=https%3A%2F%2Fvercel-plus-flask.vercel.app%2F&demo-image=https://assets.vercel.com/image/upload/v1669994600/random/python.png) diff --git a/python/flask/endpoints/__init__.py b/python/flask/endpoints/__init__.py new file mode 100644 index 0000000000..b497fd4f42 --- /dev/null +++ b/python/flask/endpoints/__init__.py @@ -0,0 +1,3 @@ +from .routes import api_bp + +__all__ = ["api_bp"] diff --git a/python/flask/endpoints/routes.py b/python/flask/endpoints/routes.py new file mode 100644 index 0000000000..5521deb675 --- /dev/null +++ b/python/flask/endpoints/routes.py @@ -0,0 +1,33 @@ +from flask import Blueprint, jsonify + + +api_bp = Blueprint("api", __name__) + + +@api_bp.get("/api/data") +def get_sample_data(): + return jsonify( + { + "data": [ + {"id": 1, "name": "Sample Item 1", "value": 100}, + {"id": 2, "name": "Sample Item 2", "value": 200}, + {"id": 3, "name": "Sample Item 3", "value": 300}, + ], + "total": 3, + "timestamp": "2024-01-01T00:00:00Z", + } + ) + + +@api_bp.get("/api/items/") +def get_item(item_id: int): + return jsonify( + { + "item": { + "id": item_id, + "name": f"Sample Item {item_id}", + "value": item_id * 100, + }, + "timestamp": "2024-01-01T00:00:00Z", + } + ) diff --git a/python/flask/main.py b/python/flask/main.py new file mode 100644 index 0000000000..7fb7afb6c6 --- /dev/null +++ b/python/flask/main.py @@ -0,0 +1,101 @@ +from flask import Flask +from endpoints import api_bp + + +app = Flask(__name__) + + +app.register_blueprint(api_bp) + + +@app.get("/") +def read_root(): + return """ + + + + + + Vercel + Flask + + + + +
+ +
+
+
+

Vercel + Flask

+
+
from flask import Flask
+
+app = Flask(__name__)
+
+@app.get("/")
+def read_root():
+    return {"Python": "on Vercel"}
+
+
+ +
+
+

Sample Data

+

Access sample JSON data through our REST API. Perfect for testing and development purposes.

+ Get Data → +
+
+
+ + + """ diff --git a/python/flask/public/favicon.ico b/python/flask/public/favicon.ico new file mode 100644 index 0000000000..efc4a4c989 Binary files /dev/null and b/python/flask/public/favicon.ico differ diff --git a/python/flask/pyproject.toml b/python/flask/pyproject.toml new file mode 100644 index 0000000000..058026c02a --- /dev/null +++ b/python/flask/pyproject.toml @@ -0,0 +1,8 @@ +[project] +name = "vercel-flask-starter" +version = "0.1.0" +requires-python = ">=3.9" +dependencies = [ + "flask~=3.1", + "gunicorn~=23.0.0 +]