Skip to content

Commit bff10c8

Browse files
committed
setup and create
1 parent 57682bf commit bff10c8

File tree

2 files changed

+57
-41
lines changed

2 files changed

+57
-41
lines changed

snooty.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ toc_landing_pages = [
1111
"crud",
1212
"/crud/query",
1313
"/crud/update",
14-
"/monitoring-and-logging"
14+
"/monitoring-and-logging",
15+
"integrations"
1516
]
1617

1718
intersphinx = [

source/integrations/fastapi-integration.txt

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,75 +18,85 @@ FastAPI Integration Tutorial
1818
:description: Learn how to create an app to connect to MongoDB deployment by using the PyMongo driver.
1919
:keywords: quick start, tutorial, basics
2020

21-
`FastAPI <https://fastapi.tiangolo.com/>`__ is a modern, high-performance, easy-to-learn, fast-to-code, production-ready, Python 3.6+ framework for building APIs based on standard Python type hints. While it might not be as established as some other Python frameworks such as Django, it is already in production at companies such as Uber, Netflix, and Microsoft.
22-
23-
FastAPI is async, and as its name implies, it is super fast; so, MongoDB is the perfect accompaniment. In this quick start, we will create a CRUD (Create, Read, Update, Delete) app showing how you can integrate MongoDB with your FastAPI projects.
21+
`FastAPI <https://fastapi.tiangolo.com/>`__ is a high-performance,
22+
production-ready asynchronous Python framework for building APIs based on
23+
standard Python type hints. In this tutorial, we will create a CRUD application
24+
showing how you can integrate MongoDB with your FastAPI projects.
2425

2526
Prerequisites
2627
-------------
2728

28-
- Python 3.9.0
29-
- A MongoDB Atlas cluster. Follow the "`Get Started with Atlas <https://docs.atlas.mongodb.com/getting-started/>`__" guide to create your account and MongoDB cluster. Keep a note of your username, password, and `connection string <https://docs.atlas.mongodb.com/tutorial/connect-to-your-cluster/#connect-to-your-atlas-cluster>`__` as you will need those later.
29+
- Python v3.9.0 or later
30+
- A MongoDB Atlas cluster
31+
See the `Get Started with Atlas
32+
<https://docs.atlas.mongodb.com/getting-started/>`__ guide to create your
33+
account and MongoDB cluster.
3034

31-
Running the Example
32-
-------------------
35+
Set-up
36+
------
3337

34-
To begin, you should `clone the example code from GitHub <https://github.com/mongodb-developer/mongodb-with-fastapi>`__.
38+
1. Clone the example code from the `mongodb-with-fastapi repository <https://github.com/mongodb-developer/mongodb-with-fastapi>`__:
3539

36-
.. code-block:: shell
40+
.. code-block:: shell
3741

38-
git clone [email protected]:mongodb-developer/mongodb-with-fastapi.git
42+
git clone [email protected]:mongodb-developer/mongodb-with-fastapi.git
3943

40-
You will need to install a few dependencies: FastAPI, `Motor <https://motor.readthedocs.io/>`__, etc. I always recommend that you install all Python dependencies in a `virtualenv <https://docs.python.org/3/tutorial/venv.html>`__ for the project. Before running pip, ensure your ``virtualenv`` is active.
44+
#. Install the required dependencies listed in the ``requirements.txt`` file:
4145

42-
.. code-block:: shell
46+
.. tip:: Use a Virtual environment
4347

44-
cd mongodb-with-fastapi
45-
pip install -r requirements.txt
48+
Installing your Python dependencies in a `virtualenv
49+
<https://docs.python.org/3/tutorial/venv.html>`__ with allow for versions
50+
of the libraries to be install for individual projects. Before running
51+
pip, ensure your ``virtualenv`` is active.
4652

47-
It may take a few moments to download and install your dependencies. This is normal, especially if you have not installed a particular package before.
53+
.. code-block:: shell
4854

49-
Once you have installed the dependencies, you need to create an environment variable for your MongoDB connection string.
55+
cd mongodb-with-fastapi
56+
pip install -r requirements.txt
5057

51-
.. code-block:: shell
58+
It may take a few moments to download and install your dependencies.
5259

53-
export MONGODB_URL="mongodb+srv://<username>:<password>@<url>/<db>?retryWrites=true&w=majority"
60+
#. Create an environment variable for your MongoDB connection string:
5461

55-
Remember, anytime you start a new terminal session, you will need to set this environment variable again. I use `direnv <https://direnv.net/>`__ to make this process easier.
62+
.. code-block:: shell
5663

57-
The final step is to start your FastAPI server.
64+
export MONGODB_URL="mongodb+srv://<username>:<password>@<url>/<db>?retryWrites=true&w=majority"
5865

59-
.. code-block:: shell
66+
.. tip:: Reset Environment Variables
6067

61-
uvicorn app:app --reload
68+
Anytime you start a new terminal session, you will need to reset this
69+
environment variable. You can use `direnv <https://direnv.net/>`__ to make
70+
this process easier.
6271

63-
.. image:: /includes/integrations/fastapi-terminal.png
64-
:alt: Screenshot of terminal running FastAPI
72+
#. Start your FastAPI server:
6573

66-
Once the application has started, you can view it in your browser at http://127.0.0.1:8000/docs.
74+
.. code-block:: shell
6775

68-
.. image:: /includes/integrations/fastapi-browser.png
69-
:alt: Screenshot of browser and swagger UI
76+
uvicorn app:app --reload
7077

71-
Once you have had a chance to try the example, come back and we will walk through the code.
78+
.. image:: /includes/integrations/fastapi-terminal.png
79+
:alt: Screenshot of terminal running FastAPI
7280

73-
Creating the Application
74-
------------------------
81+
Once the application has started, you can view it in your browser at http://127.0.0.1:8000/docs.
7582

76-
All the code for the example application is within ``app.py``. I'll break it down into sections and walk through what each is doing.
83+
.. image:: /includes/integrations/fastapi-browser.png
84+
:alt: Screenshot of browser and swagger UI
7785

78-
Connecting to MongoDB
79-
~~~~~~~~~~~~~~~~~~~~~
86+
Create Your Application
87+
-----------------------
8088

81-
One of the very first things we do is connect to our MongoDB database.
89+
All the code for the example application is stored in ``app.py``.
8290

83-
.. code-block:: python
91+
1. Connect to your MongoDB Atlas cluster using the asynchronous `Pymongo Driver
92+
<https://www.mongodb.com/docs/languages/python/pymongo-driver/current>`__,
93+
then specify the database named ``college``:
8494

85-
client = motor.motor_asyncio.AsyncIOMotorClient(os.environ[MONGODB_URL])
86-
db = client.get_database("college")
87-
student_collection = db.get_collection("students")
95+
.. code-block:: python
8896

89-
We're using the async `motor driver <https://motor.readthedocs.io/en/stable/>`__ to create our MongoDB client, and then we specify our database name ``college``.
97+
client = AsyncMongoClient(MONGODB_URL, server_api=pymongo.server_api.ServerApi(version="1", strict=True, deprecation_errors=True))
98+
db = client.get_database("college")
99+
student_collection = db.get_collection("students")
90100

91101
The _id Attribute and ObjectIds
92102
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -336,7 +346,12 @@ Our final route is ``delete_student``. Again, because this is acting upon a sing
336346
Our New FastAPI App Generator
337347
-----------------------------
338348

339-
If you're excited to build something more production-ready with FastAPI, React & MongoDB, head over to the `Github repository <https://github.com/mongodb-labs/full-stack-fastapi-mongodb>`__ for our `new FastAPI app generator <https://www.mongodb.com/blog/post/introducing-full-stack-fast-api-app-generator-for-python-developers>`__ and start transforming your web development experience.
349+
If you're excited to build something more production-ready with FastAPI, React &
350+
MongoDB, head over to the `full-stack-fastapi-mongodb Github repository
351+
<https://github.com/mongodb-labs/full-stack-fastapi-mongodb>`__ for our `new
352+
FastAPI app generator
353+
<https://www.mongodb.com/blog/post/introducing-full-stack-fast-api-app-generator-for-python-developers>`__
354+
and start transforming your web development experience.
340355

341356
Wrapping Up
342357
-----------

0 commit comments

Comments
 (0)