Skip to content

A demonstration of how to use FastAPI together with React

Notifications You must be signed in to change notification settings

finsberg/fastapi-react-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fast API + SQLModel + React example

This is an implementation of a simple TODO list with authentication using FastAPI, SQLModel and React.

Tech stack

For the frontend we use

For the backend we use

Installation

Installing the backend dependencies

If is recommended to first create a virtual environment.

python3 -m venv venv

and activate the virtual environment

. venv/bin/activate

Go in to the backend directory

cd backend

and install the dependencies

python3 -m pip install -r requirements.txt

Note that the dependencies are generated from the requirements.in file and compiled with pip-tools.

If you want to run the tests you also need to install the dev-requirements, i.e

python3 -m pip install dev-requirements.txt

Installing the frontend dependencies

First go in to the frontend directory

cd frontend

and then execute

npm install

Running

Once you have installed the dependencies you can run both the backend and frontend server.

Setting up the secrets

Before running the backend we need to set up the secrets. You do this by creating the file backend/.env with the following content

JWT_SECRET_KEY=my-secret
JWT_REFRESH_SECRET_KEY=another-secret
SQL_CONNECTION_STRING=sqlite:///database.db

See .env.exmple for an example file.

Of course the secret keys should be changed when moving to production. We will also use a simple SQLite database i.e database.db stored in the same folder. If you want to reset the database, you can just delete this file.

Running the backend

We will use uvicorn to run the backend. Go in to the backend directory

cd backend

and execute the command

uvicorn app.main:app --reload --port 8000

Your backend server should now run at http://localhost:8000. You can verify that the server is running by pinging the server

curl localhost:8000/api/v1/health

You should now see the following output

{"msg":"OK"}

You can also go to http://localhost:8000/docs to see the OpenAPI documentation for the endpoints.

Running the frontend

To run the frontend, go to the frontend directory

cd frontend

and execute

yarn dev

If you don't have yarn install, you can also use

npm run dev

The frontend server should now run on http://localhost:5173/, which you can open in the browser.

Testing

There also automated tests

Pre-commit hooks

There are set of pre-commit hooks defined in the .pre-commit-config.yaml which will run linters and formatter on both the python and javascript code.

These are also run in the CI using GitHub actions. If you want make sure that the pre-commit hooks are regularly updated, then you might consider to set up pre-commit.ci instead.

Testing the backend

You can run the backend tests with pytest. First go to the backend directory

cd backend

and then run pytest

python3 -m pytest -vv

There is also a GitHub action to run run the tests in CI.

Testing the frontend

The frontend tests are run with vitetest. To run the tests you need to first go to the frontend directory

cd frontend

and then run

yarn test

This will run the test, watch for changes and then rerun the tests it is detects changes. There is also a UI version that can be using using

yarn test:ui

Which will also open a dashboard with the test run in the browser. You can also just run the tests without watching for changes with the command

yarn test:run

To get coverage report, you can run

yarn coverage

Finally, there is a GitHub action to run run the tests in CI.

About

A demonstration of how to use FastAPI together with React

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published