diff --git a/python/starlette/.gitignore b/python/starlette/.gitignore new file mode 100644 index 0000000000..fb86138ef5 --- /dev/null +++ b/python/starlette/.gitignore @@ -0,0 +1,8 @@ +.vercel/ +.venv/ +venv/ +__pycache__/ +.Python +.DS_Store +.env* +.vercel diff --git a/python/starlette/README.md b/python/starlette/README.md new file mode 100644 index 0000000000..a1c02db634 --- /dev/null +++ b/python/starlette/README.md @@ -0,0 +1,50 @@ +# Starlette Starter + +Deploy your [Starlette](https://www.starlette.io/) project to Vercel with zero configuration. + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?demo-description=Deploy%20Python%20Starlette%20applications%20with%20zero%20configuration.&demo-title=Starlette%20Boilerplate&demo-url=https%3A%2F%2Fvercel-plus-starlette.vercel.app%2F&from=templates&project-name=Starlette%20Boilerplate&repository-name=starlette-python-boilerplate&repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fexamples%2Ftree%2Fmain%2Fpython%2Fstarlette&skippable-integrations=1) + +_Live Example: https://vercel-plus-starlette.vercel.app/_ + +Visit the [Starlette documentation](https://www.starlette.io/) to learn more. + +## Getting Started + +Install the required dependencies: + +```bash +python -m venv .venv +source .venv/bin/activate +pip install . +``` + +Or, if using [uv](https://docs.astral.sh/uv/): + +```bash +uv sync +``` + +## Running Locally + +Start the development server on http://0.0.0.0:5001 + +```bash +python main.py +# using uv: +uv run main.py +``` + +When you make changes to your project, the server will automatically reload. + +## Deploying to Vercel + +Deploy your project to Vercel with the following command: + +```bash +npm install -g vercel +vercel --prod +``` + +Or `git push` to your repository with our [git integration](https://vercel.com/docs/deployments/git). + +To view the source code for this template, [visit the example repository](https://github.com/vercel/vercel/tree/main/examples/starlette). diff --git a/python/starlette/api/__init__.py b/python/starlette/api/__init__.py new file mode 100644 index 0000000000..25f28f9a11 --- /dev/null +++ b/python/starlette/api/__init__.py @@ -0,0 +1,3 @@ +from .routes import api_routes + +__all__ = ["api_routes"] diff --git a/python/starlette/api/routes.py b/python/starlette/api/routes.py new file mode 100644 index 0000000000..277d75a598 --- /dev/null +++ b/python/starlette/api/routes.py @@ -0,0 +1,36 @@ +from starlette.responses import JSONResponse +from starlette.routing import Route + + +async def get_sample_data(request): + return JSONResponse( + { + "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", + } + ) + + +async def get_item(request): + item_id = int(request.path_params["item_id"]) + return JSONResponse( + { + "item": { + "id": item_id, + "name": f"Sample Item {item_id}", + "value": item_id * 100, + }, + "timestamp": "2024-01-01T00:00:00Z", + } + ) + + +api_routes = [ + Route("/data", get_sample_data), + Route("/items/{item_id:int}", get_item), +] diff --git a/python/starlette/main.py b/python/starlette/main.py new file mode 100644 index 0000000000..b941be4c09 --- /dev/null +++ b/python/starlette/main.py @@ -0,0 +1,110 @@ +from starlette.applications import Starlette +from starlette.responses import HTMLResponse +from starlette.routing import Mount, Route +from api import api_routes + + +async def read_root(request): + return HTMLResponse(""" + + + + + + Vercel + Starlette + + + + +
+ +
+
+
+

Vercel + Starlette

+
+
from starlette.applications import Starlette
+from starlette.responses import JSONResponse
+from starlette.routing import Route
+
+async def homepage(request):
+    return JSONResponse({"Python": "on Vercel"})
+
+app = Starlette(routes=[Route("/", homepage)])
+
+
+ +
+
+

Sample Data

+

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

+ Get Data → +
+
+
+ + + """) + + +app = Starlette( + routes=[ + Route("/", read_root), + Mount("/api", routes=api_routes), + ] +) + + +if __name__ == "__main__": + import uvicorn + uvicorn.run(app, host="0.0.0.0", port=5001) diff --git a/python/starlette/public/favicon.ico b/python/starlette/public/favicon.ico new file mode 100644 index 0000000000..b40a8cd538 Binary files /dev/null and b/python/starlette/public/favicon.ico differ diff --git a/python/starlette/pyproject.toml b/python/starlette/pyproject.toml new file mode 100644 index 0000000000..59d218fde6 --- /dev/null +++ b/python/starlette/pyproject.toml @@ -0,0 +1,7 @@ +[project] +name = "vercel-starlette-starter" +version = "0.1.0" +requires-python = ">=3.12" +dependencies = [ + "starlette>=0.40" +]