A comprehensive learning project for integrating Python backends with Next.js frontends. Perfect for developers with no Python experience who want to learn from beginner to advanced level.
- Project Overview
- Tech Stack
- Project Structure
- Quick Start
- Python Learning Path
- Integration Examples
- Testing
- Troubleshooting
- Resources
This project serves as a hands-on learning environment for:
- Python fundamentals from beginner to advanced
- Backend development with FastAPI
- Frontend-backend integration patterns
- Full-stack development workflows
- Testing practices for both Python and TypeScript
Architecture Philosophy: Keep Python and Next.js as separate, loosely-coupled services that communicate via HTTP APIs.
- Next.js 16 - React framework with App Router
- React 19 - Latest React features
- TypeScript - Type-safe JavaScript
- TailwindCSS 4 - Utility-first CSS
- Python 3.10+ - Core language
- FastAPI - Modern web framework
- Uvicorn - ASGI server
- pytest - Testing framework
solar-python-demo/
βββ my-app/ # Next.js frontend (current folder)
β βββ app/
β β βββ page.tsx # Main page - edit here for UI
β β βββ layout.tsx # Root layout with global setup
β β βββ globals.css # Global styles
β βββ public/ # Static assets
β βββ package.json # Node dependencies & scripts
β βββ next.config.ts # Next.js configuration
β
βββ python-api/ # Python backend (to be created)
βββ main.py # FastAPI application
βββ requirements.txt # Python dependencies
βββ tests/ # pytest tests
βββ examples/ # Learning examples
β βββ basics/ # Python fundamentals
β βββ data_processing/ # pandas, numpy examples
β βββ ml_models/ # Machine learning demos
βββ .venv/ # Virtual environment (created locally)
# Navigate to the Next.js app
cd my-app
# Install dependencies
npm install
# Start development server
npm run devFrontend will be available at http://localhost:3000
# Go back to project root
cd ..
# Create Python service directory
mkdir python-api
cd python-api
# Create virtual environment (Windows)
python -m venv .venv
.venv\Scripts\activate
# Create virtual environment (macOS/Linux)
python3 -m venv .venv
source .venv/bin/activate
# Install FastAPI and dependencies
pip install fastapi uvicorn pytest httpx
# Save dependencies
pip freeze > requirements.txtCreate python-api/main.py:
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(title="Solar Python API", version="1.0.0")
# Enable CORS for Next.js frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
def root():
return {"message": "Welcome to Solar Python API!"}
@app.get("/health")
def health_check():
return {"status": "healthy", "service": "python-api"}
@app.get("/api/calculate")
def calculate(x: int, y: int, operation: str = "add"):
"""Simple calculator endpoint"""
operations = {
"add": x + y,
"subtract": x - y,
"multiply": x * y,
"divide": x / y if y != 0 else None
}
result = operations.get(operation)
return {"x": x, "y": y, "operation": operation, "result": result}Start the Python server:
# Make sure you're in python-api/ with venv activated
uvicorn main:app --reload --port 8000Backend will be available at http://localhost:8000
Test it: Open http://localhost:8000/docs to see interactive API documentation!
Update my-app/app/page.tsx to fetch from Python API:
"use client";
import { useEffect, useState } from "react";
import Image from "next/image";
interface HealthResponse {
status: string;
service: string;
}
export default function Home() {
const [health, setHealth] = useState<HealthResponse | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
fetch("http://localhost:8000/health")
.then((res) => res.json())
.then((data) => {
setHealth(data);
setLoading(false);
})
.catch((err) => {
setError(err.message);
setLoading(false);
});
}, []);
return (
<div className="flex min-h-screen items-center justify-center bg-zinc-50 dark:bg-black">
<main className="flex w-full max-w-3xl flex-col gap-8 p-8">
<Image
className="dark:invert"
src="/next.svg"
alt="Next.js logo"
width={100}
height={20}
priority
/>
<h1 className="text-4xl font-bold">Solar Python Demo</h1>
<div className="rounded-lg border border-zinc-200 p-6 dark:border-zinc-800">
<h2 className="text-xl font-semibold mb-4">Python API Status</h2>
{loading && <p>Connecting to Python backend...</p>}
{error && <p className="text-red-500">Error: {error}</p>}
{health && (
<div className="space-y-2">
<p className="text-green-600 dark:text-green-400">
β Connected successfully!
</p>
<p>
Status: <code>{health.status}</code>
</p>
<p>
Service: <code>{health.service}</code>
</p>
</div>
)}
</div>
</main>
</div>
);
}Restart your frontend (npm run dev) and you should see the Python API status!
Create python-api/examples/basics/01_variables.py:
# Variables and data types
name = "Solar Python"
age = 1
is_learning = True
price = 29.99
# Lists
frameworks = ["FastAPI", "Django", "Flask"]
# Dictionaries
user = {"name": "Developer", "role": "Full Stack"}
# Functions
def greet(name: str) -> str:
return f"Hello, {name}!"
if __name__ == "__main__":
print(greet("World"))Run it: python examples/basics/01_variables.py
Learning Goals:
- Variables and data types
- Control flow (if/else, loops)
- Functions and parameters
- Lists, dictionaries, sets
- File I/O basics
Create python-api/examples/fastapi_basics/simple_crud.py:
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI()
# Data model
class Item(BaseModel):
id: int
name: str
price: float
# In-memory database
items_db = []
@app.post("/items")
def create_item(item: Item):
items_db.append(item)
return {"message": "Item created", "item": item}
@app.get("/items")
def list_items():
return {"items": items_db}
@app.get("/items/{item_id}")
def get_item(item_id: int):
for item in items_db:
if item.id == item_id:
return item
raise HTTPException(status_code=404, detail="Item not found")Learning Goals:
- REST API concepts
- Request/response models with Pydantic
- Path and query parameters
- HTTP methods (GET, POST, PUT, DELETE)
- Error handling
Create python-api/examples/data_processing/pandas_demo.py:
import pandas as pd
from fastapi import FastAPI
app = FastAPI()
@app.get("/api/data/summary")
def get_data_summary():
# Create sample data
data = {
"month": ["Jan", "Feb", "Mar", "Apr"],
"sales": [15000, 18000, 22000, 19000],
"expenses": [12000, 13000, 14000, 13500]
}
df = pd.DataFrame(data)
# Calculate profit
df["profit"] = df["sales"] - df["expenses"]
return {
"data": df.to_dict(orient="records"),
"total_sales": int(df["sales"].sum()),
"total_profit": int(df["profit"].sum()),
"avg_sales": float(df["sales"].mean())
}Install pandas: pip install pandas
Learning Goals:
- pandas DataFrames
- Data manipulation and analysis
- Working with CSV/JSON data
- Aggregations and statistics
from fastapi import FastAPI
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# SQLite database setup
DATABASE_URL = "sqlite:///./solar_demo.db"
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(bind=engine)
Base = declarative_base()
# Define model
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String)
email = Column(String, unique=True)
Base.metadata.create_all(bind=engine)Learning Goals:
- SQLAlchemy ORM
- Database models
- CRUD operations
- Relationships and joins
- Async/Await: Non-blocking operations
- Background Tasks: Long-running processes
- WebSockets: Real-time communication
- Caching: Redis integration
- Authentication: JWT tokens
- Machine Learning: scikit-learn, TensorFlow basics
Backend (python-api/main.py):
@app.get("/api/calculate")
def calculate(x: float, y: float, op: str):
ops = {"add": x+y, "sub": x-y, "mul": x*y, "div": x/y}
return {"result": ops.get(op, 0)}Frontend (my-app/app/calculator/page.tsx):
const response = await fetch(
`http://localhost:8000/api/calculate?x=10&y=5&op=add`
);
const data = await response.json();
console.log(data.result); // 15Backend: Return chart data
@app.get("/api/chart-data")
def chart_data():
return {
"labels": ["Q1", "Q2", "Q3", "Q4"],
"values": [45, 60, 80, 95]
}Frontend: Use with Chart.js or Recharts
const { labels, values } = await fetch("/api/chart-data").then((r) => r.json());
// Render with your chart libraryBackend: Accept file uploads
from fastapi import File, UploadFile
@app.post("/api/upload")
async def upload_file(file: UploadFile = File(...)):
contents = await file.read()
# Process the file
return {"filename": file.filename, "size": len(contents)}Create python-api/tests/test_main.py:
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_health_check():
response = client.get("/health")
assert response.status_code == 200
assert response.json()["status"] == "healthy"
def test_calculate():
response = client.get("/api/calculate?x=10&y=5&operation=add")
assert response.status_code == 200
assert response.json()["result"] == 15Run tests:
cd python-api
pytest tests/ -vInstall testing libraries:
npm install -D @testing-library/react @testing-library/jest-dom jestError: uvicorn: command not found
- Solution: Activate virtual environment:
.venv\Scripts\activate
Error: ModuleNotFoundError: No module named 'fastapi'
- Solution: Install dependencies:
pip install -r requirements.txt
Error: Access to fetch blocked by CORS policy
- Solution: Ensure CORS middleware is configured in
main.py:
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000"],
allow_methods=["*"],
allow_headers=["*"],
)Error: fetch failed: connection refused
- Solution: Make sure Python API is running on port 8000:
uvicorn main:app --reload --port 8000
Error: Address already in use
- Solution: Change port:
uvicorn main:app --reload --port 8001
- Official Python Tutorial
- Real Python - Comprehensive tutorials
- Python for Everybody - Free course
- Complete the Quick Start - Get both services running
- Work through Level 1 Python examples - Build fundamentals
- Create your first custom endpoint - Practice API design
- Build a small project - Todo list, calculator, or data dashboard
- Explore advanced topics - Database, authentication, real-time features
npm run dev # Start development server
npm run build # Build for production
npm run start # Start production server
npm run lint # Run ESLint# Activate virtual environment
.venv\Scripts\activate # Windows
source .venv/bin/activate # macOS/Linux
# Run server
uvicorn main:app --reload --port 8000
# Run tests
pytest tests/ -v
# Check code style
pip install black flake8
black .
flake8 .This is a learning project! Feel free to:
- Add more Python examples
- Create new integration patterns
- Improve documentation
- Share your learning journey
This project is for educational purposes.
Happy Learning! π Start with the Quick Start guide and work your way through the Python Learning Path.