Write React components using Python syntax. No Node.js required.
ReactXPy is a tool that lets you build modern web applications using Python syntax instead of JavaScript. If you know Python and want to create interactive websites with React, this tool is for you.
ReactXPy is a transpiler - a tool that converts code from one language to another. Specifically:
- You write: Python syntax with HTML-like tags (
.pysxfiles) - ReactXPy converts: Your code into JavaScript that runs in the browser
- Result: A working React application
If you love Python but need to build web apps, ReactXPy helps you:
- ✅ Use Python's clean, indentation-based syntax
- ✅ Avoid complex JavaScript build tools (webpack, babel, etc.)
- ✅ Write React components without learning JavaScript first
- ✅ Get started in minutes, not hours
- ✅ Work offline after initial setup
Example: Instead of writing complex JavaScript:
function Counter() {
const [count, setCount] = React.useState(0);
return React.createElement('button', {
onClick: () => setCount(count + 1)
}, count);
}You write clean Python:
def Counter():
count, setCount = useState(0)
return <button onClick={lambda: setCount(count + 1)}>{count}</button>Make sure you have Python 3.7 or higher installed:
python3 --versionOpen your terminal and run:
pip install reactxpyThis downloads ReactXPy and automatically compiles the C++ compiler on your machine.
Check that ReactXPy is installed correctly:
reactxpy --versionYou should see: reactxpy version 0.3.2
When you install ReactXPy, you get three command-line tools:
| Command | What It Does |
|---|---|
reactxpy |
Compiles individual .pysx files to JavaScript |
create-reactxpy-app |
Creates a new project with all necessary files |
build.py / dev.py |
Scripts in your project for building and development |
create-reactxpy-app my-appThis creates a folder called my-app with all the files you need.
cd my-apppython3 dev.pyGo to: http://localhost:3000
You should see your app running! The server automatically reloads when you save changes.
create-reactxpy-appsets up a complete project structuredev.pywatches your files and recompiles automatically- Your browser shows the live result instantly
- Edit files in the
src/folder and see changes immediately
ReactXPy files use the .pysx extension. They look like Python but let you write HTML-like tags directly in your code.
A component is a reusable piece of your user interface. Think of it like a function that returns what should appear on screen.
# Greeting.pysx
def Greeting(name):
return <div class="card">
<h2>Hello, {name}!</h2>
<p>Welcome to ReactXPy</p>
</div>Key Points:
- Use
defto define a component (just like Python functions) - Return HTML-like tags using
<and> - Use
{curly braces}to insert Python variables or expressions - Indentation matters (standard Python rules)
State is data that changes over time. For example: a counter value, form input, or toggle switch.
# Counter.pysx
def Counter():
# Create state: count starts at 0, setCount updates it
count, setCount = useState(0)
return <div>
<p>You clicked {count} times</p>
<button onClick={lambda: setCount(count + 1)}>
Click me
</button>
</div>How it works:
useState(0)creates a variablecountstarting at 0setCountis a function to updatecount- When you call
setCount, the component re-renders with the new value lambda:creates a small function (likedefbut inline)
Side effects are things that happen outside your component: API calls, modifying the document title, timers, etc.
def DocumentTitle():
title, setTitle = useState("My App")
# This runs when 'title' changes
useEffect(lambda: document.title = title, [title])
return <input
value={title}
onChange={lambda e: setTitle(e.target.value)}
/>How it works:
useEffecttakes two arguments: a function and a list of dependencies- The function runs after the component renders
- If dependencies change, the effect runs again
- Empty list
[]means "run once when component loads"
Show different content based on conditions:
def Dashboard(user):
return <div>
{/* Show admin panel only if user is admin */}
{user.isAdmin && <AdminPanel />}
{/* Show different view based on status */}
{user.status === "active"
? <ActiveView />
: <InactiveView />}
</div>Two approaches:
condition && <Component />- Shows component only if condition is truecondition ? <IfTrue /> : <IfFalse />- Shows one or the other
Display arrays of data:
def TodoList():
todos = [
{id: 1, text: "Learn ReactXPy"},
{id: 2, text: "Build something awesome"},
{id: 3, text: "Share with friends"}
]
return <ul>
{todos.map(lambda todo:
<li key={todo.id}>{todo.text}</li>
)}
</ul>Key Points:
map()transforms each item in a list- Always provide a unique
keyprop for list items keyhelps React track which items changed
my-app/
├── src/
│ ├── components/ # Reusable components
│ └── App.pysx # Main application
├── public/
│ ├── index.html # HTML entry point
│ └── style.css # Styles
├── runtime/
│ ├── react.development.js # React library
│ ├── react-dom.development.js # ReactDOM library
│ └── runtime.js # ReactXPy runtime
├── dist/ # Compiled JavaScript
├── build.py # Build script
└── dev.py # Development server
ReactXPy shows helpful error messages when something goes wrong. Each error includes:
- The filename and line number
- A visual pointer to the problem
- An error code (like RX203)
- A clear description of what went wrong
File "src/App.pysx", line 4
x =
^
RX203: SyntaxError: Incomplete assignment - missing right-hand side expression
What this means: You wrote x = but didn't provide a value after the equals sign.
| Code | What It Means | How to Fix |
|---|---|---|
| RX100 | String not closed | Add closing quote " or ' |
| RX101 | Invalid character | Remove or replace the strange character |
| RX200 | Import statement incomplete | Add the module name after import |
| RX201 | Function name missing | Add a name after def |
| RX203 | Assignment incomplete | Add a value after = |
| RX206 | HTML attribute empty | Add a value after attribute= |
| RX208 | If statement incomplete | Add a condition after if |
| RX209 | Lambda is empty | Add code after lambda: |
- Read the error message carefully - It tells you exactly where the problem is
- Check line numbers - The error shows which line has the issue
- Look at the
^pointer - It shows the exact position of the error - Start simple - If stuck, try removing code until it works, then add back slowly
Compile individual .pysx files:
reactxpy src/App.pysx -o dist/App.jsScaffold a new project:
create-reactxpy-app my-projectCompile all source files:
python3 build.pyStart development server with hot reload:
python3 dev.pyBehind the scenes, ReactXPy does several things to turn your Python code into a working website:
-
C++ Compiler (The Engine)
- Reads your
.pysxfiles - Understands both Python syntax and HTML-like tags
- Converts everything to JavaScript
- Runs extremely fast because it's written in C++
- Reads your
-
Python Tools (The Helpers)
create-reactxpy-app: Sets up new projectsdev.py: Watches your files and rebuilds automaticallybuild.py: Creates production-ready files
-
Runtime (The Bridge)
- A small JavaScript file that connects your code to React
- Handles Python-style hooks like
useStateanduseEffect
When you save a .pysx file, here's what happens:
Your Code (.pysx)
↓
[Lexer] - Breaks code into tokens
↓
[Parser] - Builds a tree structure (AST)
↓
[Generator] - Creates JavaScript code
↓
[Linker] - Combines all files
↓
Working Website!
Example Transformation:
Your Python code:
def App():
return <h1>Hello World</h1>Becomes JavaScript:
function App(props){
return React.createElement("h1", null, "Hello World")
}
export default App;ReactXPy generates standard ES6 JavaScript compatible with:
- Chrome 60+
- Firefox 55+
- Safari 12+
- Edge 79+
- JSX transpilation to React.createElement
- useState and useEffect hooks
- Hot reload development server
- Error reporting with codes
- Offline React files
- List comprehension syntax
- Async/await support
- Type hints integration
Contributions are welcome! Please feel free to submit issues or pull requests.
MIT License - See LICENSE for details.
For questions or issues:
- GitHub Issues: https://github.com/Anishkumar1928/reactxpy
- Documentation: docs/
**Built with ❤️ for Python and React developer @Anishkumar1928 **