CLI and project workflows
Reflex gives developers one command surface for creating a project, preparing its frontend, compiling Python app definitions, running the full stack, and producing deployable output. The workflows share the project configuration and generated frontend workspace so that local development and export use the same app description. For the wider workflow map, see Developer workflows.
Command surface
The command surface is the developer-facing entry point for project work. The reflex executable invokes the Click group in reflex/reflex.py, which owns common logging and connects the main commands with hosting, database, and helper-script command groups.
Command groups
The top-level commands cover the normal app lifecycle. Optional hosting commands use lazy loading, so the core CLI can start without importing the hosting package. The command resolves its implementation only when the user needs its help or runs it. If a hosting command is used without that package, the CLI reports the package that must be installed.
| Command | Workflow purpose |
|---|---|
reflex init | Create or refresh a project and its frontend workspace. |
reflex run | Compile and serve the frontend, backend, or both. |
reflex compile | Validate and compile the app without starting servers. |
reflex export | Build frontend and backend artifacts for separate deployment. |
reflex deploy, reflex cloud | Send an app package to a hosting service. See Deployment and hosting. |
reflex db | Create, inspect, and apply database schema migrations. |
reflex script | Run project helper scripts. |
reflex component | Report that the removed component CLI has moved to direct React wrapping or the component template. See Extensions and custom components. |
reflex login, reflex logout | Manage access to the hosting service. |
reflex rename | Change the app name in the project configuration and source layout. |
The shared --loglevel and --json options make command output useful both to a person at a terminal and to an automated tool. The command must still run from the project root for workflows that need rxconfig.py.
CLI startup can check the installed Reflex package against PyPI without making every command wait on the network. The check stores package-specific timestamps in .web/reflex.json; successful checks stay fresh for one day, while failed attempts use a one-hour cooldown. This keeps offline command paths responsive and still allows update notices.
The old reflex component command remains as a migration message rather than a project-scaffolding workflow. It accepts the old command shape so users receive guidance about the replacement instead of a generic unknown-command error.
References: pyproject.toml• L75-76 reflex/reflex.py• L33-256 reflex/reflex.py• L1013-1211 reflex/…/cli_options.py• L28-75 reflex/…/prerequisites.py• L86-186
Project scaffolding
Project scaffolding turns an empty directory into a Python app that Reflex can find and compile. It creates the app configuration, a Python package with an initial page, and the generated frontend workspace that later commands refresh as the app changes.
Initialization flow
reflex init uses the current directory name by default and checks that the name is a valid Python package name. It then prepares the frontend toolchain and selects either the built-in blank app, a supported remote template, or the AI builder redirect.
The initialization flow is:
flowchart TD
A[Project directory] --> B[Validate app name]
B --> C[Prepare frontend runtime and packages]
C --> D[Write rxconfig.py and app package]
D --> E[Create .web workspace]
E --> F[Write ignore and agent guidance files]
F --> G[Ready for reflex run]
The generated app exposes an rx.App instance and a first page. If the directory already has rxconfig.py, initialization acts as re-initialization instead of replacing the app source. The frontend workspace is refreshed while persisted dependency data and the project identity are retained. Initialization also writes AGENTS.md by default when agent guidance is enabled, and gives the developer the matching dependency setup step for projects with or without pyproject.toml.
References: reflex/reflex.py• L270-363 reflex/…/prerequisites.py• L603-633 reflex/…/prerequisites.py• L807-819 reflex/…/templates.py• L28-112 reflex/…/templates.py• L273-333 reflex/…/frontend_skeleton.py• L451-485 packages/…/blank.py• L1-36
Project layout
The project root is the source of truth for Python configuration and app code. The .web directory is generated frontend state. It contains the browser project, its build output, and the frontend-side data needed to connect to the Python backend.
| Area | Role in the workflow |
|---|---|
| rxconfig.py | Names the app and supplies project-wide settings. |
<app_name>/ | Holds the Python app module and related source. |
.web/ | Holds the generated frontend project and build output. |
reflex.lock/ | Keeps frontend package metadata and lock state across workspace refreshes. |
assets/ and public/ | Supply app assets to the frontend. |
.reflex/ | Stores local Reflex state used to identify and prepare the project. |
The generated directories are not a second application source tree. They are working output that lets the frontend toolchain run without requiring developers to author JavaScript for the normal app path.
References: packages/…/base.py• L20-70 packages/…/config.py• L14-21 packages/…/blank.py• L3-36 reflex/…/frontend_skeleton.py• L451-485 reflex/…/prerequisites.py• L55-84 reflex/…/prerequisites.py• L724-759
Frontend dependencies and runtimes
The frontend workflow supplies the JavaScript packages and runtime needed to serve or build the generated browser app. Reflex prefers Bun when available, can use npm, and preserves the selected dependency state so a later initialization does not silently replace resolved versions.
Package manager selection
Reflex chooses a usable package manager from Bun and npm. An explicit REFLEX_USE_NPM setting takes precedence, and a project with only an npm lockfile can keep using npm without a new setting. On Windows paths under OneDrive, npm is selected because Bun is not reliable in that environment.
The package manager installs framework packages plus packages requested by configuration, plugins, and imported components. It keeps regular and development packages in their intended sections and removes entries that are no longer needed. A failed frozen install stops the workflow rather than switching managers and changing the resolved dependency set.
References: reflex/…/js_runtimes.py• L90-168 reflex/…/js_runtimes.py• L188-222 reflex/…/js_runtimes.py• L356-377 reflex/…/js_runtimes.py• L606-781 reflex/…/prerequisites.py• L807-819
Lockfile persistence
Frontend dependency state is persisted under reflex.lock/ and mirrored into .web before an install. After frontend work, the workspace lock state is copied back to the project root. This lets reflex init rebuild the generated workspace without losing package pins or user metadata.
The persisted package.json keeps user fields while Reflex refreshes the scripts required for development and export. A mismatch between the persisted package data and its lockfile is treated as a project state error; the recovery path is to remove the persisted lock state and let the next setup create it again.
References: reflex/…/frontend_skeleton.py• L270-410 reflex/…/frontend_skeleton.py• L533-608 reflex/…/js_runtimes.py• L396-403 reflex/…/js_runtimes.py• L468-531
Frontend workspace
The frontend workspace is built from framework templates and then filled with project-specific settings. Reflex writes the package manifest, runtime config, route base, public directory, and client environment data before it starts or exports the frontend.
The frontend compilation details are covered in Frontend compilation. The key workflow boundary is that the CLI prepares this workspace, while the compiler supplies the app-specific page and component output that the workspace serves.
References: reflex/…/frontend_skeleton.py• L451-485 reflex/…/frontend_skeleton.py• L505-530 reflex/…/frontend_skeleton.py• L611-660 reflex/…/build.py• L19-34
Application execution
The run workflow validates the project, compiles the app when the frontend is present, and starts the selected services. It separates the Python backend from the browser frontend in development while allowing a single compiled service for preview or production.
Run modes
The --env option selects the serving model. Frontend-only and backend-only flags narrow the running mode when a developer needs to work on one side of the app.
| Mode | Frontend behavior | Backend behavior |
|---|---|---|
dev | Runs the frontend development server with fast refresh. | Runs the reload-capable development server. |
preview | Serves a fresh, readable compiled frontend bundle. | Keeps backend reload behavior for local checks. |
prod | Serves an optimized compiled frontend. | Runs the production backend service. |
Development normally uses port 3000 for the frontend and 8000 for the backend. Preview and production use one port for the selected service combination. The CLI can also run frontend-only or backend-only workflows when the ports and mode support that choice. The explicit --single-port option is limited to a full-stack production run.
Production backend startup can preload the app before worker processes start on platforms that support fork. This allows workers to share loaded app memory. If active telemetry makes forking unsafe, the service uses a safer worker start method instead. The production frontend can also run as a static service when the compiled browser app is served without the backend.
References: reflex/reflex.py• L494-835 reflex/…/exec.py• L987-1114 reflex/…/exec.py• L1157-1210 packages/…/base.py• L196-221
Compilation boundary
Before a frontend run, Reflex imports the app module, checks that it exposes a valid rx.App, and compiles the app definition. This step is the boundary between Python source and generated frontend and backend artifacts.
The run command can reinitialize missing or outdated generated state before compiling. A backend-only run skips frontend compilation. A compile also checks the configured database schema when a database migration setup exists. For the relationship between app definitions and generated browser code, see Application lifecycle and Frontend compilation.
References: reflex/…/prerequisites.py• L238-345 reflex/…/prerequisites.py• L408-444 reflex/…/prerequisites.py• L724-759 reflex/…/prerequisites.py• L840-891 reflex/reflex.py• L366-391 reflex/reflex.py• L715-741
Local service coordination
In development, the CLI starts the frontend in a separate process and keeps the backend in the main process. The frontend process watches its generated project and reports its URL. It also observes the generated package manifest, so dependency changes are visible while the frontend process runs. The launcher supplies the development runtime condition needed by the frontend toolchain and stops the whole frontend process tree when the run ends.
This diagram shows how a source change moves through the local development loop.
sequenceDiagram
participant D as Developer
participant R as Reflex CLI
participant C as Compiler
participant F as Frontend server
participant B as Backend server
D->>R: Save Python app changes
R->>C: Recompile app definition
C-->>F: Refresh generated frontend
F-->>D: Fast refresh in browser
B-->>D: Serve backend requests and events
The frontend and backend must use compatible generated state. A package change or a compiler error can therefore stop the local loop even when the Python process itself starts. The backend reload path marks its first worker start separately from later hot reloads, which keeps startup and reload events distinct across worker restarts.
References: reflex/reflex.py• L494-572 reflex/…/exec.py• L40-369 reflex/…/exec.py• L748-984 reflex/…/build.py• L303-316
Build and export
The export workflow converts a project into frontend and backend artifacts that another service can host. It first compiles the app, prepares the generated frontend, creates the production build, and then optionally writes zip files.
Export stages
The export path has one shared build sequence for local packaging and hosting deployment:
flowchart LR
A[Python app and config] --> B[Compile app]
B --> C[Prepare frontend workspace]
C --> D[Create production frontend build]
D --> E[Frontend static artifact]
A --> F[Backend project artifact]
E --> G[Optional zip files]
F --> G
The frontend artifact is a static compiled app. The backend artifact contains the Python project and generated backend material needed to run server-side behavior. Developers can export either side, exclude selected backend paths, include or exclude the local database according to the export option, and choose whether to create zip files. Export can also enable or disable server-side rendering, which controls whether routes are pre-rendered for the frontend artifact.
References: reflex/reflex.py• L939-1034 reflex/…/export.py• L19-117 reflex/…/build.py• L231-300 reflex/…/build.py• L124-168
Deployment boundary
Export does not make the frontend and backend one inseparable artifact. A static host can serve the frontend, while a separate Python service runs the backend. The frontend must be compiled with the URLs that clients use to reach the backend and frontend host.
The hosting workflow calls the same export capability after it resolves the target app and hosting settings. Deployment-specific provider selection, authentication, scaling, and rollout behavior belong in Deployment and hosting.
References: docs/…/cli.md• L86-92 reflex/reflex.py• L1192-1207 reflex/…/export.py• L19-117 packages/…/cli.py• L741-878
Project maintenance
Maintenance commands keep project identity, schema state, and generated dependencies aligned as the app changes. These workflows do not replace the main compile and run path; they prepare inputs that the path later validates or consumes.
App renaming
reflex rename validates the new Python package name, reloads the current configuration, and updates the app name in the configuration and source layout. A rename must keep the configured module importable so the next compile can find the app.
References: reflex/reflex.py• L1236-1247 reflex/…/prerequisites.py• L207-235 docs/…/cli.md• L94-108
Database schema workflow
The reflex db group manages the database schema lifecycle when a project has a database URL. Initialization creates the migration configuration and an initial schema. Later commands create migration scripts, apply them, and report whether the database matches the app models.
The migration environment is tied to the app configuration rather than a connection string written into the migration file. During migration work, Reflex loads the app and evaluates all pages so models that pages or route hooks import only when they run can take part in the initial revision and later comparisons. Commands invoked from app import code do not start a second migration while an Alembic command is already loading the app.
Schema checks are part of compile-sensitive workflows when enabled. The database model is loaded only when a database URL and migration setup are present, so commands that do not use a database avoid that work. A missing migration setup or an out-of-date schema can therefore block a run, export, or deployment that requires a valid database state.
Projects from older Reflex releases can update the stock migration environment in place while keeping existing revisions in numbered backup files. A hand-written environment is not replaced automatically; Reflex warns that it may miss app URLs, model discovery, server defaults, and dropped-table warnings.
References: reflex/reflex.py• L1084-1233 reflex/…/migrations.py• L78-219 reflex/…/migrations.py• L238-306 reflex/…/prerequisites.py• L285-345 reflex/…/prerequisites.py• L840-891
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the executable target in pyproject.toml aligned with the top-level Click group in reflex/reflex.py.
- When adding a run mode or service flag, update the running-mode validation, port rules, and the process that receives the mode.
- When changing generated frontend scripts or required packages, review both the workspace template and the persisted reflex.lock synchronization rules.
- When changing app naming or template layout, preserve the import path that rxconfig.py resolves and the source rename performed by initialization.
- When changing export URLs or output selection, review both direct
reflex exportcallers and the hosting deploy callback that packages frontend and backend separately. - When changing package-manager behavior, keep Bun and npm lockfile selection consistent with frozen-install and cache invalidation behavior.
- When changing package version checks, preserve the per-package timestamp keys and the separate cooldowns for successful checks and failed attempts.
- When changing optional command registration, preserve lazy command resolution and the install guidance for missing hosting packages.
- When changing the removed component command, keep its migration message aligned with the supported wrapping and component-template workflows.
- When changing frontend process startup or backend worker startup, preserve process-tree shutdown, reload markers, and platform-safe start-method selection.
- When changing database command flow, keep app and page evaluation aligned with model discovery and prevent nested Alembic runs during app import.
- When changing the migration environment template, preserve legacy backup behavior and the warning path for custom environments.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Run, compile, or export starts outside a project root without rxconfig.py | Reflex cannot identify the app or generated workspace. | Move to the project root or run reflex init; root-sensitive commands assert the config file. |
| App name does not form a valid Python package or does not match the configured module | App import and compilation fail before services start. | Validate the name during initialization and rename operations. |
| Bun and npm are unavailable, or the selected Node.js version is too old | Frontend setup, development server, and production build cannot run. | Install a supported runtime or use the supported package-manager choice. |
| Persisted frontend package data does not match its lockfile | Frozen installation fails and the frontend workspace is not trusted. | Remove the stale reflex.lock/ state and regenerate it. |
| Production or preview receives different frontend and backend ports | The command rejects the configuration or cannot expose the combined service as expected. | Use one port for combined non-development modes. |
| Frontend-only export is used as if it also contained the backend | Static pages load but server-side events and API work are unavailable. | Export and host the backend separately. |
| Database URL exists without initialized migration configuration | Database commands and schema checks stop without a usable schema workflow. | Run reflex db init before migration or deployment-sensitive work. |
| The migration environment is from the old stock template | The app models and configured database URL are not loaded for migration work. | Run reflex db init to install the current environment while keeping revisions. |
| A custom migration environment fails or omits Reflex behavior | Migration checks can fail or compare an incomplete model set. | Review the warning and adopt the current template after moving the custom file aside. |
| Optional hosting commands are used without the hosting CLI package | The command cannot authenticate or deploy. | Install the hosting CLI package named by the command error. |
The removed reflex component command is used for new component work | The command exits and no component project is created. | Wrap React components directly or start from the component template. |
| PyPI is slow or unavailable during a package version check | A command can wait on a network request, and repeated attempts can add startup delay. | Store a per-package failed-attempt timestamp and retry after the one-hour cooldown. |
Key files
| File | Why to read it |
|---|---|
| reflex/reflex.py | Primary CLI entry point and orchestration for init, run, compile, export, deploy, rename, database, and command-group registration. It also defines lazy loading for optional command groups and the migration message for the removed component CLI. |
| reflex/…/prerequisites.py | Project checks, app discovery, compile handoff, running-mode selection, generated-state readiness, database checks, and throttled package version checks. |
| reflex/…/migrations.py | Shared database migration flow, app and page loading, migration-environment classification, legacy upgrades, and error guidance. |
| reflex/…/templates.py | Built-in and remote project template flow. |
| reflex/…/frontend_skeleton.py | Generated frontend workspace, package manifest, runtime settings, and lockfile persistence. |
| reflex/…/js_runtimes.py | Bun/npm choice, runtime checks, frontend package installation, and install caching. |
| reflex/…/exec.py | Frontend process startup, package-change observation, backend reload state, production worker startup, compiled frontend serving, URL reporting, and local development process behavior. |
| reflex/…/build.py | Production frontend build, static post-processing, and frontend/backend zip assembly. |
| reflex/…/export.py | Shared export orchestration used by direct export and hosting deployment. |
| packages/…/blank.py | Minimal generated app that shows the source shape expected after initialization. |
| pyproject.toml | Python package entry point and runtime dependency contract for the reflex command. |