Skip to content

Commit 5b21703

Browse files
committed
formatting
1 parent 5d209b7 commit 5b21703

File tree

3 files changed

+74
-50
lines changed

3 files changed

+74
-50
lines changed

source/connect.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ see the corresponding code:
100100
uri = "mongodb://localhost:27017/"
101101
client = AsyncMongoClient(uri)
102102

103+
.. _pymongo_connect_atlas:
104+
103105
Atlas
104106
~~~~~
105107

Binary file not shown.

source/integrations/fastapi-integration.txt

Lines changed: 72 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
.. _pymongo-fastapi:
22
.. original URL: https://www.mongodb.com/developer/languages/python/python-quickstart-fastapi/
33

4-
============================
5-
FastAPI Integration Tutorial
6-
============================
4+
=============================
5+
Tutorial: FastAPI Integration
6+
=============================
77

88
.. contents:: On this page
99
:local:
@@ -16,92 +16,105 @@ FastAPI Integration Tutorial
1616
:values: tutorial
1717

1818
.. meta::
19-
:description: Learn how to create an app to connect to MongoDB deployment by using the PyMongo driver.
19+
:description: Build an application use FastAPI that integrates with a MongoDB deployment by using the PyMongo Async driver.
2020
:keywords: quick start, tutorial, basics
2121

2222
Overview
2323
--------
2424

2525
`FastAPI <https://fastapi.tiangolo.com/>`__ is a high-performance,
26-
production-ready asynchronous Python framework for building APIs based on
27-
standard Python type hints. In this tutorial, we will create a CRUD application
28-
showing how you can integrate MongoDB with your FastAPI projects.
26+
production-ready asynchronous {+language+} framework for building APIs based on
27+
standard {+language+} type hints. In this tutorial, you can learn how to create a CRUD application
28+
that integrates MongoDB with your FastAPI projects.
2929

3030
Tutorial
3131
--------
3232

33-
This tutorial walks through the `MongoDB with FastAPI sample project <https://github.com/mongodb-developer/mongodb-with-fastapi>`__.
33+
You can find the completed sample app for this tutorial in the :github:`MongoDB
34+
with FastAPI sample project
35+
repository</mongodb-developer/mongodb-with-fastapi>`__ on GitHub.
3436

3537
Prerequisites
3638
~~~~~~~~~~~~~
3739

38-
- Python v3.9.0 or later
39-
- A MongoDB Atlas cluster
40-
See the `Get Started with Atlas
41-
<https://docs.atlas.mongodb.com/getting-started/>`__ guide to create your
42-
account and MongoDB cluster.
40+
- Python v3.9.0 or later
41+
- A MongoDB Atlas cluster
42+
See the :re:`Getting Started <pymongo-get-started>`__ guide for more information.
4343

4444
Set-up
4545
~~~~~~
4646

4747
.. procedure::
4848
:style: connected
4949

50-
.. step:: Clone the example code from the `mongodb-with-fastapi repository <https://github.com/mongodb-developer/mongodb-with-fastapi>`__:
50+
.. step:: Clone the example code example
51+
52+
Run the following command in your terminal to clone the code from the `mongodb-with-fastapi repository <https://github.com/mongodb-developer/mongodb-with-fastapi>`__:
5153

5254
.. code-block:: shell
5355

54-
git clone [email protected]:mongodb-developer/mongodb-with-fastapi.git
56+
git clone [email protected]:mongodb-developer/mongodb-with-fastapi.git
5557

56-
.. step:: Install the required dependencies listed in the ``requirements.txt`` file:
58+
.. step:: Install the required dependencies.
59+
60+
.. tip:: Use a Virtual environment
5761

58-
.. tip:: Use a Virtual environment
62+
Installing your Python dependencies in a `virtualenv <https://docs.python.org/3/tutorial/venv.html>`__ with allow for versions of the libraries to be install for individual projects. Before running pip, ensure your ``virtualenv`` is active.
63+
64+
Run the following command in your terminal to install the dependencies listed in the ``requirements.txt`` file:
5965

60-
Installing your Python dependencies in a `virtualenv <https://docs.python.org/3/tutorial/venv.html>`__ with allow for versions of the libraries to be install for individual projects. Before running pip, ensure your ``virtualenv`` is active.
61-
62-
.. code-block:: shell
63-
64-
cd mongodb-with-fastapi
65-
pip install -r requirements.txt
66-
67-
It may take a few moments to download and install your dependencies.
68-
69-
.. step:: Create an environment variable for your MongoDB connection string:
70-
71-
.. code-block:: shell
66+
.. code-block:: shell
7267

73-
export MONGODB_URL="mongodb+srv://<username>:<password>@<url>/<db>?retryWrites=true&w=majority"
68+
cd mongodb-with-fastapi
69+
pip install -r requirements.txt
7470

75-
.. tip:: Reset Environment Variables
71+
It may take a few moments to download and install your dependencies.
7672

