Make a website with Python
Go from an empty folder to a hosted site, with every page written as a Python function.
Can you make a website with Python? Yes, and there are two ways to do it. You can run Python on a server with a web framework, so your code builds each page when someone asks for it. Or you can run Python once on your own machine, have it write the HTML files ahead of time, and upload those.
This guide explains how to choose, then walks through the second route from an empty folder to a site you can host.
Two ways to build a website with Python
A framework, running on a server
Django, Flask and FastAPI all work this way. A Python process stays running, receives each request and decides what to send back. You need this when pages depend on the visitor: logins, a shopping basket, search, anything that reads or writes a database while people use the site.
The price is a server to run, secure and pay for. If that's the kind of site you're making, Python web frameworks compared will help you pick one.
Python that writes the HTML ahead of time
A portfolio, a blog, documentation or a site for a small business shows everyone the same pages. Nothing needs to be worked out per visit, so you can generate every page in advance. The result is a folder of plain HTML and CSS that any static host will serve, usually for free, with no Python running in production.
That's what a static site generator does, and it's the simpler option whenever it fits. The rest of this guide uses Nitro CLI, where each page is a Python function and there's no template language to learn.
Install Nitro
You need Python 3 and a terminal. Create a folder to work in, set up a virtual environment and install Nitro into it:
python -m venv .venv
source .venv/bin/activate
pip install nitro-cli
On Windows, the second line is .venv\Scripts\activate.
Create a project
nitro new my-site
cd my-site
nitro new creates the folder, installs what's listed in requirements.txt and starts a git repository. Pass --no-install or --no-git to skip either step.
Here's what you get:
my-site/
nitro.config.py # site name, base URL, build settings
requirements.txt
README.md
.gitignore
src/
pages/
index.py # the home page
components/
header.py # example components
footer.py
data/
site.json # example data
styles/
main.css
The rule that matters most: every Python file in src/pages/ becomes a page, and its path becomes its URL. src/pages/index.py is the home page, and src/pages/about.py would be /about.
Start the dev server
nitro dev
Open http://localhost:3000 and you'll see the welcome page that ships with a new project. Leave this running. Each time you save a file, the site rebuilds and the browser reloads.
Edit the home page
A page is a file with a render() function that returns a Page. The HTML inside it is built from Nitro UI classes, one per HTML tag.
Replace everything in src/pages/index.py with this:
from nitro import Page
from nitro_ui import HTML, Head, Body, Title, Meta, Link, H1, Paragraph
def render():
return Page(
title="Home",
content=HTML(
Head(
Meta(charset="utf-8"),
Meta(name="viewport", content="width=device-width, initial-scale=1"),
Title("Home - My Site"),
Link(rel="stylesheet", href="/assets/styles/main.css"),
),
Body(
H1("Hello, I'm Sam"),
Paragraph("I write Python and take too many photos."),
),
lang="en",
),
)
Save, and the browser shows your heading and paragraph. Children go in as positional arguments and HTML attributes as keyword arguments, so Link(rel="stylesheet", href="...") becomes a <link> tag. Text is escaped for you.
The stylesheet path is worth a note. Files in src/styles/ are served from /assets/styles/. The main.css in a new project is written for the welcome page, so swap its contents for your own CSS when you're ready.
Share a layout between pages
A second page would need the same Head, the same navigation and the same footer. In a template language you'd reach for a base template. Here it's a function.
Create src/components/layout.py:
from nitro_ui import (
HTML, Head, Body, Title, Meta, Link, Header, Nav, Main, Footer, Anchor, Paragraph,
)
def layout(title, *children):
return HTML(
Head(
Meta(charset="utf-8"),
Meta(name="viewport", content="width=device-width, initial-scale=1"),
Title(f"{title} - My Site"),
Link(rel="stylesheet", href="/assets/styles/main.css"),
),
Body(
Header(
Nav(
Anchor("Home", href="/"),
Anchor("About", href="/about"),
)
),
Main(*children),
Footer(Paragraph("Built with Python.")),
),
lang="en",
)
Whatever you pass after the title ends up inside <main>. Nitro puts src/ on the import path while it builds, so pages can import this as components.layout.
Add a second page
Create src/pages/about.py:
from nitro import Page
from nitro_ui import H1, Paragraph
from components.layout import layout
def render():
return Page(
title="About",
content=layout(
"About",
H1("About"),
Paragraph("This site is a handful of Python files."),
),
)
Visit http://localhost:3000/about. No route to register and no config to touch: the file exists, so the page exists.
Build a list from data
Hand-writing a list of projects gets old quickly, and this is where making a website with Python pays off. Put the data in a file and loop over it.
Create src/data/projects.json:
[
{
"name": "Weather bot",
"url": "https://example.com/weather",
"summary": "Posts the forecast to our team chat every morning."
},
{
"name": "Photo sorter",
"url": "https://example.com/photos",
"summary": "Renames holiday photos by date and place."
}
]
Now rewrite src/pages/index.py to use the layout and read that file:
import json
from pathlib import Path
from nitro import Page
from nitro_ui import H1, H2, Paragraph, UnorderedList, ListItem, Anchor
from components.layout import layout
DATA = Path(__file__).resolve().parent.parent / "data" / "projects.json"
def project_item(project):
return ListItem(
Anchor(project["name"], href=project["url"]),
" - ",
project["summary"],
)
def render():
projects = json.loads(DATA.read_text(encoding="utf-8"))
return Page(
title="Home",
content=layout(
"Home",
H1("Hello, I'm Sam"),
Paragraph("I write Python and take too many photos."),
H2("Projects"),
UnorderedList(*[project_item(project) for project in projects]),
),
)
project_item is an ordinary function that returns one <li>, and the list comprehension calls it once per project. Add a third entry to the JSON file and it appears on the page with no change to the Python. The data doesn't have to be JSON either. Anything Python can read at build time works: a CSV, a folder of Markdown files, an API.
Build the site
Stop the dev server with Ctrl+C, then:
nitro build
Everything is written to build/:
build/
index.html
about.html
assets/styles/main.3d10a61c76f3.css
sitemap.xml
robots.txt
manifest.json
The HTML and CSS are minified, and the stylesheet gets a hash in its filename so browsers pick up changes. Your Link tags are rewritten to match, so keep writing /assets/styles/main.css in the source. To check the result the way a visitor would see it, run nitro preview.
Before you publish, open nitro.config.py and set base_url to your real domain. The sitemap is built from it.
Put it online
The build/ folder is the whole website. Upload it to any static host and you're done; there's no Python to install on the server and no process to keep alive. Cloudflare Pages, Netlify, Vercel and GitHub Pages all work, and nitro deploy can push to the first three for you. Deploying your site covers each host, including how to rebuild on every push.
Where to go next
- Turn a folder of posts into pages with Build a blog from Markdown.
- Add utility classes with Using Tailwind CSS.
- If you find you need logins or a database after all, go back to the framework comparison.