Getting Started with React as a Python User
All of the interactive applets in the Interactive Simulations Gallery are powered client-side by React components embedded inside Astro (see About the Stack for how the rest of the site fits together). If your background is mostly in Python, React clicks quickly once you map its primitives to familiar Python concepts.
Python Mental Model → React Equivalents
| Python Concept | React Equivalent | How It Maps |
|---|---|---|
Pure Functions (def) | Components | In Python, functions process data and return values. In React, components are functions that accept props (like kwargs) and return UI markup (JSX). |
| Mutable Variables | useState() | Updating a normal Python variable doesn’t automatically redraw a screen. useState() creates reactive variables that trigger a UI re-render whenever their setter is called. |
| f-strings / Jinja2 | JSX | JSX mixes HTML tags with JavaScript expressions inside { curly braces }, feeling very similar to Python f"{expr}" or Jinja2 templates. |
| Event Loops / Callbacks | useEffect() | Runs side effects—such as fetching live API data, setting timers, or driving animation frames—in response to component mount or state changes. |
Side-by-Side Example: A Stateful Counter
In Python, you might represent state in a small class:
class Counter:
def __init__(self, initial: int = 0):
self.count = initial
def increment(self) -> None:
self.count += 1
In React, you express the state and its rendered output together as a function component:
import React, { useState } from "react";
export default function InteractiveCounter({ initial = 0 }) {
const [count, setCount] = useState(initial);
return (
<button onClick={() => setCount(count + 1)}>
Count is {count}
</button>
);
}
Every time the button calls setCount(count + 1), React re-runs InteractiveCounter with the updated count and updates only the changed DOM text.
Quickstart for Python Developers
- Install the JavaScript Runtime: Install Node.js (LTS), which gives you both the
noderuntime andnpm/npx(JavaScript’s equivalent topipanduvx). - Scaffold a React App with Vite:
npm create vite@latest my-app -- --template react-ts cd my-app npm install npm run dev - Think in State, Not Manual DOM Edits: Define reactive values with
useStateand derive the UI from them instead of reaching in and mutating elements imperatively. - Pair with Python Backends: Keep heavy data work in Python (FastAPI, Flask, or DuckDB/Polars endpoints) and let a lightweight React frontend handle live interactivity and visualizations.
Reference: Installing Node.js, npm & React
1. Installing JavaScript (Node.js & npm)
- Official Downloads: nodejs.org — Download Node.js (LTS)
- macOS (Homebrew):
brew install node - Windows (winget):
winget install OpenJS.NodeJS.LTS - Linux / macOS (
nvm):nvm install --lts(nvm on GitHub)
2. Official React Learning Resources
- Official React Docs: react.dev — interactive tutorials and API reference.
- Try the live components: Browse the Interactive Simulations Gallery to see these patterns driving real-time canvas simulations, transit dashboards, and macroeconomic models.