77-
Anytime you start a new terminal session, you will must reset this
78-
environment variable. You can use `direnv <https://direnv.net/>`__ to make
79-
this process easier.
73+
.. step:: Retrieve your connection string
74+
75+
Follow the :manual:`Find Your MongoDB Atlas Connection String guide </reference/connection-string/#find-your-mongodb-atlas-connection-string>` to retrieve your connection string.
76+
77+
Run the following code in your terminal to create an environment variable to store your connection string:
8078

81-
.. step:: Start your FastAPI server:
79+
..
80+
is this necessary? Are we connecting to MongoDB in this step?
8281

83-
.. code-block:: shell
82+
.. code-block:: shell
8483

85-
uvicorn app:app --reload
84+
export MONGODB_URL="mongodb+srv://<username>:<password>@<url>/<db>?retryWrites=true&w=majority"
85+
86+
.. tip:: Reset Environment Variables
8687

87-
.. image:: /includes/integrations/fastapi-terminal.png
88-
:alt: Screenshot of terminal running FastAPI
88+
Anytime you start a new terminal session, you will must reset this
89+
environment variable. You can use `direnv <https://direnv.net/>`__ to
90+
make this process easier.
8991

90-
Once the application has started, you can view it in your browser at http://127.0.0.1:8000/docs.
92+
.. step:: Start your FastAPI server
93+
94+
Run the following code in your terminal to start your FastAPI server:
95+
96+
.. code-block:: shell
9197

92-
.. image:: /includes/integrations/fastapi-browser.png
93-
:alt: Screenshot of browser and swagger UI
98+
uvicorn app:app --reload
99+
100+
Once the application has started, you can view it in your browser at http://127.0.0.1:8000/docs.
101+
102+
.. image:: /includes/integrations/fastapi-browser.png
103+
:alt: Screenshot of browser and swagger UI
94104

95-
Create Your Application
96-
~~~~~~~~~~~~~~~~~~~~~~~
105+
Connect Your Application to Your Cluster
106+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97107

98-
All the code for the example application is stored in ``app.py``.
108+
All the code for the example application is stored in the `app.py file in the mongodb-with-fastapi repository
109+
<https://github.com/mongodb-developer/mongodb-with-fastapi/blob/master/app.py>`__.
99110

100-
Connect to your MongoDB Atlas cluster using the asynchronous `Pymongo Driver <https://www.mongodb.com/docs/languages/python/pymongo-driver/current>`__, then specify the database named ``college``:
111+
Use the following code to :ref:`connect to your MongoDB Atlas cluster
112+
<pymongo_connect_atlas>` by using the {+driver-async+} ``AsyncMongoClient()``
113+
method and the ``MONGODB_URL`` environment variable, then specify the database named ``college``:
101114

102115
.. code-block:: python
103-
104-
client = AsyncMongoClient(MONGODB_URL, server_api=pymongo.server_api.ServerApi(version="1", strict=True, deprecation_errors=True))
116+
117+
client = AsyncMongoClient(os.environ["MONGODB_URL"],server_api=pymongo.server_api.ServerApi(version="1", strict=True,deprecation_errors=True))
105118
db = client.get_database("college")
106119
student_collection = db.get_collection("students")
107120

@@ -119,7 +132,10 @@ Define Your Database Models
119132
For more information about how BSON compares to JSON, see this `JSON and BSON
120133
<https://www.mongodb.com/json-and-bson>`__ MongoDB article.
121134

122-
Our application has three models, the ``StudentModel``, the ``UpdateStudentModel``, and the ``StudentCollection``.
135+
Our application has three models, the ``StudentModel``, the
136+
``UpdateStudentModel``, and the ``StudentCollection``.
137+
138+
Define the ``StudentModel`` class using the following code:
123139

124140
.. code-block:: python
125141

@@ -154,7 +170,9 @@ Our application has three models, the ``StudentModel``, the ``UpdateStudentModel
154170
},
155171
)
156172

157-
This is the primary model we use as the `response model <https://fastapi.tiangolo.com/tutorial/response-model/>`__ for the majority of our endpoints.
173+
This is the primary model we use as the `response model
174+
<https://fastapi.tiangolo.com/tutorial/response-model/>`__ for the majority of
175+
our endpoints.
158176

159177
I want to draw attention to the ``id`` field on this model. MongoDB uses
160178
``_id``, but in Python, underscores at the start of attributes have special
@@ -168,6 +186,8 @@ cannot assign it a value. To get around this, we name the field
168186
We set this ``id`` value automatically to ``None``, so that can create a new
169187
student with out specifying it.
170188

189+
Define the ``UpdateStudentModel`` class using the following code:
190+
171191
.. code-block:: python
172192

173193
class UpdateStudentModel(BaseModel):
@@ -199,6 +219,8 @@ The ``UpdateStudentModel`` has two key differences from the ``StudentModel``:
199219

200220
Finally, ``StudentCollection`` is defined to encapsulate a list of ``StudentModel`` instances. In theory, the endpoint could return a top-level list of StudentModels, but there are some vulnerabilities associated with returning JSON responses with top-level lists.
201221

222+
Define the ``StudentCollection`` class using the following code:
223+
202224
.. code-block:: python
203225

204226
class StudentCollection(BaseModel):

0 commit comments

Comments
 (0)