Skip to content

Commit 5be84d6

Browse files
authored
Update nextjs flask example (#1452)
### Description Update Next.js + Flask example to use Services ### Demo URL <!-- Provide a URL to a live deployment where we can test your PR. If a demo isn't possible feel free to omit this section. --> ### Type of Change - [ ] New Example - [x] Example updates (Bug fixes, new features, etc.) - [ ] Other (changes to the codebase, but not to examples) ### New Example Checklist - [ ] 🛫 `npm run new-example` was used to create the example - [ ] 📚 The template wasn't used but I carefuly read the [Adding a new example](https://github.com/vercel/examples#adding-a-new-example) steps and implemented them in the example - [ ] 📱 Is it responsive? Are mobile and tablets considered?
1 parent c9ecf12 commit 5be84d6

9 files changed

Lines changed: 46 additions & 194 deletions

File tree

python/nextjs-flask/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ yarn-error.log*
3434
*.tsbuildinfo
3535
next-env.d.ts
3636
__pycache__
37+
*.egg-info/
38+
build/
39+
dist/

python/nextjs-flask/README.md

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ This is a hybrid Next.js + Python app that uses Next.js as the frontend and Flas
1515

1616
## How It Works
1717

18-
The Python/Flask server is mapped into to Next.js app under `/api/`.
18+
This project uses [Vercel Services](https://vercel.com/docs/services) to deploy a Next.js frontend and a Flask backend as part of a single Vercel project. Services are configured in `vercel.json` using the `experimentalServices` key:
1919

20-
This is implemented using [`next.config.js` rewrites](https://github.com/vercel/examples/blob/main/python/nextjs-flask/next.config.js) to map any request to `/api/:path*` to the Flask API, which is hosted in the `/api` folder.
20+
- **Frontend** — Next.js, mounted at `/`
21+
- **Backend** — Flask, mounted at `/api/python`
2122

22-
On localhost, the rewrite will be made to the `127.0.0.1:5328` port, which is where the Flask server is running.
23-
24-
In production, the Flask server is hosted as [Python serverless functions](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python) on Vercel.
23+
Vercel automatically routes requests to the correct service based on the URL path prefix. It also automatically injects environment variables based on the service name to easily route between services.
24+
To make requests to the Python backend service and keep preview environments in sync without having to hard-code urls, `NEXT_PUBLIC_BACKEND_URL` is automatically set.
2525

2626
## Demo
2727

@@ -53,26 +53,16 @@ yarn
5353
pnpm install
5454
```
5555

56-
Then, run the development server:
56+
Then, run both services locally:
5757

5858
```bash
59-
npm run dev
60-
# or
61-
yarn dev
62-
# or
63-
pnpm dev
59+
vercel dev -L
6460
```
6561

66-
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
67-
68-
The Flask server will be running on [http://127.0.0.1:5328](http://127.0.0.1:5328) – feel free to change the port in `package.json` (you'll also need to update it in `next.config.js`).
62+
The `-L` flag runs all services together locally without authenticating with the Vercel Cloud. Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
6963

7064
## Learn More
7165

72-
To learn more about Next.js, take a look at the following resources:
73-
66+
- [Vercel Services](https://vercel.com/docs/services) - learn how services work on Vercel.
7467
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
75-
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
76-
- [Flask Documentation](https://flask.palletsprojects.com/en/1.1.x/) - learn about Flask features and API.
77-
78-
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
68+
- [Flask Documentation](https://flask.palletsprojects.com/) - learn about Flask features and API.

python/nextjs-flask/api/index.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
from flask import Flask
1+
from flask import Flask, jsonify
2+
23
app = Flask(__name__)
34

4-
@app.route("/api/python")
5-
def hello_world():
6-
return "<p>Hello, World!</p>"
5+
6+
@app.route("/")
7+
def python_route():
8+
return jsonify(message="Hello from Flask!")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[project]
2+
name = "vercel-flask-starter"
3+
version = "0.1.0"
4+
requires-python = ">=3.12"
5+
dependencies = [
6+
"flask>=3.1,<4.0",
7+
"gunicorn~=23.0.0"
8+
]

python/nextjs-flask/app/page.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import Image from 'next/image'
2-
import Link from 'next/link'
2+
3+
const BACKEND_URL = process.env.NEXT_PUBLIC_BACKEND_URL
34

45
export default function Home() {
56
return (
67
<main className="flex min-h-screen flex-col items-center justify-between p-24">
78
<div className="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex">
89
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
910
Get started by editing&nbsp;
10-
<Link href="/api/python">
11+
<a href={`${BACKEND_URL}`}>
1112
<code className="font-mono font-bold">api/index.py</code>
12-
</Link>
13+
</a>
1314
</p>
1415
<div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:h-auto lg:w-auto lg:bg-none">
1516
<a

python/nextjs-flask/next.config.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
11
/** @type {import('next').NextConfig} */
2-
const nextConfig = {
3-
rewrites: async () => {
4-
return [
5-
{
6-
source: '/api/:path*',
7-
destination:
8-
process.env.NODE_ENV === 'development'
9-
? 'http://127.0.0.1:5328/api/:path*'
10-
: '/api/',
11-
},
12-
]
13-
},
14-
}
2+
const nextConfig = {}
153

164
module.exports = nextConfig

python/nextjs-flask/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"scripts": {
6-
"flask-dev": "FLASK_DEBUG=1 pip3 install -r requirements.txt && python3 -m flask --app api/index run -p 5328",
7-
"next-dev": "next dev",
8-
"dev": "concurrently \"pnpm run next-dev\" \"pnpm run flask-dev\"",
6+
"dev": "next dev",
97
"build": "next build",
108
"start": "next start",
119
"lint": "next lint"
@@ -15,7 +13,6 @@
1513
"@types/react": "18.2.6",
1614
"@types/react-dom": "18.2.4",
1715
"autoprefixer": "10.4.14",
18-
"concurrently": "^8.0.1",
1916
"eslint": "8.40.0",
2017
"eslint-config-next": "13.4.2",
2118
"next": "^16.0.10",

0 commit comments

Comments
 (0)