Turn a Python function into a web app
To turn a Python function into a web app with Reflex, collect inputs in a form, call the function from an event handler, and store the result in state. This example calculates a loan's monthly payment. It runs locally without a service or API key and keeps the calculation independent of the UI.
Create a blank app using the installation guide. Replace the app module with the code below and the two registration lines that follow it.
Form, function, and result
Interactive preview loads as you scroll.
app = rx.App()
app.add_page(payment_app)Run uv run reflex run and submit the form. At zero interest, 12,000 over one year returns 1,000 per month. An invalid input clears the previous result and displays an error. This is a simplified calculation, not a lending quote.
What happens on submit
Each input's name becomes a key in form_data. The browser sends the form submission to PaymentState.calculate; the backend parses and validates it, calls monthly_payment, and sends the changed state to the interface. The result and error views update from that state.
The calculation remains an ordinary Python function. You can call it from tests, a script, or another service independently of Reflex. This explicit form-to-handler mapping does not automatically infer an interface from a function signature.
Replace the calculation with your workflow
The same pattern can call a data transformation, a loaded model, or an external service. Keep input validation at the backend boundary even when the form has browser validation. Keep secrets out of state variables sent to the frontend.
For fast local calculations, a normal handler is sufficient. For a slow API call, use an async SDK and show a loading state. If other events need to run while work is pending, use a background event and hold the state lock only while reading or updating state. CPU-heavy inference needs a separate execution strategy; marking a function async does not make blocking computation asynchronous.
For a complete data-analysis workflow, try the pandas data app: upload a CSV, filter records, and download a grouped summary.
Continue with model and media interfaces, streaming chat, and self-hosting.