Python python-3.x python-import fastapi. Improve this question. Follow edited Jul 8 '20 at 11:41. Asked Jul 7 '20 at 17:21. Cylon86 cylon86. In this series, I try out FastAPI, a new Python framework built on Starlette. I show you how to build a web-based stock screener using this framework and use. High-Performance, Robust, Auto-docs APIs using FastAPI in Python October 6, 2020 October 6, 2020 by Om Singh Building APIs and using them in your application has become an inevitable task to make your application more modular and the development process even faster and use resources efficiently.Latest version
Fast Admin Dashboard based on fastapi and tortoise-orm.
FastAPI-Admin is a admin dashboard based onfastapi andtortoise-orm.
FastAPI-Admin provide crud feature out-of-the-box with just a fewconfig.
Check a live Demo herehttps://fastapi-admin.long2ice.cn.
Data in database will restore every day.
- FastAPI framework as yourbackend framework.
- Tortoise-ORM as your ormframework, by the way, which is best asyncio orm so far and I'm oneof the contributors😋.
Look full example atexamples.
git clone https://github.com/long2ice/fastapi-admin.git.
docker-compose up -d --build.
docker-compose exec -T mysql mysql -uroot -p123456 < examples/example.sql fastapi-admin.
- That's just all, api server is listen at http://127.0.0.1:8000 now.
See documentation at https://long2ice.github.io/fastapi-admin.
Deploy fastapi app by gunicorn+uvicorn or referencehttps://fastapi.tiangolo.com/deployment/.
Restful API Docs
See restful apidocs.
Support this project
|PayPal to my account long2ice.|
This project is licensed under theApache-2.0License.
Release historyRelease notifications RSS feed
Python Fastapi Post
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size fastapi_admin-0.3.3-py3-none-any.whl (28.3 kB)||File type Wheel||Python version py3||Upload date||Hashes|
|Filename, size fastapi-admin-0.3.3.tar.gz (19.7 kB)||File type Source||Python version None||Upload date||Hashes|
Hashes for fastapi_admin-0.3.3-py3-none-any.whl
Hashes for fastapi-admin-0.3.3.tar.gz
In this tutorial, we’ll build a production-ready FastAPI server together.The full functional example is available here.
Our application stack will look like this:
Start a New Project¶
Instead of pip, let’s use the shiny Poetry to manage our project. Follow the link toinstall Poetry, and create our newproject in an empty directory:
Then follow the Poetry guide to finish the initialization - you may say “no” to theinteractive dependency creation, because we will add them manually. It is okay to usethe default values in the other steps of the guide, and make sure the package nameremains
FastAPI is built on top of the Starlette framework, so we shall use the GINOextension for Starlette. Simply run:
Then let’s add FastAPI, together with the lightning-fast ASGI server Uvicorn, andGunicorn as a production application server:
For database migration, we’ll use Alembic. Because it uses normal DB-API, we needpsycopg here too:
At last, let’s add pytest in the development environment for testing. We also want toadd the requests library to use the Starlette
With the steps above, Poetry will automatically create a virtualenv for youbehind the scene, and all the dependencies are installed there. We will assumeusing this for the rest of the tutorial. But you’re free to create your ownvirtualenv, and Poetry will honor it when it’s activated.
That’s all, this is my
pyproject.toml created by Poetry, yours should look similar:
And there’s also an auto-generated
poetry.lock file with the frozen versions. Thedirectory layout should look like the diagram on the right. Now let’s add the two filesto the Git repository (we will skip showing these git operations in future steps):
Write a Simple Server¶
Now let’s write some Python code.
We’ll create an extra
src directory to include all the Python files, as demonstratedin the diagram below. This is known as the “src layout” providing a cleaner hierarchy.
The root Python package of our project is named as
gino_fastapi_demo, under which wewill create two Python modules:
asgias the ASGI entry point - we’ll feed it to the ASGI server
mainto initialize our server
And we’ll simply instantiate our application in
poetryinstall to link our Python package into the
PYTHONPATH indevelopment mode. We’ll be able to start a Uvicorn development server after that:
--reload option enables Uvicorn to automatically reload the server for us whenthe Python source code is updated. Now access http://127.0.0.1:8000/docs to see theSwagger UI of our new FastAPI server.
Fastapi Python Youtube
As mentioned previously, if you’re in your own virtualenv, the command
poetryrunuvicorn can be simplified as just
poetryrun is a convenient shortcut to run the following command in thevirtualenv managed by Poetry.
Add GINO Extension¶
Now let’s add GINO to our server.
First of all, we need a way to configure the database. In this tutorial, we’ll use theconfiguration system from Starlette.Add
src/gino_fastapi_demo/config.py as follows:
This config file will load from environment variable first, if not found then from afile named
.env from current path (usually the project root directory), and at lastuse the default value defined above. For example, you can either overwrite in CLIdirectly like this:
Or set them in the file
.env (this file must not be committed into Git, remember toadd it to
Now it’s time to create a PostgreSQL database and set the connection variablescorrectly here. This is usually something like
createdbyourdbname, but it may varyacross different platforms, so we won’t cover this part in this tutorial.
Alternatively, you could also set
DB_DSN to for example
postgresql://user:[email protected]:5432/dbname to override the other individualconfig values like
DB_HOST defined before
DB_DSN always have the higher priority over the individual ones,regardless of where they are defined - even if
DB_HOST is defined in environmentvariable and
DB_DSN is defined in
DB_HOST is still ignored.Default value doesn’t count.
Then, create a new Python sub-package
gino_fastapi_demo.models to encapsulatedatabase-related code, and add the code below to
At last, modify
src/gino_fastapi_demo/main.py to install the GINO extension:
Saving the file, you should see the Uvicorn server reloads our changes and connects tothe database:
Create Models and API¶
It’s time to implement the API now. Let’s say we are building a user management service,through which we could add users, list users and delete users.
First of all, we need a database table
users to store the data, mapped to a GINOmodel named
User. We shall add the model in
The model definition is simple enough to explain itself.
Then we only have to use it properly in the API implementation, for which we’ll create anew Python sub-package
gino_fastapi_demo.views, and a new module
gino_fastapi_demo.views.users as follows:
APIRouter holds our new APIs locally, and
init_app is used to integrate itinto our FastAPI application. Here we want some inversion of control: let’s make theAPIs plugable, so that we don’t have to import all possible future views manually. Weshall use the Entry Points feature to load the dependencies. Add this code below to
If you’re running Python < 3.8, you’ll need this importlib-metadata backport.
And call it in our application factory:
Finally, define the entry points in
pyproject.toml following the Poetry documentfor plugins:
poetryinstall again to activate the entry points - you may need to restart theUvicorn development server manually, as the reloader cannot capture the changes we madeto
Now you should be able to see the 3 new APIs on the Swagger UI. But none of them works,because we still haven’t created the database tables.
Integrate with Alembic¶
To get started with Alembic, run this command in the project root directory:
This will generate a new directory
migrations where Alembic will store databasemigration revisions. At the same time, an
alembic.ini file is created in the projectroot directory. Let’s simply add all of them to Git control.
For Alembic to use our data models defined with GINO (and of course the databaseconfig), we need to modify
migrations/env.py to connect with the GINO instance:
Then create our first migration revision with:
The generated revision file should roughly look like this:
Whenever there is a change to the database schema in the future, just modify theGINO models and run
alembicrevision--autogenerate again to generate newrevisions to track the change. Remember to review the revision file - you may wantto adjust it.
Eventually, let’s apply this migration, by upgrading to the latest revision:
Now all the APIs should be fully operational, try with the Swagger UI.
Write the Tests¶
Fastapi Python Logging
In order not to break our development database with running tests, let’s create aseparate database to run tests. Apply this change to
You need to run
createdb to actually create the database. If you have set
.env - e.g.
DB_DATABASE=mydb, the name of the testingdatabase should be
mydb_test. Or else,
Then, let’s create our pytest fixture in
This fixture creates all the database tables before running the test, yield a Starlette
TestClient, and drop all the tables with all the data after the test to maintain aclean environment for the next test.
Here’s a sample test in
Then run the test:
Notes for Production¶
Given the popularity of Docker/Kubernetes, we’ll build a
Dockerfile for our demo:
Dockerfile, we used 2 phases to separate the building from the productionimage to reduce target artifact size. Also, we are using Gunicorn with
UvicornWorker from Uvicorn as the worker class for best production reliability.
Let’s review what we have in the project.
This is the end of the tutorial to build a demo. Below is an incomplete checklist togo live:
DB_RETRY_LIMITto a larger number to allow staring the application serverbefore the database is fully ready.
Implement the same retry logic in
migrations/env.pyso that Alembic gets the samefunctionality.
docker-compose.ymlfor other developers to get a quick taste or even useit for development.
Enable CI, install
--cov-fail-underto guarantee coverage.
Integrate static code analysis tools and security/CVE checking tools.
Automate Alembic upgrade properly - e.g. after new version is deployed.
Be aware of the common security attacks like CSRF, XSS, etc.
Write load tests.
Again, the source code of the demo is available here,and the source of this tutorial is here.Please feel free to submit PRs to fix issues or add your thoughts. Happy hacking!