From be669059fbc64dd36a5544d19eeaef5de8468f41 Mon Sep 17 00:00:00 2001 From: Brock Friedrich Date: Sat, 29 Aug 2020 09:02:58 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Simplify=20docs=20hl=5Flines=20r?= =?UTF-8?q?anges=20and=20standardize=202=20spaces=20between=20each=20range?= =?UTF-8?q?=20(#1863)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +-- docs/en/docs/advanced/additional-responses.md | 12 ++++---- docs/en/docs/advanced/async-sql-databases.md | 14 +++++----- docs/en/docs/advanced/async-tests.md | 2 +- docs/en/docs/advanced/behind-a-proxy.md | 6 ++-- .../docs/advanced/custom-request-and-route.md | 10 +++---- docs/en/docs/advanced/custom-response.md | 14 +++++----- docs/en/docs/advanced/extending-openapi.md | 14 +++++----- docs/en/docs/advanced/graphql.md | 4 +-- docs/en/docs/advanced/middleware.md | 2 +- docs/en/docs/advanced/nosql-databases.md | 12 ++++---- docs/en/docs/advanced/openapi-callbacks.md | 6 ++-- .../path-operation-advanced-configuration.md | 4 +-- docs/en/docs/advanced/response-cookies.md | 4 +-- docs/en/docs/advanced/response-directly.md | 2 +- docs/en/docs/advanced/response-headers.md | 4 +-- .../docs/advanced/security/http-basic-auth.md | 6 ++-- .../docs/advanced/security/oauth2-scopes.md | 10 +++---- docs/en/docs/advanced/settings.md | 14 +++++----- docs/en/docs/advanced/sql-databases-peewee.md | 20 ++++++------- docs/en/docs/advanced/sub-applications.md | 6 ++-- docs/en/docs/advanced/templates.md | 2 +- docs/en/docs/advanced/testing-database.md | 8 +++--- docs/en/docs/advanced/testing-dependencies.md | 2 +- docs/en/docs/advanced/testing-events.md | 2 +- docs/en/docs/advanced/testing-websockets.md | 2 +- .../docs/advanced/using-request-directly.md | 2 +- docs/en/docs/advanced/websockets.md | 8 +++--- docs/en/docs/advanced/wsgi.md | 2 +- docs/en/docs/async.md | 2 +- docs/en/docs/index.md | 4 +-- docs/en/docs/python-types.md | 8 +++--- docs/en/docs/tutorial/background-tasks.md | 6 ++-- docs/en/docs/tutorial/bigger-applications.md | 12 ++++---- docs/en/docs/tutorial/body-fields.md | 2 +- docs/en/docs/tutorial/body-multiple-params.md | 2 +- docs/en/docs/tutorial/body-nested-models.md | 8 +++--- docs/en/docs/tutorial/body-updates.md | 4 +-- docs/en/docs/tutorial/body.md | 4 +-- docs/en/docs/tutorial/cors.md | 2 +- docs/en/docs/tutorial/debugging.md | 2 +- .../dependencies/classes-as-dependencies.md | 2 +- ...pendencies-in-path-operation-decorators.md | 6 ++-- .../dependencies/dependencies-with-yield.md | 12 ++++---- docs/en/docs/tutorial/dependencies/index.md | 2 +- .../tutorial/dependencies/sub-dependencies.md | 2 +- docs/en/docs/tutorial/encoder.md | 2 +- docs/en/docs/tutorial/extra-data-types.md | 4 +-- docs/en/docs/tutorial/extra-models.md | 10 +++---- docs/en/docs/tutorial/handling-errors.md | 10 +++---- docs/en/docs/tutorial/metadata.md | 4 +-- docs/en/docs/tutorial/middleware.md | 4 +-- .../tutorial/path-operation-configuration.md | 6 ++-- docs/en/docs/tutorial/path-params.md | 6 ++-- docs/en/docs/tutorial/query-params.md | 4 +-- docs/en/docs/tutorial/request-files.md | 2 +- docs/en/docs/tutorial/response-model.md | 16 +++++------ docs/en/docs/tutorial/response-status-code.md | 2 +- docs/en/docs/tutorial/schema-extra-example.md | 6 ++-- .../tutorial/security/get-current-user.md | 6 ++-- docs/en/docs/tutorial/security/oauth2-jwt.md | 8 +++--- .../docs/tutorial/security/simple-oauth2.md | 6 ++-- docs/en/docs/tutorial/sql-databases.md | 28 +++++++++---------- docs/en/docs/tutorial/static-files.md | 2 +- docs/en/docs/tutorial/testing.md | 2 +- docs/es/docs/async.md | 2 +- docs/es/docs/index.md | 4 +-- docs/es/docs/python-types.md | 6 ++-- docs/it/docs/index.md | 4 +-- docs/pt/docs/index.md | 4 +-- docs/ru/docs/index.md | 4 +-- docs/uk/docs/index.md | 4 +-- docs/zh/docs/index.md | 4 +-- docs/zh/docs/python-types.md | 6 ++-- docs/zh/docs/tutorial/body-fields.md | 2 +- docs/zh/docs/tutorial/body-multiple-params.md | 2 +- docs/zh/docs/tutorial/body.md | 4 +-- docs/zh/docs/tutorial/path-params.md | 8 +++--- docs/zh/docs/tutorial/query-params.md | 4 +-- 79 files changed, 232 insertions(+), 232 deletions(-) diff --git a/README.md b/README.md index 9fed5de828091..d0b73f9fc2371 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,7 @@ def read_item(item_id: int, q: Optional[str] = None): If your code uses `async` / `await`, use `async def`: -```Python hl_lines="9 14" +```Python hl_lines="9 14" from typing import Optional from fastapi import FastAPI @@ -245,7 +245,7 @@ Now modify the file `main.py` to receive a body from a `PUT` request. Declare the body using standard Python types, thanks to Pydantic. -```Python hl_lines="4 9 10 11 12 25 26 27" +```Python hl_lines="4 9-12 25-27" from typing import Optional from fastapi import FastAPI diff --git a/docs/en/docs/advanced/additional-responses.md b/docs/en/docs/advanced/additional-responses.md index c8d443c3ca570..4b8b3b71e3430 100644 --- a/docs/en/docs/advanced/additional-responses.md +++ b/docs/en/docs/advanced/additional-responses.md @@ -23,7 +23,7 @@ Each of those response `dict`s can have a key `model`, containing a Pydantic mod For example, to declare another response with a status code `404` and a Pydantic model `Message`, you can write: -```Python hl_lines="18 23" +```Python hl_lines="18 23" {!../../../docs_src/additional_responses/tutorial001.py!} ``` @@ -44,7 +44,7 @@ For example, to declare another response with a status code `404` and a Pydantic The generated responses in the OpenAPI for this *path operation* will be: -```JSON hl_lines="3 4 5 6 7 8 9 10 11 12" +```JSON hl_lines="3-12" { "responses": { "404": { @@ -83,7 +83,7 @@ The generated responses in the OpenAPI for this *path operation* will be: The schemas are referenced to another place inside the OpenAPI schema: -```JSON hl_lines="4 5 6 7 8 9 10 11 12 13 14 15 16" +```JSON hl_lines="4-16" { "components": { "schemas": { @@ -168,7 +168,7 @@ You can use this same `responses` parameter to add different media types for the For example, you can add an additional media type of `image/png`, declaring that your *path operation* can return a JSON object (with media type `application/json`) or a PNG image: -```Python hl_lines="19 20 21 22 23 24 28" +```Python hl_lines="19-24 28" {!../../../docs_src/additional_responses/tutorial002.py!} ``` @@ -192,7 +192,7 @@ For example, you can declare a response with a status code `404` that uses a Pyd And a response with a status code `200` that uses your `response_model`, but includes a custom `example`: -```Python hl_lines="20 21 22 23 24 25 26 27 28 29 30 31" +```Python hl_lines="20-31" {!../../../docs_src/additional_responses/tutorial003.py!} ``` @@ -228,7 +228,7 @@ You can use that technique to re-use some predefined responses in your *path ope For example: -```Python hl_lines="13 14 15 16 17 26" +```Python hl_lines="13-17 26" {!../../../docs_src/additional_responses/tutorial004.py!} ``` diff --git a/docs/en/docs/advanced/async-sql-databases.md b/docs/en/docs/advanced/async-sql-databases.md index 523bc91bf4726..93c288e1b4f56 100644 --- a/docs/en/docs/advanced/async-sql-databases.md +++ b/docs/en/docs/advanced/async-sql-databases.md @@ -23,7 +23,7 @@ Later, for your production application, you might want to use a database server * Create a `metadata` object. * Create a table `notes` using the `metadata` object. -```Python hl_lines="4 14 16 17 18 19 20 21 22" +```Python hl_lines="4 14 16-22" {!../../../docs_src/async_sql_databases/tutorial001.py!} ``` @@ -38,7 +38,7 @@ Later, for your production application, you might want to use a database server * Create a `DATABASE_URL`. * Create a `database` object. -```Python hl_lines="3 9 12" +```Python hl_lines="3 9 12" {!../../../docs_src/async_sql_databases/tutorial001.py!} ``` @@ -54,7 +54,7 @@ Here, this section would run directly, right before starting your **FastAPI** ap * Create an `engine`. * Create all the tables from the `metadata` object. -```Python hl_lines="25 26 27 28" +```Python hl_lines="25-28" {!../../../docs_src/async_sql_databases/tutorial001.py!} ``` @@ -65,7 +65,7 @@ Create Pydantic models for: * Notes to be created (`NoteIn`). * Notes to be returned (`Note`). -```Python hl_lines="31 32 33 36 37 38 39" +```Python hl_lines="31-33 36-39" {!../../../docs_src/async_sql_databases/tutorial001.py!} ``` @@ -78,7 +78,7 @@ So, you will be able to see it all in the interactive API docs. * Create your `FastAPI` application. * Create event handlers to connect and disconnect from the database. -```Python hl_lines="42 45 46 47 50 51 52" +```Python hl_lines="42 45-47 50-52" {!../../../docs_src/async_sql_databases/tutorial001.py!} ``` @@ -86,7 +86,7 @@ So, you will be able to see it all in the interactive API docs. Create the *path operation function* to read notes: -```Python hl_lines="55 56 57 58" +```Python hl_lines="55-58" {!../../../docs_src/async_sql_databases/tutorial001.py!} ``` @@ -103,7 +103,7 @@ That documents (and validates, serializes, filters) the output data, as a `list` Create the *path operation function* to create notes: -```Python hl_lines="61 62 63 64 65" +```Python hl_lines="61-65" {!../../../docs_src/async_sql_databases/tutorial001.py!} ``` diff --git a/docs/en/docs/advanced/async-tests.md b/docs/en/docs/advanced/async-tests.md index 9e7cc70a9e637..921bdb708c871 100644 --- a/docs/en/docs/advanced/async-tests.md +++ b/docs/en/docs/advanced/async-tests.md @@ -77,7 +77,7 @@ The marker `@pytest.mark.asyncio` tells pytest that this test function should be Then we can create an `AsyncClient` with the app, and send async requests to it, using `await`. -```Python hl_lines="9 10" +```Python hl_lines="9-10" {!../../../docs_src/async_tests/test_main.py!} ``` diff --git a/docs/en/docs/advanced/behind-a-proxy.md b/docs/en/docs/advanced/behind-a-proxy.md index 70cd5a1701483..766a218aa344c 100644 --- a/docs/en/docs/advanced/behind-a-proxy.md +++ b/docs/en/docs/advanced/behind-a-proxy.md @@ -44,7 +44,7 @@ proxy --> server The docs UI would also need the OpenAPI schema to declare that this API `server` is located at `/api/v1` (behind the proxy). For example: -```JSON hl_lines="4 5 6 7 8" +```JSON hl_lines="4-8" { "openapi": "3.0.2", // More stuff here @@ -290,13 +290,13 @@ If you pass a custom list of `servers` and there's a `root_path` (because your A For example: -```Python hl_lines="4 5 6 7" +```Python hl_lines="4-7" {!../../../docs_src/behind_a_proxy/tutorial003.py!} ``` Will generate an OpenAPI schema like: -```JSON hl_lines="5 6 7" +```JSON hl_lines="5-7" { "openapi": "3.0.2", // More stuff here diff --git a/docs/en/docs/advanced/custom-request-and-route.md b/docs/en/docs/advanced/custom-request-and-route.md index 6c5e97a18b003..bca0c7603613d 100644 --- a/docs/en/docs/advanced/custom-request-and-route.md +++ b/docs/en/docs/advanced/custom-request-and-route.md @@ -36,7 +36,7 @@ If there's no `gzip` in the header, it will not try to decompress the body. That way, the same route class can handle gzip compressed or uncompressed requests. -```Python hl_lines="8 9 10 11 12 13 14 15" +```Python hl_lines="8-15" {!../../../docs_src/custom_request_and_route/tutorial001.py!} ``` @@ -50,7 +50,7 @@ This method returns a function. And that function is what will receive a request Here we use it to create a `GzipRequest` from the original request. -```Python hl_lines="18 19 20 21 22 23 24 25 26" +```Python hl_lines="18-26" {!../../../docs_src/custom_request_and_route/tutorial001.py!} ``` @@ -84,13 +84,13 @@ We can also use this same approach to access the request body in an exception ha All we need to do is handle the request inside a `try`/`except` block: -```Python hl_lines="13 15" +```Python hl_lines="13 15" {!../../../docs_src/custom_request_and_route/tutorial002.py!} ``` If an exception occurs, the`Request` instance will still be in scope, so we can read and make use of the request body when handling the error: -```Python hl_lines="16 17 18" +```Python hl_lines="16-18" {!../../../docs_src/custom_request_and_route/tutorial002.py!} ``` @@ -104,6 +104,6 @@ You can also set the `route_class` parameter of an `APIRouter`: In this example, the *path operations* under the `router` will use the custom `TimedRoute` class, and will have an extra `X-Response-Time` header in the response with the time it took to generate the response: -```Python hl_lines="13 14 15 16 17 18 19 20" +```Python hl_lines="13-20" {!../../../docs_src/custom_request_and_route/tutorial003.py!} ``` diff --git a/docs/en/docs/advanced/custom-response.md b/docs/en/docs/advanced/custom-response.md index f753cc300fc7a..e9cac6df08487 100644 --- a/docs/en/docs/advanced/custom-response.md +++ b/docs/en/docs/advanced/custom-response.md @@ -21,7 +21,7 @@ For example, if you are squeezing performance, you can install and use unit tests for it: -```Python hl_lines="36 37 38 39 40 41 42" +```Python hl_lines="36-42" {!../../../docs_src/nosql_databases/tutorial001.py!} ``` @@ -145,7 +145,7 @@ As our code is calling Couchbase and we are not using the threads", so, we can just get the bucket directly and pass it to our utility functions: -```Python hl_lines="49 50 51 52 53" +```Python hl_lines="49-53" {!../../../docs_src/nosql_databases/tutorial001.py!} ``` diff --git a/docs/en/docs/advanced/openapi-callbacks.md b/docs/en/docs/advanced/openapi-callbacks.md index 4c3d97e385bd1..e927aea4db6f8 100644 --- a/docs/en/docs/advanced/openapi-callbacks.md +++ b/docs/en/docs/advanced/openapi-callbacks.md @@ -31,7 +31,7 @@ It will have a *path operation* that will receive an `Invoice` body, and a query This part is pretty normal, most of the code is probably already familiar to you: -```Python hl_lines="10 11 12 13 14 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54" +```Python hl_lines="10-14 37-54" {!../../../docs_src/openapi_callbacks/tutorial001.py!} ``` @@ -92,7 +92,7 @@ Because of that, you need to declare what will be the `default_response_class`, But as we are never calling `app.include_router(some_router)`, we need to set the `default_response_class` during creation of the `APIRouter`. -```Python hl_lines="5 26" +```Python hl_lines="5 26" {!../../../docs_src/openapi_callbacks/tutorial001.py!} ``` @@ -105,7 +105,7 @@ It should look just like a normal FastAPI *path operation*: * It should probably have a declaration of the body it should receive, e.g. `body: InvoiceEvent`. * And it could also have a declaration of the response it should return, e.g. `response_model=InvoiceEventReceived`. -```Python hl_lines="17 18 19 22 23 29 30 31 32 33" +```Python hl_lines="17-19 22-23 29-33" {!../../../docs_src/openapi_callbacks/tutorial001.py!} ``` diff --git a/docs/en/docs/advanced/path-operation-advanced-configuration.md b/docs/en/docs/advanced/path-operation-advanced-configuration.md index 7a3247a64ab4d..8d085d7549835 100644 --- a/docs/en/docs/advanced/path-operation-advanced-configuration.md +++ b/docs/en/docs/advanced/path-operation-advanced-configuration.md @@ -19,7 +19,7 @@ If you want to use your APIs' function names as `operationId`s, you can iterate You should do it after adding all your *path operations*. -```Python hl_lines="2 12 13 14 15 16 17 18 19 20 21 24" +```Python hl_lines="2 12-21 24" {!../../../docs_src/path_operation_advanced_configuration/tutorial002.py!} ``` @@ -47,6 +47,6 @@ Adding an `\f` (an escaped "form feed" character) causes **FastAPI** to truncate It won't show up in the documentation, but other tools (such as Sphinx) will be able to use the rest. -```Python hl_lines="19 20 21 22 23 24 25 26 27 28 29" +```Python hl_lines="19-29" {!../../../docs_src/path_operation_advanced_configuration/tutorial004.py!} ``` diff --git a/docs/en/docs/advanced/response-cookies.md b/docs/en/docs/advanced/response-cookies.md index e46ae755f79d9..9178ef81621db 100644 --- a/docs/en/docs/advanced/response-cookies.md +++ b/docs/en/docs/advanced/response-cookies.md @@ -6,7 +6,7 @@ You can declare a parameter of type `Response` in your *path operation function* And then you can set cookies in that *temporal* response object. -```Python hl_lines="1 8 9" +```Python hl_lines="1 8-9" {!../../../docs_src/response_cookies/tutorial002.py!} ``` @@ -26,7 +26,7 @@ To do that, you can create a response as described in [Return a Response Directl Then set Cookies in it, and then return it: -```Python hl_lines="10 11 12" +```Python hl_lines="10-12" {!../../../docs_src/response_cookies/tutorial001.py!} ``` diff --git a/docs/en/docs/advanced/response-directly.md b/docs/en/docs/advanced/response-directly.md index f0ab2831d5a9c..8836140ecf7ee 100644 --- a/docs/en/docs/advanced/response-directly.md +++ b/docs/en/docs/advanced/response-directly.md @@ -31,7 +31,7 @@ For example, you cannot put a Pydantic model in a `JSONResponse` without first c For those cases, you can use the `jsonable_encoder` to convert your data before passing it to a response: -```Python hl_lines="6 7 21 22" +```Python hl_lines="6-7 21-22" {!../../../docs_src/response_directly/tutorial001.py!} ``` diff --git a/docs/en/docs/advanced/response-headers.md b/docs/en/docs/advanced/response-headers.md index 914bd44017a86..758bd64556c85 100644 --- a/docs/en/docs/advanced/response-headers.md +++ b/docs/en/docs/advanced/response-headers.md @@ -6,7 +6,7 @@ You can declare a parameter of type `Response` in your *path operation function* And then you can set headers in that *temporal* response object. -```Python hl_lines="1 7 8" +```Python hl_lines="1 7-8" {!../../../docs_src/response_headers/tutorial002.py!} ``` @@ -24,7 +24,7 @@ You can also add headers when you return a `Response` directly. Create a response as described in [Return a Response Directly](response-directly.md){.internal-link target=_blank} and pass the headers as an additional parameter: -```Python hl_lines="10 11 12" +```Python hl_lines="10-12" {!../../../docs_src/response_headers/tutorial001.py!} ``` diff --git a/docs/en/docs/advanced/security/http-basic-auth.md b/docs/en/docs/advanced/security/http-basic-auth.md index c0ebea4fed9f7..6c589cd9afe32 100644 --- a/docs/en/docs/advanced/security/http-basic-auth.md +++ b/docs/en/docs/advanced/security/http-basic-auth.md @@ -20,7 +20,7 @@ Then, when you type that username and password, the browser sends them in the he * It returns an object of type `HTTPBasicCredentials`: * It contains the `username` and `password` sent. -```Python hl_lines="2 6 10" +```Python hl_lines="2 6 10" {!../../../docs_src/security/tutorial006.py!} ``` @@ -36,7 +36,7 @@ Use a dependency to check if the username and password are correct. For this, use the Python standard module `secrets` to check the username and password: -```Python hl_lines="1 11 12 13" +```Python hl_lines="1 11-13" {!../../../docs_src/security/tutorial007.py!} ``` @@ -102,6 +102,6 @@ That way, using `secrets.compare_digest()` in your application code, it will be After detecting that the credentials are incorrect, return an `HTTPException` with a status code 401 (the same returned when no credentials are provided) and add the header `WWW-Authenticate` to make the browser show the login prompt again: -```Python hl_lines="15 16 17 18 19" +```Python hl_lines="15-19" {!../../../docs_src/security/tutorial007.py!} ``` diff --git a/docs/en/docs/advanced/security/oauth2-scopes.md b/docs/en/docs/advanced/security/oauth2-scopes.md index 5b3d57d7fa2e1..c40695ba6c044 100644 --- a/docs/en/docs/advanced/security/oauth2-scopes.md +++ b/docs/en/docs/advanced/security/oauth2-scopes.md @@ -56,7 +56,7 @@ They are normally used to declare specific security permissions, for example: First, let's quickly see the parts that change from the examples in the main **Tutorial - User Guide** for [OAuth2 with Password (and hashing), Bearer with JWT tokens](../../tutorial/security/oauth2-jwt.md){.internal-link target=_blank}. Now using OAuth2 scopes: -```Python hl_lines="2 4 8 12 46 64 105 107 108 109 110 111 112 113 114 115 121 122 123 124 128 129 130 131 132 133 134 139 153" +```Python hl_lines="2 4 8 12 46 64 105 107-115 121-124 128-134 139 153" {!../../../docs_src/security/tutorial005.py!} ``` @@ -68,7 +68,7 @@ The first change is that now we are declaring the OAuth2 security scheme with tw The `scopes` parameter receives a `dict` with each scope as a key and the description as the value: -```Python hl_lines="62 63 64 65" +```Python hl_lines="62-65" {!../../../docs_src/security/tutorial005.py!} ``` @@ -159,7 +159,7 @@ We create an `HTTPException` that we can re-use (`raise`) later at several point In this exception, we include the scopes required (if any) as a string separated by spaces (using `scope_str`). We put that string containing the scopes in in the `WWW-Authenticate` header (this is part of the spec). -```Python hl_lines="105 107 108 109 110 111 112 113 114 115" +```Python hl_lines="105 107-115" {!../../../docs_src/security/tutorial005.py!} ``` @@ -177,7 +177,7 @@ Instead of, for example, a `dict`, or something else, as it could break the appl We also verify that we have a user with that username, and if not, we raise that same exception we created before. -```Python hl_lines="46 116 117 118 119 120 121 122 123 124 125 126 127" +```Python hl_lines="46 116-127" {!../../../docs_src/security/tutorial005.py!} ``` @@ -187,7 +187,7 @@ We now verify that all the scopes required, by this dependency and all the depen For this, we use `security_scopes.scopes`, that contains a `list` with all these scopes as `str`. -```Python hl_lines="128 129 130 131 132 133 134" +```Python hl_lines="128-134" {!../../../docs_src/security/tutorial005.py!} ``` diff --git a/docs/en/docs/advanced/settings.md b/docs/en/docs/advanced/settings.md index f30de226c7c22..706a71173e76f 100644 --- a/docs/en/docs/advanced/settings.md +++ b/docs/en/docs/advanced/settings.md @@ -135,7 +135,7 @@ The same way as with Pydantic models, you declare class attributes with type ann You can use all the same validation features and tools you use for Pydantic models, like different data types and additional validations with `Field()`. -```Python hl_lines="2 5 6 7 8 11" +```Python hl_lines="2 5-8 11" {!../../../docs_src/settings/tutorial001.py!} ``` @@ -150,7 +150,7 @@ Next it will convert and validate the data. So, when you use that `settings` obj Then you can use the new `settings` object in your application: -```Python hl_lines="18 19 20" +```Python hl_lines="18-20" {!../../../docs_src/settings/tutorial001.py!} ``` @@ -189,7 +189,7 @@ For example, you could have a file `config.py` with: And then use it in a file `main.py`: -```Python hl_lines="3 11 12 13" +```Python hl_lines="3 11-13" {!../../../docs_src/settings/app01/main.py!} ``` @@ -216,7 +216,7 @@ Notice that now we don't create a default instance `settings = Settings()`. Now we create a dependency that returns a new `config.Settings()`. -```Python hl_lines="5 11 12" +```Python hl_lines="5 11-12" {!../../../docs_src/settings/app02/main.py!} ``` @@ -227,7 +227,7 @@ Now we create a dependency that returns a new `config.Settings()`. And then we can require it from the *path operation function* as a dependency and use it anywhere we need it. -```Python hl_lines="16 18 19 20" +```Python hl_lines="16 18-20" {!../../../docs_src/settings/app02/main.py!} ``` @@ -235,7 +235,7 @@ And then we can require it from the *path operation function* as a dependency an Then it would be very easy to provide a different settings object during testing by creating a dependency override for `get_settings`: -```Python hl_lines="8 9 12 21" +```Python hl_lines="8-9 12 21" {!../../../docs_src/settings/app02/test_main.py!} ``` @@ -272,7 +272,7 @@ APP_NAME="ChimichangApp" And then update your `config.py` with: -```Python hl_lines="9 10" +```Python hl_lines="9-10" {!../../../docs_src/settings/app03/config.py!} ``` diff --git a/docs/en/docs/advanced/sql-databases-peewee.md b/docs/en/docs/advanced/sql-databases-peewee.md index 7610290066b01..536a08fb9604a 100644 --- a/docs/en/docs/advanced/sql-databases-peewee.md +++ b/docs/en/docs/advanced/sql-databases-peewee.md @@ -113,7 +113,7 @@ This might seem a bit complex (and it actually is), you don't really need to com We will create a `PeeweeConnectionState`: -```Python hl_lines="10 11 12 13 14 15 16 17 18 19" +```Python hl_lines="10-19" {!../../../docs_src/sql_databases_peewee/sql_app/database.py!} ``` @@ -161,7 +161,7 @@ This is the same you would do if you followed the Peewee tutorial and updated th Import `db` from `database` (the file `database.py` from above) and use it here. -```Python hl_lines="3 6 7 8 9 10 11 12 15 16 17 18 19 20 21" +```Python hl_lines="3 6-12 15-21" {!../../../docs_src/sql_databases_peewee/sql_app/models.py!} ``` @@ -189,7 +189,7 @@ Now let's check the file `sql_app/schemas.py`. Create all the same Pydantic models as in the SQLAlchemy tutorial: -```Python hl_lines="16 17 18 21 22 25 26 27 28 29 30 34 35 38 39 42 43 44 45 46 47 48" +```Python hl_lines="16-18 21-22 25-30 34-35 38-39 42-48" {!../../../docs_src/sql_databases_peewee/sql_app/schemas.py!} ``` @@ -214,7 +214,7 @@ But recent versions of Pydantic allow providing a custom class that inherits fro We are going to create a custom `PeeweeGetterDict` class and use it in all the same Pydantic *models* / schemas that use `orm_mode`: -```Python hl_lines="3 8 9 10 11 12 13 31 49" +```Python hl_lines="3 8-13 31 49" {!../../../docs_src/sql_databases_peewee/sql_app/schemas.py!} ``` @@ -235,7 +235,7 @@ Now let's see the file `sql_app/crud.py`. Create all the same CRUD utils as in the SQLAlchemy tutorial, all the code is very similar: -```Python hl_lines="1 4 5 8 9 12 13 16 17 18 19 20 23 24 27 28 29 30" +```Python hl_lines="1 4-5 8-9 12-13 16-20 23-24 27-30" {!../../../docs_src/sql_databases_peewee/sql_app/crud.py!} ``` @@ -259,7 +259,7 @@ And now in the file `sql_app/main.py` let's integrate and use all the other part In a very simplistic way create the database tables: -```Python hl_lines="9 10 11" +```Python hl_lines="9-11" {!../../../docs_src/sql_databases_peewee/sql_app/main.py!} ``` @@ -267,7 +267,7 @@ In a very simplistic way create the database tables: Create a dependency that will connect the database right at the beginning of a request and disconnect it at the end: -```Python hl_lines="23 24 25 26 27 28 29" +```Python hl_lines="23-29" {!../../../docs_src/sql_databases_peewee/sql_app/main.py!} ``` @@ -291,7 +291,7 @@ For all the `contextvars` parts to work, we need to make sure we have an indepen For that, we need to create another `async` dependency `reset_db_state()` that is used as a sub-dependency in `get_db()`. It will set the value for the context variable (with just a default `dict`) that will be used as the database state for the whole request. And then the dependency `get_db()` will store in it the database state (connection, transactions, etc). -```Python hl_lines="18 19 20" +```Python hl_lines="18-20" {!../../../docs_src/sql_databases_peewee/sql_app/main.py!} ``` @@ -310,7 +310,7 @@ If you are using a `Union`, include the most specific type first, followed by the less specific type. In the example below, the more specific `PlaneItem` comes before `CarItem` in `Union[PlaneItem, CarItem]`. -```Python hl_lines="1 14 15 18 19 20 33" +```Python hl_lines="1 14-15 18-20 33" {!../../../docs_src/extra_models/tutorial003.py!} ``` @@ -175,7 +175,7 @@ The same way, you can declare responses of lists of objects. For that, use the standard Python `typing.List`: -```Python hl_lines="1 20" +```Python hl_lines="1 20" {!../../../docs_src/extra_models/tutorial004.py!} ``` @@ -187,7 +187,7 @@ This is useful if you don't know the valid field/attribute names (that would be In this case, you can use `typing.Dict`: -```Python hl_lines="1 8" +```Python hl_lines="1 8" {!../../../docs_src/extra_models/tutorial005.py!} ``` diff --git a/docs/en/docs/tutorial/handling-errors.md b/docs/en/docs/tutorial/handling-errors.md index 714c4c94a57ad..9ea2f2ad840be 100644 --- a/docs/en/docs/tutorial/handling-errors.md +++ b/docs/en/docs/tutorial/handling-errors.md @@ -92,7 +92,7 @@ And you want to handle this exception globally with FastAPI. You could add a custom exception handler with `@app.exception_handler()`: -```Python hl_lines="5 6 7 13 14 15 16 17 18 24" +```Python hl_lines="5-7 13-18 24" {!../../../docs_src/handling_errors/tutorial003.py!} ``` @@ -129,7 +129,7 @@ To override it, import the `RequestValidationError` and use it with `@app.except The exception handler will receive a `Request` and the exception. -```Python hl_lines="2 14 15 16" +```Python hl_lines="2 14-16" {!../../../docs_src/handling_errors/tutorial004.py!} ``` @@ -179,7 +179,7 @@ The same way, you can override the `HTTPException` handler. For example, you could want to return a plain text response instead of JSON for these errors: -```Python hl_lines="3 4 9 10 11 22" +```Python hl_lines="3-4 9-11 22" {!../../../docs_src/handling_errors/tutorial004.py!} ``` @@ -209,7 +209,7 @@ Now try sending an invalid item like: You will receive a response telling you that the data is invalid containing the received body: -```JSON hl_lines="12 13 14 15" +```JSON hl_lines="12-15" { "detail": [ { @@ -256,7 +256,7 @@ You could also just want to use the exception somehow, but then use the same def You can import and re-use the default exception handlers from `fastapi.exception_handlers`: -```Python hl_lines="2 3 4 5 15 21" +```Python hl_lines="2-5 15 21" {!../../../docs_src/handling_errors/tutorial006.py!} ``` diff --git a/docs/en/docs/tutorial/metadata.md b/docs/en/docs/tutorial/metadata.md index 25c00db7052ff..7ec3054218a16 100644 --- a/docs/en/docs/tutorial/metadata.md +++ b/docs/en/docs/tutorial/metadata.md @@ -13,7 +13,7 @@ You can set the: To set them, use the parameters `title`, `description`, and `version`: -```Python hl_lines="4 5 6" +```Python hl_lines="4-6" {!../../../docs_src/metadata/tutorial001.py!} ``` @@ -41,7 +41,7 @@ Let's try that in an example with tags for `users` and `items`. Create metadata for your tags and pass it to the `openapi_tags` parameter: -```Python hl_lines="3 4 5 6 7 8 9 10 11 12 13 14 15 16 18" +```Python hl_lines="3-16 18" {!../../../docs_src/metadata/tutorial004.py!} ``` diff --git a/docs/en/docs/tutorial/middleware.md b/docs/en/docs/tutorial/middleware.md index 6f7ca000addf9..3c6868fe4de73 100644 --- a/docs/en/docs/tutorial/middleware.md +++ b/docs/en/docs/tutorial/middleware.md @@ -28,7 +28,7 @@ The middleware function receives: * Then it returns the `response` generated by the corresponding *path operation*. * You can then modify further the `response` before returning it. -```Python hl_lines="8 9 11 14" +```Python hl_lines="8-9 11 14" {!../../../docs_src/middleware/tutorial001.py!} ``` @@ -50,7 +50,7 @@ And also after the `response` is generated, before returning it. For example, you could add a custom header `X-Process-Time` containing the time in seconds that it took to process the request and generate a response: -```Python hl_lines="10 12 13" +```Python hl_lines="10 12-13" {!../../../docs_src/middleware/tutorial001.py!} ``` diff --git a/docs/en/docs/tutorial/path-operation-configuration.md b/docs/en/docs/tutorial/path-operation-configuration.md index c2c3008944157..0d606331db2f9 100644 --- a/docs/en/docs/tutorial/path-operation-configuration.md +++ b/docs/en/docs/tutorial/path-operation-configuration.md @@ -28,7 +28,7 @@ That status code will be used in the response and will be added to the OpenAPI s You can add tags to your *path operation*, pass the parameter `tags` with a `list` of `str` (commonly just one `str`): -```Python hl_lines="17 22 27" +```Python hl_lines="17 22 27" {!../../../docs_src/path_operation_configuration/tutorial002.py!} ``` @@ -40,7 +40,7 @@ They will be added to the OpenAPI schema and used by the automatic documentation You can add a `summary` and `description`: -```Python hl_lines="20 21" +```Python hl_lines="20-21" {!../../../docs_src/path_operation_configuration/tutorial003.py!} ``` @@ -50,7 +50,7 @@ As descriptions tend to be long and cover multiple lines, you can declare the *p You can write Markdown in the docstring, it will be interpreted and displayed correctly (taking into account docstring indentation). -```Python hl_lines="19 20 21 22 23 24 25 26 27" +```Python hl_lines="19-27" {!../../../docs_src/path_operation_configuration/tutorial004.py!} ``` diff --git a/docs/en/docs/tutorial/path-params.md b/docs/en/docs/tutorial/path-params.md index 77c5c02e2fb5d..4f59069fd28ca 100644 --- a/docs/en/docs/tutorial/path-params.md +++ b/docs/en/docs/tutorial/path-params.md @@ -2,7 +2,7 @@ You can declare path "parameters" or "variables" with the same syntax used by Python format strings: -```Python hl_lines="6 7" +```Python hl_lines="6-7" {!../../../docs_src/path_params/tutorial001.py!} ``` @@ -109,7 +109,7 @@ And then you can also have a path `/users/{user_id}` to get data about a specifi Because *path operations* are evaluated in order, you need to make sure that the path for `/users/me` is declared before the one for `/users/{user_id}`: -```Python hl_lines="6 11" +```Python hl_lines="6 11" {!../../../docs_src/path_params/tutorial003.py!} ``` @@ -127,7 +127,7 @@ By inheriting from `str` the API docs will be able to know that the values must Then create class attributes with fixed values, which will be the available valid values: -```Python hl_lines="1 6 7 8 9" +```Python hl_lines="1 6-9" {!../../../docs_src/path_params/tutorial005.py!} ``` diff --git a/docs/en/docs/tutorial/query-params.md b/docs/en/docs/tutorial/query-params.md index d5dec36def324..f741b99a00d35 100644 --- a/docs/en/docs/tutorial/query-params.md +++ b/docs/en/docs/tutorial/query-params.md @@ -126,7 +126,7 @@ And you don't have to declare them in any specific order. They will be detected by name: -```Python hl_lines="8 10" +```Python hl_lines="8 10" {!../../../docs_src/query_params/tutorial004.py!} ``` @@ -138,7 +138,7 @@ If you don't want to add a specific value but just make it optional, set the def But when you want to make a query parameter required, you can just not declare any default value: -```Python hl_lines="6 7" +```Python hl_lines="6-7" {!../../../docs_src/query_params/tutorial005.py!} ``` diff --git a/docs/en/docs/tutorial/request-files.md b/docs/en/docs/tutorial/request-files.md index 260a5e1905aeb..3bdf8d0022bdd 100644 --- a/docs/en/docs/tutorial/request-files.md +++ b/docs/en/docs/tutorial/request-files.md @@ -121,7 +121,7 @@ They would be associated to the same "form field" sent using "form data". To use that, declare a `List` of `bytes` or `UploadFile`: -```Python hl_lines="10 15" +```Python hl_lines="10 15" {!../../../docs_src/request_files/tutorial002.py!} ``` diff --git a/docs/en/docs/tutorial/response-model.md b/docs/en/docs/tutorial/response-model.md index b7201ee77d723..fdf6611348a53 100644 --- a/docs/en/docs/tutorial/response-model.md +++ b/docs/en/docs/tutorial/response-model.md @@ -35,13 +35,13 @@ But most importantly: Here we are declaring a `UserIn` model, it will contain a plaintext password: -```Python hl_lines="9 11" +```Python hl_lines="9 11" {!../../../docs_src/response_model/tutorial002.py!} ``` And we are using this model to declare our input and the same model to declare our output: -```Python hl_lines="17 18" +```Python hl_lines="17-18" {!../../../docs_src/response_model/tutorial002.py!} ``` @@ -58,7 +58,7 @@ But if we use the same model for another *path operation*, we could be sending o We can instead create an input model with the plaintext password and an output model without it: -```Python hl_lines="9 11 16" +```Python hl_lines="9 11 16" {!../../../docs_src/response_model/tutorial003.py!} ``` @@ -90,7 +90,7 @@ And both models will be used for the interactive API documentation: Your response model could have default values, like: -```Python hl_lines="11 13 14" +```Python hl_lines="11 13-14" {!../../../docs_src/response_model/tutorial004.py!} ``` @@ -136,7 +136,7 @@ So, if you send a request to that *path operation* for the item with ID `foo`, t But if your data has values for the model's fields with default values, like the item with ID `bar`: -```Python hl_lines="3 5" +```Python hl_lines="3 5" { "name": "Bar", "description": "The bartenders", @@ -151,7 +151,7 @@ they will be included in the response. If the data has the same values as the default ones, like the item with ID `baz`: -```Python hl_lines="3 5 6" +```Python hl_lines="3 5-6" { "name": "Baz", "description": None, @@ -185,7 +185,7 @@ This can be used as a quick shortcut if you have only one Pydantic model and wan This also applies to `response_model_by_alias` that works similarly. -```Python hl_lines="31 37" +```Python hl_lines="31 37" {!../../../docs_src/response_model/tutorial005.py!} ``` @@ -198,7 +198,7 @@ This can be used as a quick shortcut if you have only one Pydantic model and wan If you forget to use a `set` and use a `list` or `tuple` instead, FastAPI will still convert it to a `set` and it will work correctly: -```Python hl_lines="31 37" +```Python hl_lines="31 37" {!../../../docs_src/response_model/tutorial006.py!} ``` diff --git a/docs/en/docs/tutorial/response-status-code.md b/docs/en/docs/tutorial/response-status-code.md index 7915624fa67ee..646378aa105ac 100644 --- a/docs/en/docs/tutorial/response-status-code.md +++ b/docs/en/docs/tutorial/response-status-code.md @@ -71,7 +71,7 @@ But you don't have to memorize what each of these codes mean. You can use the convenience variables from `fastapi.status`. -```Python hl_lines="1 6" +```Python hl_lines="1 6" {!../../../docs_src/response_status_code/tutorial002.py!} ``` diff --git a/docs/en/docs/tutorial/schema-extra-example.md b/docs/en/docs/tutorial/schema-extra-example.md index d0f1b843e57ee..d1f9bfec44de3 100644 --- a/docs/en/docs/tutorial/schema-extra-example.md +++ b/docs/en/docs/tutorial/schema-extra-example.md @@ -10,7 +10,7 @@ There are several ways you can declare extra JSON Schema information. You can declare an example for a Pydantic model using `Config` and `schema_extra`, as described in Pydantic's docs: Schema customization: -```Python hl_lines="15 16 17 18 19 20 21 22 23" +```Python hl_lines="15-23" {!../../../docs_src/schema_extra_example/tutorial001.py!} ``` @@ -20,7 +20,7 @@ That extra info will be added as-is to the output JSON Schema. In `Field`, `Path`, `Query`, `Body` and others you'll see later, you can also declare extra info for the JSON Schema by passing any other arbitrary arguments to the function, for example, to add an `example`: -```Python hl_lines="4 10 11 12 13" +```Python hl_lines="4 10-13" {!../../../docs_src/schema_extra_example/tutorial002.py!} ``` @@ -33,7 +33,7 @@ The same way you can pass extra info to `Field`, you can do the same with `Path` For example, you can pass an `example` for a body request to `Body`: -```Python hl_lines="21 22 23 24 25 26" +```Python hl_lines="21-26" {!../../../docs_src/schema_extra_example/tutorial003.py!} ``` diff --git a/docs/en/docs/tutorial/security/get-current-user.md b/docs/en/docs/tutorial/security/get-current-user.md index 3fc0164c3263f..f50b69acf0228 100644 --- a/docs/en/docs/tutorial/security/get-current-user.md +++ b/docs/en/docs/tutorial/security/get-current-user.md @@ -16,7 +16,7 @@ First, let's create a Pydantic user model. The same way we use Pydantic to declare bodies, we can use it anywhere else: -```Python hl_lines="5 12 13 14 15 16" +```Python hl_lines="5 12-16" {!../../../docs_src/security/tutorial002.py!} ``` @@ -38,7 +38,7 @@ The same as we were doing before in the *path operation* directly, our new depen `get_current_user` will use a (fake) utility function we created, that takes a token as a `str` and returns our Pydantic `User` model: -```Python hl_lines="19 20 21 22 26 27" +```Python hl_lines="19-22 26-27" {!../../../docs_src/security/tutorial002.py!} ``` @@ -98,7 +98,7 @@ And all of them (or any portion of them that you want) can take the advantage of And all these thousands of *path operations* can be as small as 3 lines: -```Python hl_lines="30 31 32" +```Python hl_lines="30-32" {!../../../docs_src/security/tutorial002.py!} ``` diff --git a/docs/en/docs/tutorial/security/oauth2-jwt.md b/docs/en/docs/tutorial/security/oauth2-jwt.md index b65a9f58c28b2..47b0d75d04d10 100644 --- a/docs/en/docs/tutorial/security/oauth2-jwt.md +++ b/docs/en/docs/tutorial/security/oauth2-jwt.md @@ -109,7 +109,7 @@ And another utility to verify if a received password matches the hash stored. And another one to authenticate and return a user. -```Python hl_lines="7 48 55 56 59 60 69 70 71 72 73 74 75" +```Python hl_lines="7 48 55-56 59-60 69-75" {!../../../docs_src/security/tutorial004.py!} ``` @@ -144,7 +144,7 @@ Define a Pydantic Model that will be used in the token endpoint for the response Create a utility function to generate a new access token. -```Python hl_lines="6 12 13 14 28 29 30 78 79 80 81 82 83 84 85 86" +```Python hl_lines="6 12-14 28-30 78-86" {!../../../docs_src/security/tutorial004.py!} ``` @@ -156,7 +156,7 @@ Decode the received token, verify it, and return the current user. If the token is invalid, return an HTTP error right away. -```Python hl_lines="89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106" +```Python hl_lines="89-106" {!../../../docs_src/security/tutorial004.py!} ``` @@ -166,7 +166,7 @@ Create a `timedelta` with the expiration time of the token. Create a real JWT access token and return it. -```Python hl_lines="115 116 117 118 119 120 121 122 123 124 125 126 127 128" +```Python hl_lines="115-128" {!../../../docs_src/security/tutorial004.py!} ``` diff --git a/docs/en/docs/tutorial/security/simple-oauth2.md b/docs/en/docs/tutorial/security/simple-oauth2.md index e6882c7e2c23b..2aa747847ad54 100644 --- a/docs/en/docs/tutorial/security/simple-oauth2.md +++ b/docs/en/docs/tutorial/security/simple-oauth2.md @@ -90,7 +90,7 @@ If there is no such user, we return an error saying "incorrect username or passw For the error, we use the exception `HTTPException`: -```Python hl_lines="3 77 78 79" +```Python hl_lines="3 77-79" {!../../../docs_src/security/tutorial003.py!} ``` @@ -118,7 +118,7 @@ If your database is stolen, the thief won't have your users' plaintext passwords So, the thief won't be able to try to use those same passwords in another system (as many users use the same password everywhere, this would be dangerous). -```Python hl_lines="80 81 82 83" +```Python hl_lines="80-83" {!../../../docs_src/security/tutorial003.py!} ``` @@ -181,7 +181,7 @@ Both of these dependencies will just return an HTTP error if the user doesn't ex So, in our endpoint, we will only get a user if the user exists, was correctly authenticated, and is active: -```Python hl_lines="58 59 60 61 62 63 64 65 66 67 69 70 71 72 90" +```Python hl_lines="58-67 69-72 90" {!../../../docs_src/security/tutorial003.py!} ``` diff --git a/docs/en/docs/tutorial/sql-databases.md b/docs/en/docs/tutorial/sql-databases.md index a3f7fae052d24..59eb6a4fb23dc 100644 --- a/docs/en/docs/tutorial/sql-databases.md +++ b/docs/en/docs/tutorial/sql-databases.md @@ -86,13 +86,13 @@ Let's refer to the file `sql_app/database.py`. ### Import the SQLAlchemy parts -```Python hl_lines="1 2 3" +```Python hl_lines="1-3" {!../../../docs_src/sql_databases/sql_app/database.py!} ``` ### Create a database URL for SQLAlchemy -```Python hl_lines="5 6" +```Python hl_lines="5-6" {!../../../docs_src/sql_databases/sql_app/database.py!} ``` @@ -120,7 +120,7 @@ The first step is to create a SQLAlchemy "engine". We will later use this `engine` in other places. -```Python hl_lines="8 9 10" +```Python hl_lines="8-10" {!../../../docs_src/sql_databases/sql_app/database.py!} ``` @@ -189,7 +189,7 @@ Create classes that inherit from it. These classes are the SQLAlchemy models. -```Python hl_lines="4 7 8 18 19" +```Python hl_lines="4 7-8 18-19" {!../../../docs_src/sql_databases/sql_app/models.py!} ``` @@ -205,7 +205,7 @@ We use `Column` from SQLAlchemy as the default value. And we pass a SQLAlchemy class "type", as `Integer`, `String`, and `Boolean`, that defines the type in the database, as an argument. -```Python hl_lines="1 10 11 12 13 21 22 23 24" +```Python hl_lines="1 10-13 21-24" {!../../../docs_src/sql_databases/sql_app/models.py!} ``` @@ -217,7 +217,7 @@ For this, we use `relationship` provided by SQLAlchemy ORM. This will become, more or less, a "magic" attribute that will contain the values from other tables related to this one. -```Python hl_lines="2 15 26" +```Python hl_lines="2 15 26" {!../../../docs_src/sql_databases/sql_app/models.py!} ``` @@ -248,7 +248,7 @@ So, the user will also have a `password` when creating it. But for security, the `password` won't be in other Pydantic *models*, for example, it won't be sent from the API when reading a user. -```Python hl_lines="3 6 7 8 11 12 23 24 27 28" +```Python hl_lines="3 6-8 11-12 23-24 27-28" {!../../../docs_src/sql_databases/sql_app/schemas.py!} ``` @@ -278,7 +278,7 @@ The same way, when reading a user, we can now declare that `items` will contain Not only the IDs of those items, but all the data that we defined in the Pydantic *model* for reading items: `Item`. -```Python hl_lines="15 16 17 31 32 33 34" +```Python hl_lines="15-17 31-34" {!../../../docs_src/sql_databases/sql_app/schemas.py!} ``` @@ -293,7 +293,7 @@ This