Skip to main content

A Python web framework inspired by Next.js with file-based routing, SSR, and SSG

Project description

NextPy Framework

A Python web framework inspired by Next.js. Build modern web applications with file-based routing, server-side rendering (SSR), static site generation (SSG), and more.

Features

  • File-based Routing - Pages in pages/ become routes automatically
  • Dynamic Routes - [slug].py creates dynamic URL segments
  • Server-Side Rendering - get_server_side_props fetches data per request
  • Static Site Generation - get_static_props builds pages at compile time
  • API Routes - Create API endpoints in pages/api/
  • HTMX Integration - SPA-like navigation without heavy JavaScript
  • Jinja2 Templates - Powerful templating with layout inheritance
  • Hot Reload - Instant updates on file changes during development
  • Debug Panel - Built-in error overlay for development
  • Components Library - Pre-built UI components (buttons, cards, forms)

Installation

Via pip

pip install nextpy-framework

Development Install (Editable)

git clone https://github.com/nextpy/nextpy-framework.git
cd nextpy-framework
pip install -e .

This installs NextPy in editable mode, so changes to the source code take effect immediately.

Quick Start

1. Create a new project

nextpy create my-app
cd my-app

2. Install dependencies

pip install -e .
# or
pip install -r requirements.txt

3. Run development server

nextpy dev

Visit http://localhost:5000 - your app starts with hot reload enabled!

4. Create a page

# pages/hello.py

def get_template():
    return "hello.html"

async def get_server_side_props(context):
    return {
        "props": {
            "message": "Hello, World!"
        }
    }

5. Create a template

<!-- templates/hello.html -->
{% extends "_base.html" %}

{% block content %}
<h1>{{ message }}</h1>
{% endblock %}

Visit http://localhost:5000/hello - page updates instantly as you edit!

Project Structure

my-app/
├── pages/                 # Your pages (file-based routing)
│   ├── index.py          # Home page (/)
│   ├── about.py          # About page (/about)
│   ├── blog/
│   │   ├── index.py      # Blog listing (/blog)
│   │   └── [slug].py     # Dynamic post (/blog/:slug)
│   └── api/
│       └── posts.py      # API route (/api/posts)
├── templates/            # Jinja2 templates
│   ├── _base.html        # Base layout
│   ├── components/       # Reusable components
│   │   ├── button.html
│   │   ├── card.html
│   │   └── form.html
│   └── index.html        # Page templates
├── public/               # Static files (CSS, JS, images)
├── main.py              # Entry point
└── requirements.txt     # Python dependencies

Package Architecture

NextPy is fully self-contained and tightly integrated:

Core Components

Module Purpose
nextpy/server/app.py Creates FastAPI application with routing
nextpy/core/router.py Scans pages/, templates/, APIs dynamically
nextpy/core/renderer.py SSR with Jinja2 + data fetching
nextpy/core/builder.py SSG with static HTML generation
nextpy/cli.py CLI scaffolding & commands
nextpy/components/ Button, card, form components
nextpy/middleware.py Debug panel, error handling

Setup Files

  • pyproject.toml - Package metadata & dependencies
  • setup.cfg - Build configuration
  • MANIFEST.in - Include non-Python files
  • LICENSE - MIT License
  • README.md - Documentation

How It All Works Together

  1. CLI (nextpy create myapp) scaffolds project structure
  2. Router scans pages/ and maps files to routes
  3. Renderer executes get_server_side_props and renders with Jinja2
  4. FastAPI serves pages and API routes
  5. Builder generates static files for SSG
  6. Watchdog detects file changes → automatic hot reload
  7. Debug Middleware shows errors in development

Installable Features

✅ Works with pip install -e . for local development
✅ All files tightly integrated (no external dependencies)
✅ Auto-discovery of pages, templates, APIs
✅ CLI includes scaffolding with example structure
✅ Hot reload watches pages/, templates/, nextpy/ directories
✅ Debug panel shows errors in browser

Data Fetching

Server-Side Rendering (SSR)

Fetch data on every request:

async def get_server_side_props(context):
    # Runs on every request
    data = await fetch_from_api()
    return {"props": {"data": data}}

