Skip to content

Commit b312aaf

Browse files
authored
Merge pull request #34 from mongodb/python-readme
feat: Add python readme
2 parents a1e276f + 7904869 commit b312aaf

3 files changed

Lines changed: 187 additions & 129 deletions

File tree

mflix/README-PYTHON-FASTAPI.md

Lines changed: 185 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,207 @@
11
# Python FastAPI MongoDB Sample MFlix Application
22

3-
[TODO: Add intro]
3+
This is a full-stack movie browsing application built with Python FastAPI and Next.js, demonstrating MongoDB operations using the `sample_mflix` dataset. The application showcases CRUD operations, aggregations, and MongoDB Search using the PyMongo driver.
4+
5+
## Project Structure
46

57
```
68
├── README.md
7-
├── client/ # Next.js frontend
8-
└── server/ # Python FastAPI backend
9+
├── client/ # Next.js frontend (TypeScript)
10+
└── server/ # Python FastAPI backend
11+
├── src/
12+
├── tests/
13+
├── .env.example
14+
├── main.py
15+
├── pytest.ini
16+
├── requirements.in
17+
└── requirements.txt
918
```
1019

20+
## Prerequisites
21+
22+
- **Python 3.10** to **Python 3.13**
23+
- **Node.js 20** or higher
24+
- **MongoDB Atlas cluster or local deployment** with the `sample_mflix` dataset loaded
25+
- [Load sample data](https://www.mongodb.com/docs/atlas/sample-data/)
26+
- **pip** for Python package management
27+
- **Voyage AI API key** (For MongoDB Vector Search)
28+
- [Get a Voyage AI API key](https://www.voyageai.com/)
29+
1130
## Getting Started
1231

13-
[TODO: Add getting started instructions explaining how to set connection string, start server, start client, etc.]
32+
### 1. Configure the Backend
33+
34+
Navigate to the Python FastAPI server directory:
35+
36+
```bash
37+
cd server/
38+
```
39+
40+
Create a `.env` file from the example:
41+
42+
```bash
43+
cp .env.example .env
44+
```
45+
46+
Edit the `.env` file and set your MongoDB connection string:
47+
48+
```env
49+
# MongoDB Configuration
50+
MONGO_URI=mongodb+srv://<username>:<password>@<cluster>.mongodb.net/sample_mflix?retryWrites=true&w=majority
51+
MONGO_DB=sample_mflix
52+
53+
# Voyage AI Configuration
54+
# API key for Voyage AI embedding model (required for Vector Search)
55+
VOYAGE_API_KEY=your_voyage_api_key
56+
57+
# CORS Configuration
58+
# Comma-separated list of allowed origins for CORS
59+
CORS_ORIGINS=http://localhost:3000,http://localhost:8000
60+
```
61+
62+
**Note:** Replace `username`, `password`, and `cluster` with your actual MongoDB Atlas
63+
credentials.
64+
65+
Make a virtual environment:
66+
67+
```bash
68+
python -m venv .venv
69+
```
70+
71+
Activate the virtual environment:
72+
73+
```bash
74+
source .venv/bin/activate
75+
```
76+
77+
Install Python dependencies:
78+
79+
```bash
80+
pip install -r requirements.txt
81+
```
82+
83+
### 2. Start the Backend Server
84+
85+
From the `server/` directory, run:
86+
87+
```bash
88+
fastapi dev main.py --reload
89+
```
90+
91+
The server will start on `http://localhost:8000`. You can verify it's running by visiting:
92+
- API root: http://localhost:8000/api/movies
93+
- API documentation (Swagger UI): http://localhost:8000/docs
94+
95+
### 3. Configure and Start the Frontend
96+
97+
Open a new terminal and navigate to the client directory:
98+
99+
```bash
100+
cd client
101+
```
102+
103+
Install dependencies:
104+
105+
```bash
106+
npm install
107+
```
108+
109+
Start the development server:
110+
111+
```bash
112+
npm run dev
113+
```
114+
115+
The Next.js application will start on `http://localhost:3000`.
116+
117+
### 4. Access the Application
118+
119+
Open your browser and navigate to:
120+
- **Frontend:** http://localhost:3000
121+
- **Backend API:** http://localhost:8000
122+
- **API Documentation:** http://localhost:8000/docs
123+
124+
## Features
125+
126+
- **Browse Movies:** View a paginated list of movies from the sample_mflix dataset
127+
- **Search:** Full-text search using MongoDB Search
128+
- **Vector Search:** Semantic search using MongoDB Vector Search with Voyage AI embeddings
129+
- **Filter:** Filter movies by genre, year, rating, and more
130+
- **Movie Details:** View detailed information about each movie
131+
- **Aggregations:** Complex data aggregations and analytics
132+
133+
## Development
134+
135+
### Backend Development
136+
137+
The Python FastAPI backend uses:
138+
- **FastAPI** for REST API framework
139+
- **PyMongo** for database operations
140+
- **Voyage AI** for vector embeddings
141+
- **fastapi** for ASGI server
142+
143+
To run all tests:
144+
145+
```bash
146+
cd server/
147+
source .venv/bin/activate # or `.venv\Scripts\activate` on Windows
148+
pytest tests/ -v
149+
```
150+
151+
### Frontend Development
152+
153+
The Next.js frontend uses:
154+
- **React 19** with TypeScript
155+
- **Next.js 16** with App Router
156+
- **Turbopack** for fast development builds
157+
158+
#### Development Mode
159+
160+
For active development with hot reloading and fast refresh:
161+
162+
```bash
163+
cd client
164+
npm run dev
165+
```
166+
167+
This starts the development server on `http://localhost:3000` with Turbopack for fast rebuilds.
168+
169+
#### Production Build
170+
171+
To create an optimized production build and run it:
172+
173+
```bash
174+
cd client
175+
npm run build # Creates optimized production build
176+
npm start # Starts production server
177+
```
178+
179+
The production build:
180+
- Minifies and optimizes JavaScript and CSS
181+
- Optimizes images and assets
182+
- Generates static pages where possible
183+
- Provides better performance for end users
184+
185+
#### Linting
186+
187+
To check code quality:
188+
189+
```bash
190+
cd client
191+
npm run lint
192+
```
14193

15194
## Issues
16195

17196
If you have problems running the sample app, please check the following:
18197

19198
- [ ] Verify that you have set your MongoDB connection string in the `.env` file.
199+
- [ ] Verify that you have created and activated a Python `.venv` on Python v3.10 through v3.13.
20200
- [ ] Verify that you have started the Python FastAPI server.
21201
- [ ] Verify that you have started the Next.js client.
22202
- [ ] Verify that you have no firewalls blocking access to the server or client ports.
23203

24204
If you have verified the above and still have issues, please
25205
[open an issue](https://github.com/mongodb/docs-sample-apps/issues/new/choose)
26206
on the source repository `mongodb/docs-sample-apps`.
207+
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# MongoDB Configuration
2-
MONGO_URI="mongodb://localhost:27017"
2+
MONGO_URI="mongodb+srv://<username>:<password>@<cluster>.mongodb.net/sample_mflix?retryWrites=true&w=majority"
33
MONGO_DB="sample_mflix"
44

55
# Voyage AI Configuration
@@ -8,4 +8,4 @@ VOYAGE_API_KEY=your_voyage_api_key
88

99
# CORS Configuration
1010
# Comma-separated list of allowed origins for CORS
11-
CORS_ORIGINS="http://localhost:3000,http://localhost:3001"
11+
CORS_ORIGINS="http://localhost:3000,http://localhost:8000"

mflix/server/python-fastapi/README_OLD_REMOVE_ME.md

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)