Static Site Generation (SSG)

Fetch data at build time:

async def get_static_props(context):
    # Runs at build time
    data = await fetch_from_api()
    return {"props": {"data": data}}

async def get_static_paths():
    # For dynamic routes - define which paths to pre-render
    return {
        "paths": [
            {"params": {"slug": "post-1"}},
            {"params": {"slug": "post-2"}},
        ]
    }

API Routes

Create API endpoints in pages/api/:

# pages/api/users.py

async def get(request):
    return {"users": [...]}

async def post(request):
    data = await request.json()
    return {"created": data}

async def put(request):
    data = await request.json()
    return {"updated": data}

async def delete(request):
    return {"deleted": True}

Components

Use pre-built components via Jinja2 macros:

{% from "components/button.html" import button %}
{% from "components/card.html" import card %}
{% from "components/form.html" import input, textarea, select %}

{{ button("Click Me", "/action", "primary") }}
{{ card(title="My Card", content="Description", link="/details") }}
{{ input("name", "Full Name", "text", "John Doe", True) }}

Layouts & Nesting

Create nested layouts with template inheritance:

<!-- templates/_base.html (root) -->
<html>
  <body>
    <nav>...</nav>
    {% block content %}{% endblock %}
  </body>
</html>

<!-- templates/_blog-layout.html (extends base) -->
{% extends "_base.html" %}
{% block content %}
  <div class="blog-container">
    {% block blog_content %}{% endblock %}
  </div>
{% endblock %}

<!-- templates/blog/[slug].html (extends blog layout) -->
{% extends "_blog-layout.html" %}
{% block blog_content %}
  <h1>{{ title }}</h1>
{% endblock %}

CLI Commands

nextpy dev      # Start development server with hot reload & debug panel
nextpy build    # Generate static files to out/
nextpy start    # Start production server
nextpy routes   # Display all registered routes
nextpy create   # Create new project

Development Features

🔥 Hot Reload - File changes trigger instant server restart
🐛 Debug Panel - Error overlay with stack traces
📍 Route Listing - View all registered routes
🎯 Auto-discovery - Pages, templates, APIs loaded automatically

Tech Stack

  • FastAPI - High-performance async web framework
  • Uvicorn - Lightning-fast ASGI server
  • Jinja2 - Powerful templating engine with inheritance
  • Pydantic - Type-safe data validation
  • Watchdog - File system monitoring for hot reload
  • Click - Elegant CLI framework
  • HTMX - Modern browser features without JavaScript complexity
  • Tailwind CSS - Utility-first CSS framework

Production Deployment

# Build static files
nextpy build

# Start production server
nextpy start

For hosting:

  • Heroku: git push heroku main
  • Vercel: Static hosting (use nextpy build)
  • Docker: Dockerfile ready
  • VPS: Run nextpy start with process manager (systemd, supervisord)

Contributing

Contributions welcome! NextPy is MIT licensed.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nextpy_framework-1.1.0.tar.gz (50.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nextpy_framework-1.1.0-py3-none-any.whl (63.0 kB view details)

Uploaded Python 3

File details

Details for the file nextpy_framework-1.1.0.tar.gz.

File metadata

  • Download URL: nextpy_framework-1.1.0.tar.gz
  • Upload date:
  • Size: 50.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for nextpy_framework-1.1.0.tar.gz
Algorithm Hash digest
SHA256 c05d162eb8ce098ba5d7d8964ee3ea16b31632a5b93a50bb3a6aacad6f4676c7
MD5 f1c6ee97b450f9f020936eb5dd836c9d
BLAKE2b-256 6e3f8364b70ed7038e59ad069ec579fece4390d166c2db719818b127b888ce74

See more details on using hashes here.

File details

Details for the file nextpy_framework-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for nextpy_framework-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 61f7f185f610400308562ed05a591b443168291d0b96fcf4305627178d7c9419
MD5 b7bb041ac4ad835b6c1afb54a328cf23
BLAKE2b-256 b9ecbdd673718308bdddbe07eae4080c91c047c928214c43f1ae90df593709d9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page