mirror of
https://github.com/fastapi/fastapi.git
synced 2026-06-19 21:08:47 -04:00
Compare commits
9 Commits
0.137.2
...
translate-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab057beddb | ||
|
|
2d00470875 | ||
|
|
6e6c74fc16 | ||
|
|
1b68e378ef | ||
|
|
da9d05bc7a | ||
|
|
c61384e3d7 | ||
|
|
08321394ab | ||
|
|
c744991f96 | ||
|
|
2f4ba0bdd1 |
@@ -432,13 +432,13 @@ For a more complete example including more features, see the <a href="https://fa
|
||||
|
||||
**Spoiler alert**: the tutorial - user guide includes:
|
||||
|
||||
* Declaration of **parameters** from other different places as: **headers**, **cookies**, **form fields** and **files**.
|
||||
* How to set **validation constraints** as `maximum_length` or `regex`.
|
||||
* Declaration of **parameters** from other different places such as: **headers**, **cookies**, **form fields** and **files**.
|
||||
* How to set **validation constraints** such as `maximum_length` or `regex`.
|
||||
* A very powerful and easy to use **<dfn title="also known as components, resources, providers, services, injectables">Dependency Injection</dfn>** system.
|
||||
* Security and authentication, including support for **OAuth2** with **JWT tokens** and **HTTP Basic** auth.
|
||||
* More advanced (but equally easy) techniques for declaring **deeply nested JSON models** (thanks to Pydantic).
|
||||
* **GraphQL** integration with [Strawberry](https://strawberry.rocks) and other libraries.
|
||||
* Many extra features (thanks to Starlette) as:
|
||||
* Many extra features (thanks to Starlette) such as:
|
||||
* **WebSockets**
|
||||
* extremely easy tests based on HTTPX and `pytest`
|
||||
* **CORS**
|
||||
|
||||
@@ -185,7 +185,7 @@ See section `### Links` in the general prompt in `scripts/translate.py`.
|
||||
|
||||
//// tab | Test
|
||||
|
||||
Here some things wrapped in HTML "abbr" elements (Some are invented):
|
||||
Here are some things wrapped in HTML "abbr" elements (Some are invented):
|
||||
|
||||
### The abbr gives a full phrase { #the-abbr-gives-a-full-phrase }
|
||||
|
||||
@@ -488,7 +488,7 @@ For some language specific instructions, see e.g. section `### Headings` in `doc
|
||||
|
||||
//// tab | Info
|
||||
|
||||
This is a not complete and not normative list of (mostly) technical terms seen in the docs. It may be helpful for the prompt designer to figure out for which terms the LLM needs a helping hand. For example when it keeps reverting a good translation to a suboptimal translation. Or when it has problems conjugating/declinating a term in your language.
|
||||
This is neither a complete nor a normative list of (mostly) technical terms seen in the docs. It may be helpful for the prompt designer to figure out for which terms the LLM needs a helping hand. For example when it keeps reverting a good translation to a suboptimal translation. Or when it has problems conjugating/declinating a term in your language.
|
||||
|
||||
See e.g. section `### List of English terms and their preferred German translations` in `docs/de/llm-prompt.md`.
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ It will use the default status code or the one you set in your *path operation*.
|
||||
|
||||
If you want to return additional status codes apart from the main one, you can do that by returning a `Response` directly, like a `JSONResponse`, and set the additional status code directly.
|
||||
|
||||
For example, let's say that you want to have a *path operation* that allows to update items, and returns HTTP status codes of 200 "OK" when successful.
|
||||
For example, let's say that you want to have a *path operation* that allows updating items, and returns HTTP status codes of 200 "OK" when successful.
|
||||
|
||||
But you also want it to accept new items. And when the items didn't exist before, it creates them, and returns an HTTP status code of 201 "Created".
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ checker(q="somequery")
|
||||
|
||||
/// tip
|
||||
|
||||
All this might seem contrived. And it might not be very clear how is it useful yet.
|
||||
All this might seem contrived. And it might not be very clear how it is useful yet.
|
||||
|
||||
These examples are intentionally simple, but show how it all works.
|
||||
|
||||
@@ -112,7 +112,7 @@ For example, imagine you have code that uses a database session in a dependency
|
||||
|
||||
In this case, the database session would be held until the response is finished being sent, but if you don't use it, then it wouldn't be necessary to hold it.
|
||||
|
||||
Here's how it could look like:
|
||||
Here's how it could look:
|
||||
|
||||
{* ../../docs_src/dependencies/tutorial013_an_py310.py *}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ Keep in mind that dataclasses can't do everything Pydantic models can do.
|
||||
|
||||
So, you might still need to use Pydantic models.
|
||||
|
||||
But if you have a bunch of dataclasses laying around, this is a nice trick to use them to power a web API using FastAPI. 🤓
|
||||
But if you have a bunch of dataclasses lying around, this is a nice trick to use them to power a web API using FastAPI. 🤓
|
||||
|
||||
///
|
||||
|
||||
|
||||
@@ -142,7 +142,7 @@ So, we declare the event handler function with standard `def` instead of `async
|
||||
|
||||
There's a high chance that the logic for your *startup* and *shutdown* is connected, you might want to start something and then finish it, acquire a resource and then release it, etc.
|
||||
|
||||
Doing that in separated functions that don't share logic or variables together is more difficult as you would need to store values in global variables or similar tricks.
|
||||
Doing that in separate functions that don't share logic or variables together is more difficult as you would need to store values in global variables or similar tricks.
|
||||
|
||||
Because of that, it's now recommended to instead use the `lifespan` as explained above.
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ FastAPI automatically generates **OpenAPI 3.1** specifications, so any tool you
|
||||
|
||||
This section highlights **venture-backed** and **company-supported** solutions from companies that sponsor FastAPI. These products provide **additional features** and **integrations** on top of high-quality generated SDKs.
|
||||
|
||||
By ✨ [**sponsoring FastAPI**](../help-fastapi.md#sponsor-the-author) ✨, these companies help ensure the framework and its **ecosystem** remain healthy and **sustainable**.
|
||||
By ✨ [**sponsoring FastAPI**](https://github.com/sponsors/tiangolo) ✨, these companies help ensure the framework and its **ecosystem** remain healthy and **sustainable**.
|
||||
|
||||
Their sponsorship also demonstrates a strong commitment to the FastAPI **community** (you), showing that they care not only about offering a **great service** but also about supporting a **robust and thriving framework**, FastAPI. 🙇
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ If your app needs to receive and send JSON data, but you need to include binary
|
||||
|
||||
## Base64 vs Files { #base64-vs-files }
|
||||
|
||||
Consider first if you can use [Request Files](../tutorial/request-files.md) for uploading binary data and [Custom Response - FileResponse](./custom-response.md#fileresponse--fileresponse-) for sending binary data, instead of encoding it in JSON.
|
||||
Consider first if you can use [Request Files](../tutorial/request-files.md) for uploading binary data and [Custom Response - FileResponse](./custom-response.md#fileresponse) for sending binary data, instead of encoding it in JSON.
|
||||
|
||||
JSON can only contain UTF-8 encoded strings, so it can't contain raw bytes.
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ You could create an API with a *path operation* that could trigger a request to
|
||||
|
||||
The process that happens when your API app calls the *external API* is named a "callback". Because the software that the external developer wrote sends a request to your API and then your API *calls back*, sending a request to an *external API* (that was probably created by the same developer).
|
||||
|
||||
In this case, you could want to document how that external API *should* look like. What *path operation* it should have, what body it should expect, what response it should return, etc.
|
||||
In this case, you could want to document how that external API *should* look. What *path operation* it should have, what body it should expect, what response it should return, etc.
|
||||
|
||||
## An app with callbacks { #an-app-with-callbacks }
|
||||
|
||||
@@ -25,7 +25,7 @@ Then your API will (let's imagine):
|
||||
|
||||
## The normal **FastAPI** app { #the-normal-fastapi-app }
|
||||
|
||||
Let's first see how the normal API app would look like before adding the callback.
|
||||
Let's first see how the normal API app would look before adding the callback.
|
||||
|
||||
It will have a *path operation* that will receive an `Invoice` body, and a query parameter `callback_url` that will contain the URL for the callback.
|
||||
|
||||
@@ -56,7 +56,7 @@ httpx.post(callback_url, json={"description": "Invoice paid", "paid": True})
|
||||
|
||||
But possibly the most important part of the callback is making sure that your API user (the external developer) implements the *external API* correctly, according to the data that *your API* is going to send in the request body of the callback, etc.
|
||||
|
||||
So, what we will do next is add the code to document how that *external API* should look like to receive the callback from *your API*.
|
||||
So, what we will do next is add the code to document how that *external API* should look to receive the callback from *your API*.
|
||||
|
||||
That documentation will show up in the Swagger UI at `/docs` in your API, and it will let external developers know how to build the *external API*.
|
||||
|
||||
@@ -72,11 +72,11 @@ When implementing the callback yourself, you could use something like [HTTPX](ht
|
||||
|
||||
## Write the callback documentation code { #write-the-callback-documentation-code }
|
||||
|
||||
This code won't be executed in your app, we only need it to *document* how that *external API* should look like.
|
||||
This code won't be executed in your app, we only need it to *document* how that *external API* should look.
|
||||
|
||||
But, you already know how to easily create automatic documentation for an API with **FastAPI**.
|
||||
|
||||
So we are going to use that same knowledge to document how the *external API* should look like... by creating the *path operation(s)* that the external API should implement (the ones your API will call).
|
||||
So we are going to use that same knowledge to document how the *external API* should look... by creating the *path operation(s)* that the external API should implement (the ones your API will call).
|
||||
|
||||
/// tip
|
||||
|
||||
@@ -181,6 +181,6 @@ Notice that you are not passing the router itself (`invoices_callback_router`) t
|
||||
|
||||
Now you can start your app and go to [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs).
|
||||
|
||||
You will see your docs including a "Callbacks" section for your *path operation* that shows how the *external API* should look like:
|
||||
You will see your docs including a "Callbacks" section for your *path operation* that shows how the *external API* should look:
|
||||
|
||||
<img src="/img/tutorial/openapi-callbacks/image01.png">
|
||||
|
||||
@@ -18,7 +18,7 @@ For those cases, you can use a `Response` parameter.
|
||||
|
||||
You can declare a parameter of type `Response` in your *path operation function* (as you can do for cookies and headers).
|
||||
|
||||
And then you can set the `status_code` in that *temporal* response object.
|
||||
And then you can set the `status_code` in that *temporary* response object.
|
||||
|
||||
{* ../../docs_src/response_change_status_code/tutorial001_py310.py hl[1,9,12] *}
|
||||
|
||||
@@ -26,6 +26,6 @@ And then you can return any object you need, as you normally would (a `dict`, a
|
||||
|
||||
And if you declared a `response_model`, it will still be used to filter and convert the object you returned.
|
||||
|
||||
**FastAPI** will use that *temporal* response to extract the status code (also cookies and headers), and will put them in the final response that contains the value you returned, filtered by any `response_model`.
|
||||
**FastAPI** will use that *temporary* response to extract the status code (also cookies and headers), and will put them in the final response that contains the value you returned, filtered by any `response_model`.
|
||||
|
||||
You can also declare the `Response` parameter in dependencies, and set the status code in them. But keep in mind that the last one to be set will win.
|
||||
|
||||
@@ -4,7 +4,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.
|
||||
And then you can set cookies in that *temporary* response object.
|
||||
|
||||
{* ../../docs_src/response_cookies/tutorial002_py310.py hl[1, 8:9] *}
|
||||
|
||||
@@ -12,7 +12,7 @@ And then you can return any object you need, as you normally would (a `dict`, a
|
||||
|
||||
And if you declared a `response_model`, it will still be used to filter and convert the object you returned.
|
||||
|
||||
**FastAPI** will use that *temporal* response to extract the cookies (also headers and status code), and will put them in the final response that contains the value you returned, filtered by any `response_model`.
|
||||
**FastAPI** will use that *temporary* response to extract the cookies (also headers and status code), and will put them in the final response that contains the value you returned, filtered by any `response_model`.
|
||||
|
||||
You can also declare the `Response` parameter in dependencies, and set cookies (and headers) in them.
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
You can declare a parameter of type `Response` in your *path operation function* (as you can do for cookies).
|
||||
|
||||
And then you can set headers in that *temporal* response object.
|
||||
And then you can set headers in that *temporary* response object.
|
||||
|
||||
{* ../../docs_src/response_headers/tutorial002_py310.py hl[1, 7:8] *}
|
||||
|
||||
@@ -12,7 +12,7 @@ And then you can return any object you need, as you normally would (a `dict`, a
|
||||
|
||||
And if you declared a `response_model`, it will still be used to filter and convert the object you returned.
|
||||
|
||||
**FastAPI** will use that *temporal* response to extract the headers (also cookies and status code), and will put them in the final response that contains the value you returned, filtered by any `response_model`.
|
||||
**FastAPI** will use that *temporary* response to extract the headers (also cookies and status code), and will put them in the final response that contains the value you returned, filtered by any `response_model`.
|
||||
|
||||
You can also declare the `Response` parameter in dependencies, and set headers (and cookies) in them.
|
||||
|
||||
|
||||
@@ -194,11 +194,11 @@ For this, we use `security_scopes.scopes`, that contains a `list` with all these
|
||||
|
||||
Let's review again this dependency tree and the scopes.
|
||||
|
||||
As the `get_current_active_user` dependency has as a sub-dependency on `get_current_user`, the scope `"me"` declared at `get_current_active_user` will be included in the list of required scopes in the `security_scopes.scopes` passed to `get_current_user`.
|
||||
As the `get_current_active_user` dependency has `get_current_user` as a sub-dependency, the scope `"me"` declared at `get_current_active_user` will be included in the list of required scopes in the `security_scopes.scopes` passed to `get_current_user`.
|
||||
|
||||
The *path operation* itself also declares a scope, `"items"`, so this will also be in the list of `security_scopes.scopes` passed to `get_current_user`.
|
||||
|
||||
Here's how the hierarchy of dependencies and scopes looks like:
|
||||
Here's what the hierarchy of dependencies and scopes looks like:
|
||||
|
||||
* The *path operation* `read_own_items` has:
|
||||
* Required scopes `["items"]` with the dependency:
|
||||
|
||||
@@ -14,7 +14,7 @@ To understand environment variables you can read [Environment Variables](../envi
|
||||
|
||||
## Types and validation { #types-and-validation }
|
||||
|
||||
These environment variables can only handle text strings, as they are external to Python and have to be compatible with other programs and the rest of the system (and even with different operating systems, as Linux, Windows, macOS).
|
||||
These environment variables can only handle text strings, as they are external to Python and have to be compatible with other programs and the rest of the system (and even with different operating systems, such as Linux, Windows, and macOS).
|
||||
|
||||
That means that any value read in Python from an environment variable will be a `str`, and any conversion to a different type or any validation has to be done in code.
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ Added in FastAPI 0.134.0.
|
||||
|
||||
You could use this if you want to stream pure strings, for example directly from the output of an **AI LLM** service.
|
||||
|
||||
You could also use it to stream **large binary files**, where you stream each chunk of data as you read it, without having to read it all in memory at once.
|
||||
You could also use it to stream **large binary files**, where you stream each chunk of data as you read it, without having to read it all into memory at once.
|
||||
|
||||
You could also stream **video** or **audio** this way, it could even be generated as you process and send it.
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ And then mount that under a path.
|
||||
|
||||
Previously, it was recommended to use `WSGIMiddleware` from `fastapi.middleware.wsgi`, but it is now deprecated.
|
||||
|
||||
It’s advised to use the `a2wsgi` package instead. The usage remains the same.
|
||||
It's advised to use the `a2wsgi` package instead. The usage remains the same.
|
||||
|
||||
Just ensure that you have the `a2wsgi` package installed and import `WSGIMiddleware` correctly from `a2wsgi`.
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ It was created to generate the HTML in the backend, not to create APIs used by a
|
||||
|
||||
### [Django REST Framework](https://www.django-rest-framework.org/) { #django-rest-framework }
|
||||
|
||||
Django REST framework was created to be a flexible toolkit for building Web APIs using Django underneath, to improve its API capabilities.
|
||||
Django REST Framework was created to be a flexible toolkit for building Web APIs using Django underneath, to improve its API capabilities.
|
||||
|
||||
It is used by many companies including Mozilla, Red Hat and Eventbrite.
|
||||
|
||||
@@ -345,7 +345,7 @@ Hug was created by Timothy Crosley, the same creator of [`isort`](https://github
|
||||
|
||||
Hug inspired parts of APIStar, and was one of the tools I found most promising, alongside APIStar.
|
||||
|
||||
Hug helped inspiring **FastAPI** to use Python type hints to declare parameters, and to generate a schema defining the API automatically.
|
||||
Hug helped inspire **FastAPI** to use Python type hints to declare parameters, and to generate a schema defining the API automatically.
|
||||
|
||||
Hug inspired **FastAPI** to declare a `response` parameter in functions to set headers and cookies.
|
||||
|
||||
@@ -380,7 +380,7 @@ Now APIStar is a set of tools to validate OpenAPI specifications, not a web fram
|
||||
APIStar was created by Tom Christie. The same guy that created:
|
||||
|
||||
* Django REST Framework
|
||||
* Starlette (in which **FastAPI** is based)
|
||||
* Starlette (on which **FastAPI** is based)
|
||||
* Uvicorn (used by Starlette and **FastAPI**)
|
||||
|
||||
///
|
||||
@@ -393,7 +393,7 @@ The idea of declaring multiple things (data validation, serialization and docume
|
||||
|
||||
And after searching for a long time for a similar framework and testing many different alternatives, APIStar was the best option available.
|
||||
|
||||
Then APIStar stopped to exist as a server and Starlette was created, and was a new better foundation for such a system. That was the final inspiration to build **FastAPI**.
|
||||
Then APIStar stopped existing as a server and Starlette was created, and was a new better foundation for such a system. That was the final inspiration to build **FastAPI**.
|
||||
|
||||
I consider **FastAPI** a "spiritual successor" to APIStar, while improving and increasing the features, typing system, and other parts, based on the learnings from all these previous tools.
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ Asynchronous code just means that the language 💬 has a way to tell the comput
|
||||
|
||||
So, during that time, the computer can go and do some other work, while "slow-file" 📝 finishes.
|
||||
|
||||
Then the computer / program 🤖 will come back every time it has a chance because it's waiting again, or whenever it 🤖 finished all the work it had at that point. And it 🤖 will see if any of the tasks it was waiting for have already finished, doing whatever it had to do.
|
||||
Then the computer / program 🤖 will come back every time it has a chance because it's waiting again, or whenever it 🤖 finishes all the work it had at that point. And it 🤖 will see if any of the tasks it was waiting for have already finished, doing whatever it had to do.
|
||||
|
||||
Next, it 🤖 takes the first task to finish (let's say, our "slow-file" 📝) and continues whatever it had to do with it.
|
||||
|
||||
@@ -78,7 +78,7 @@ That "wait for something else" normally refers to <abbr title="Input and Output"
|
||||
|
||||
* the data from the client to be sent through the network
|
||||
* the data sent by your program to be received by the client through the network
|
||||
* the contents of a file in the disk to be read by the system and given to your program
|
||||
* the contents of a file on the disk to be read by the system and given to your program
|
||||
* the contents your program gave to the system to be written to disk
|
||||
* a remote API operation
|
||||
* a database operation to finish
|
||||
@@ -257,7 +257,7 @@ And as you can have parallelism and asynchronicity at the same time, you get hig
|
||||
|
||||
Nope! That's not the moral of the story.
|
||||
|
||||
Concurrency is different than parallelism. And it is better on **specific** scenarios that involve a lot of waiting. Because of that, it generally is a lot better than parallelism for web application development. But not for everything.
|
||||
Concurrency is different than parallelism. And it is better in **specific** scenarios that involve a lot of waiting. Because of that, it generally is a lot better than parallelism for web application development. But not for everything.
|
||||
|
||||
So, to balance that out, imagine the following short story:
|
||||
|
||||
@@ -267,7 +267,7 @@ So, to balance that out, imagine the following short story:
|
||||
|
||||
---
|
||||
|
||||
There's no waiting 🕙 anywhere, just a lot of work to be done, on multiple places of the house.
|
||||
There's no waiting 🕙 anywhere, just a lot of work to be done, in multiple places of the house.
|
||||
|
||||
You could have turns as in the burgers example, first the living room, then the kitchen, but as you are not waiting 🕙 for anything, just cleaning and cleaning, the turns wouldn't affect anything.
|
||||
|
||||
@@ -296,7 +296,7 @@ With **FastAPI** you can take advantage of concurrency that is very common for w
|
||||
|
||||
But you can also exploit the benefits of parallelism and multiprocessing (having multiple processes running in parallel) for **CPU bound** workloads like those in Machine Learning systems.
|
||||
|
||||
That, plus the simple fact that Python is the main language for **Data Science**, Machine Learning and especially Deep Learning, make FastAPI a very good match for Data Science / Machine Learning web APIs and applications (among many others).
|
||||
That, plus the simple fact that Python is the main language for **Data Science**, Machine Learning and especially Deep Learning, makes FastAPI a very good match for Data Science / Machine Learning web APIs and applications (among many others).
|
||||
|
||||
To see how to achieve this parallelism in production see the section about [Deployment](deployment/index.md).
|
||||
|
||||
@@ -340,7 +340,7 @@ burgers = get_burgers(2)
|
||||
|
||||
---
|
||||
|
||||
So, if you are using a library that tells you that you can call it with `await`, you need to create the *path operation functions* that uses it with `async def`, like in:
|
||||
So, if you are using a library that tells you that you can call it with `await`, you need to create the *path operation functions* that use it with `async def`, like in:
|
||||
|
||||
```Python hl_lines="2-3"
|
||||
@app.get('/burgers')
|
||||
@@ -435,7 +435,7 @@ Any other utility function that you call directly can be created with normal `de
|
||||
|
||||
This is in contrast to the functions that FastAPI calls for you: *path operation functions* and dependencies.
|
||||
|
||||
If your utility function is a normal function with `def`, it will be called directly (as you write it in your code), not in a threadpool, if the function is created with `async def` then you should `await` for that function when you call it in your code.
|
||||
If your utility function is a normal function with `def`, it will be called directly (as you write it in your code), not in a threadpool, if the function is created with `async def` then you should `await` that function when you call it in your code.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ FastAPI Cloud is the primary sponsor and funding provider for the *FastAPI and f
|
||||
|
||||
## Cloud Providers - Sponsors { #cloud-providers-sponsors }
|
||||
|
||||
Some other cloud providers ✨ [**sponsor FastAPI**](../help-fastapi.md#sponsor-the-author) ✨ too. 🙇
|
||||
Some other cloud providers ✨ [**sponsor FastAPI**](https://github.com/sponsors/tiangolo) ✨ too. 🙇
|
||||
|
||||
You might also want to consider them to follow their guides and try their services:
|
||||
|
||||
|
||||
@@ -36,9 +36,9 @@ And there has to be something in charge of **renewing the HTTPS certificates**,
|
||||
Some of the tools you could use as a TLS Termination Proxy are:
|
||||
|
||||
* Traefik
|
||||
* Automatically handles certificates renewals ✨
|
||||
* Automatically handles certificate renewals ✨
|
||||
* Caddy
|
||||
* Automatically handles certificates renewals ✨
|
||||
* Automatically handles certificate renewals ✨
|
||||
* Nginx
|
||||
* With an external component like Certbot for certificate renewals
|
||||
* HAProxy
|
||||
|
||||
@@ -283,7 +283,7 @@ CMD ["fastapi", "run", "app/main.py", "--proxy-headers", "--port", "80"]
|
||||
|
||||
#### Docker Cache { #docker-cache }
|
||||
|
||||
There's an important trick in this `Dockerfile`, we first copy the **file with the dependencies alone**, not the rest of the code. Let me tell you why is that.
|
||||
There's an important trick in this `Dockerfile`, we first copy the **file with the dependencies alone**, not the rest of the code. Let me tell you why that is.
|
||||
|
||||
```Dockerfile
|
||||
COPY ./requirements.txt /code/requirements.txt
|
||||
@@ -301,7 +301,7 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
||||
|
||||
The file with the package requirements **won't change frequently**. So, by copying only that file, Docker will be able to **use the cache** for that step.
|
||||
|
||||
And then, Docker will be able to **use the cache for the next step** that downloads and install those dependencies. And here's where we **save a lot of time**. ✨ ...and avoid boredom waiting. 😪😆
|
||||
And then, Docker will be able to **use the cache for the next step** that downloads and installs those dependencies. And here's where we **save a lot of time**. ✨ ...and avoid boredom waiting. 😪😆
|
||||
|
||||
Downloading and installing the package dependencies **could take minutes**, but using the **cache** would **take seconds** at most.
|
||||
|
||||
@@ -488,7 +488,7 @@ And normally this **load balancer** would be able to handle requests that go to
|
||||
|
||||
In this type of scenario, you probably would want to have **a single (Uvicorn) process per container**, as you would already be handling replication at the cluster level.
|
||||
|
||||
So, in this case, you **would not** want to have a multiple workers in the container, for example with the `--workers` command line option. You would want to have just a **single Uvicorn process** per container (but probably multiple containers).
|
||||
So, in this case, you **would not** want to have multiple workers in the container, for example with the `--workers` command line option. You would want to have just a **single Uvicorn process** per container (but probably multiple containers).
|
||||
|
||||
Having another process manager inside the container (as would be with multiple workers) would only add **unnecessary complexity** that you are most probably already taking care of with your cluster system.
|
||||
|
||||
@@ -519,7 +519,7 @@ Here are some examples of when that could make sense:
|
||||
|
||||
#### A Simple App { #a-simple-app }
|
||||
|
||||
You could want a process manager in the container if your application is **simple enough** that can run it on a **single server**, not a cluster.
|
||||
You could want a process manager in the container if your application is **simple enough** that you can run it on a **single server**, not a cluster.
|
||||
|
||||
#### Docker Compose { #docker-compose }
|
||||
|
||||
@@ -544,7 +544,7 @@ If you run **a single process per container** you will have a more or less well-
|
||||
|
||||
And then you can set those same memory limits and requirements in your configurations for your container management system (for example in **Kubernetes**). That way it will be able to **replicate the containers** in the **available machines** taking into account the amount of memory needed by them, and the amount available in the machines in the cluster.
|
||||
|
||||
If your application is **simple**, this will probably **not be a problem**, and you might not need to specify hard memory limits. But if you are **using a lot of memory** (for example with **machine learning** models), you should check how much memory you are consuming and adjust the **number of containers** that runs in **each machine** (and maybe add more machines to your cluster).
|
||||
If your application is **simple**, this will probably **not be a problem**, and you might not need to specify hard memory limits. But if you are **using a lot of memory** (for example with **machine learning** models), you should check how much memory you are consuming and adjust the **number of containers** that run on **each machine** (and maybe add more machines to your cluster).
|
||||
|
||||
If you run **multiple processes per container** you will have to make sure that the number of processes started doesn't **consume more memory** than what is available.
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ The idea is to automate the acquisition and renewal of these certificates so tha
|
||||
|
||||
## HTTPS for Developers { #https-for-developers }
|
||||
|
||||
Here's an example of how an HTTPS API could look like, step by step, paying attention mainly to the ideas important for developers.
|
||||
Here's an example of how an HTTPS API could look, step by step, paying attention mainly to the ideas important for developers.
|
||||
|
||||
### Domain Name { #domain-name }
|
||||
|
||||
@@ -192,7 +192,7 @@ All this renewal process, while still serving the app, is one of the main reason
|
||||
|
||||
## Proxy Forwarded Headers { #proxy-forwarded-headers }
|
||||
|
||||
When using a proxy to handle HTTPS, your **application server** (for example Uvicorn via FastAPI CLI) doesn't known anything about the HTTPS process, it communicates with plain HTTP with the **TLS Termination Proxy**.
|
||||
When using a proxy to handle HTTPS, your **application server** (for example Uvicorn via FastAPI CLI) doesn't know anything about the HTTPS process, it communicates with plain HTTP with the **TLS Termination Proxy**.
|
||||
|
||||
This **proxy** would normally set some HTTP headers on the fly before transmitting the request to the **application server**, to let the application server know that the request is being **forwarded** by the proxy.
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ A similar process would apply to any other ASGI server program.
|
||||
|
||||
By adding the `standard`, Uvicorn will install and use some recommended extra dependencies.
|
||||
|
||||
That including `uvloop`, the high-performance drop-in replacement for `asyncio`, that provides the big concurrency performance boost.
|
||||
That includes `uvloop`, the high-performance drop-in replacement for `asyncio`, that provides the big concurrency performance boost.
|
||||
|
||||
When you install FastAPI with something like `pip install "fastapi[standard]"` you already get `uvicorn[standard]` as well.
|
||||
|
||||
|
||||
@@ -20,4 +20,4 @@ By default, the extension will automatically discover FastAPI applications in yo
|
||||
- **Deploy to FastAPI Cloud** - One-click deployment of your app to [FastAPI Cloud](https://fastapicloud.com/).
|
||||
- **Stream Application Logs** - Real-time log streaming from your FastAPI Cloud-deployed application with level filtering and text search.
|
||||
|
||||
If you'd like to familiarize yourself with the extension's features, you can checkout the extension walkthrough by opening the Command Palette (<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd> or on macOS: <kbd>Cmd</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd>) and selecting "Welcome: Open walkthrough..." and then choosing the "Get started with FastAPI" walkthrough.
|
||||
If you'd like to familiarize yourself with the extension's features, you can check out the extension walkthrough by opening the Command Palette (<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd> or on macOS: <kbd>Cmd</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd>) and selecting "Welcome: Open walkthrough..." and then choosing the "Get started with FastAPI" walkthrough.
|
||||
|
||||
@@ -159,7 +159,7 @@ You can read more about it at [The Twelve-Factor App: Config](https://12factor.n
|
||||
|
||||
## Types and Validation { #types-and-validation }
|
||||
|
||||
These environment variables can only handle **text strings**, as they are external to Python and have to be compatible with other programs and the rest of the system (and even with different operating systems, as Linux, Windows, macOS).
|
||||
These environment variables can only handle **text strings**, as they are external to Python and have to be compatible with other programs and the rest of the system (and even with different operating systems, such as Linux, Windows, and macOS).
|
||||
|
||||
That means that **any value** read in Python from an environment variable **will be a `str`**, and any conversion to a different type or any validation has to be done in code.
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ include_yaml:
|
||||
|
||||
**FastAPI** has a great community constantly growing.
|
||||
|
||||
There are many posts, articles, tools, and projects, related to **FastAPI**.
|
||||
There are many posts, articles, tools, and projects related to **FastAPI**.
|
||||
|
||||
You could easily use a search engine or video platform to find many resources related to FastAPI.
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ This is me:
|
||||
|
||||
</div>
|
||||
|
||||
I'm the creator of **FastAPI**. You can read more about that in [Help FastAPI - Get Help - Connect with the author](help-fastapi.md#connect-with-the-author).
|
||||
I'm the creator of **FastAPI**. You can read more about that in [Help FastAPI - Follow the author](help-fastapi.md#follow-the-author).
|
||||
|
||||
...But here I want to show you the community.
|
||||
|
||||
@@ -42,9 +42,8 @@ I'm the creator of **FastAPI**. You can read more about that in [Help FastAPI -
|
||||
These are the people that:
|
||||
|
||||
* [Help others with questions in GitHub](help-fastapi.md#help-others-with-questions-in-github).
|
||||
* [Create Pull Requests](help-fastapi.md#create-a-pull-request).
|
||||
* Review Pull Requests, [especially important for translations](contributing.md#translations).
|
||||
* Help [manage the repository](management-tasks.md) (team members).
|
||||
* Create or review Pull Requests.
|
||||
* Help [manage the repository](https://tiangolo.com/open-source/management-tasks/) (team members).
|
||||
|
||||
All these tasks help maintain the repository.
|
||||
|
||||
@@ -54,7 +53,7 @@ A round of applause to them. 👏 🙇
|
||||
|
||||
This is the current list of team members. 😎
|
||||
|
||||
They have different levels of involvement and permissions, they can perform [repository management tasks](./management-tasks.md) and together we [manage the FastAPI repository](./management.md).
|
||||
They have different levels of involvement and permissions, they can perform [repository management tasks](https://tiangolo.com/open-source/management-tasks/) and together we [manage the FastAPI repository](./management.md).
|
||||
|
||||
<div class="user-list user-list-center">
|
||||
|
||||
@@ -66,7 +65,7 @@ They have different levels of involvement and permissions, they can perform [rep
|
||||
|
||||
</div>
|
||||
|
||||
Although the team members have the permissions to perform privileged tasks, all the [help from others maintaining FastAPI](./help-fastapi.md#help-maintain-fastapi) is very much appreciated! 🙇♂️
|
||||
Although the team members have the permissions to perform privileged tasks, all the help from others maintaining FastAPI is very much appreciated! 🙇♂️
|
||||
|
||||
## FastAPI Experts
|
||||
|
||||
@@ -186,7 +185,7 @@ These are the users that have [helped others the most with questions in GitHub](
|
||||
|
||||
Here are the **Top Contributors**. 👷
|
||||
|
||||
These users have [created the most Pull Requests](help-fastapi.md#create-a-pull-request) that have been *merged*.
|
||||
These users have created the most Pull Requests that have been *merged*.
|
||||
|
||||
They have contributed source code, documentation, etc. 📦
|
||||
|
||||
@@ -210,7 +209,7 @@ There are hundreds of other contributors, you can see them all in the [FastAPI G
|
||||
|
||||
These users are the **Top Translation Reviewers**. 🕵️
|
||||
|
||||
Translation reviewers have the [**power to approve translations**](contributing.md#translations) of the documentation. Without them, there wouldn't be documentation in several other languages.
|
||||
Translation reviewers have the **power to approve translations** of the documentation. Without them, there wouldn't be documentation in several other languages.
|
||||
|
||||
<div class="user-list user-list-center">
|
||||
{% for user in (translation_reviewers.values() | list)[:50] %}
|
||||
|
||||
@@ -73,11 +73,11 @@ Pass the keys and values of the `second_user_data` dict directly as key-value ar
|
||||
|
||||
### Editor support { #editor-support }
|
||||
|
||||
All the framework was designed to be easy and intuitive to use, all the decisions were tested on multiple editors even before starting development, to ensure the best development experience.
|
||||
The whole framework was designed to be easy and intuitive to use, all the decisions were tested on multiple editors even before starting development, to ensure the best development experience.
|
||||
|
||||
In the Python developer surveys, it's clear [that one of the most used features is "autocompletion"](https://www.jetbrains.com/research/python-developers-survey-2017/#tools-and-features).
|
||||
|
||||
The whole **FastAPI** framework is based to satisfy that. Autocompletion works everywhere.
|
||||
The whole **FastAPI** framework is designed to satisfy that. Autocompletion works everywhere.
|
||||
|
||||
You will rarely need to come back to the docs.
|
||||
|
||||
@@ -147,7 +147,7 @@ FastAPI includes an extremely easy to use, but extremely powerful <dfn title='al
|
||||
|
||||
### Unlimited "plug-ins" { #unlimited-plug-ins }
|
||||
|
||||
Or in other way, no need for them, import and use the code you need.
|
||||
Or, in other words, no need for them, import and use the code you need.
|
||||
|
||||
Any integration is designed to be so simple to use (with dependencies) that you can create a "plug-in" for your application in 2 lines of code using the same structure and syntax used for your *path operations*.
|
||||
|
||||
@@ -179,7 +179,7 @@ With **FastAPI** you get all of **Starlette**'s features (as FastAPI is just Sta
|
||||
|
||||
**FastAPI** is fully compatible with (and based on) [**Pydantic**](https://docs.pydantic.dev/). So, any additional Pydantic code you have, will also work.
|
||||
|
||||
Including external libraries also based on Pydantic, as <abbr title="Object-Relational Mapper">ORM</abbr>s, <abbr title="Object-Document Mapper">ODM</abbr>s for databases.
|
||||
Including external libraries also based on Pydantic, such as <abbr title="Object-Relational Mapper">ORM</abbr>s and <abbr title="Object-Document Mapper">ODM</abbr>s for databases.
|
||||
|
||||
This also means that in many cases you can pass the same object you get from a request **directly to the database**, as everything is validated automatically.
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ You can follow **FastAPI** online in several places:
|
||||
|
||||
You can "star" FastAPI in GitHub (clicking the star button at the top right): [https://github.com/fastapi/fastapi](https://github.com/fastapi/fastapi). ⭐️
|
||||
|
||||
By adding a star, other users will be able to find it more easily and see that it has been already useful for others.
|
||||
By adding a star, other users will be able to find it more easily and see that it has already been useful for others.
|
||||
|
||||
## Watch the GitHub repository for releases { #watch-the-github-repository-for-releases }
|
||||
|
||||
|
||||
@@ -67,4 +67,4 @@ presets: [
|
||||
|
||||
These are **JavaScript** objects, not strings, so you can't pass them from Python code directly.
|
||||
|
||||
If you need to use JavaScript-only configurations like those, you can use one of the methods above. Override all the Swagger UI *path operation* and manually write any JavaScript you need.
|
||||
If you need to use JavaScript-only configurations like those, you can use one of the methods above. Override the whole Swagger UI *path operation* and manually write any JavaScript you need.
|
||||
|
||||
@@ -94,7 +94,7 @@ All we need to do is handle the request inside a `try`/`except` block:
|
||||
|
||||
{* ../../docs_src/custom_request_and_route/tutorial002_an_py310.py hl[14,16] *}
|
||||
|
||||
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:
|
||||
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:
|
||||
|
||||
{* ../../docs_src/custom_request_and_route/tutorial002_an_py310.py hl[17:19] *}
|
||||
|
||||
|
||||
@@ -57,4 +57,4 @@ If you need GraphQL, I still would recommend you check out [Strawberry](https://
|
||||
|
||||
You can learn more about **GraphQL** in the [official GraphQL documentation](https://graphql.org/).
|
||||
|
||||
You can also read more about each those libraries described above in their links.
|
||||
You can also read more about each of those libraries described above in their links.
|
||||
|
||||
@@ -8,6 +8,8 @@ FastAPI version 0.119.0 introduced partial support for Pydantic v1 from inside o
|
||||
|
||||
FastAPI 0.126.0 dropped support for Pydantic v1, while still supporting `pydantic.v1` for a little while.
|
||||
|
||||
FastAPI 0.128.0 dropped support for `pydantic.v1` as well, so the latest versions of FastAPI require Pydantic v2.
|
||||
|
||||
/// warning
|
||||
|
||||
The Pydantic team stopped support for Pydantic v1 for the latest versions of Python, starting with **Python 3.14**.
|
||||
@@ -54,6 +56,16 @@ This means that you can install the latest version of Pydantic v2 and import and
|
||||
|
||||
### FastAPI support for Pydantic v1 in v2 { #fastapi-support-for-pydantic-v1-in-v2 }
|
||||
|
||||
/// warning
|
||||
|
||||
This FastAPI support for `pydantic.v1` models was added in **FastAPI 0.119.0** and removed in **FastAPI 0.128.0**. It was meant to be a temporary aid for the migration to Pydantic v2.
|
||||
|
||||
In current versions of FastAPI, using a `pydantic.v1` model in your app will raise an error.
|
||||
|
||||
The rest of this section describes the temporary support available only in those older versions.
|
||||
|
||||
///
|
||||
|
||||
Since FastAPI 0.119.0, there's also partial support for Pydantic v1 from inside of Pydantic v2, to facilitate the migration to v2.
|
||||
|
||||
So, you could upgrade Pydantic to the latest version 2, and change the imports to use the `pydantic.v1` submodule, and in many cases it would just work.
|
||||
@@ -88,7 +100,7 @@ graph TB
|
||||
style V2Field fill:#f9fff3
|
||||
```
|
||||
|
||||
...but, you can have separated models using Pydantic v1 and v2 in the same app.
|
||||
...but you can have separate models, some using Pydantic v1 and others using Pydantic v2, in the same app.
|
||||
|
||||
```mermaid
|
||||
graph TB
|
||||
@@ -122,6 +134,12 @@ If you need to use some of the FastAPI-specific tools for parameters like `Body`
|
||||
|
||||
### Migrate in steps { #migrate-in-steps }
|
||||
|
||||
/// warning
|
||||
|
||||
The gradual migration using both Pydantic v1 and v2 models in the same app described below only works in **FastAPI 0.119.0 to 0.127.x**. It was removed in **FastAPI 0.128.0**, the latest versions require **Pydantic v2** models.
|
||||
|
||||
///
|
||||
|
||||
/// tip
|
||||
|
||||
First try with `bump-pydantic`, if your tests pass and that works, then you're done in one command. ✨
|
||||
@@ -130,6 +148,6 @@ First try with `bump-pydantic`, if your tests pass and that works, then you're d
|
||||
|
||||
If `bump-pydantic` doesn't work for your use case, you can use the support for both Pydantic v1 and v2 models in the same app to do the migration to Pydantic v2 gradually.
|
||||
|
||||
You could fist upgrade Pydantic to use the latest version 2, and change the imports to use `pydantic.v1` for all your models.
|
||||
You could first upgrade Pydantic to use the latest version 2, and change the imports to use `pydantic.v1` for all your models.
|
||||
|
||||
Then, you can start migrating your models from Pydantic v1 to v2 in groups, in gradual steps. 🚶
|
||||
|
||||
@@ -46,7 +46,7 @@ If you interact with the docs and check the response, even though the code didn'
|
||||
|
||||
This means that it will **always have a value**, it's just that sometimes the value could be `None` (or `null` in JSON).
|
||||
|
||||
That means that, clients using your API don't have to check if the value exists or not, they can **assume the field will always be there**, but just that in some cases it will have the default value of `None`.
|
||||
That means that clients using your API don't have to check if the value exists or not, they can **assume the field will always be there**, but just that in some cases it will have the default value of `None`.
|
||||
|
||||
The way to describe this in OpenAPI, is to mark that field as **required**, because it will always be there.
|
||||
|
||||
|
||||
@@ -477,13 +477,13 @@ For a more complete example including more features, see the <a href="https://fa
|
||||
|
||||
**Spoiler alert**: the tutorial - user guide includes:
|
||||
|
||||
* Declaration of **parameters** from other different places as: **headers**, **cookies**, **form fields** and **files**.
|
||||
* How to set **validation constraints** as `maximum_length` or `regex`.
|
||||
* Declaration of **parameters** from other different places such as: **headers**, **cookies**, **form fields** and **files**.
|
||||
* How to set **validation constraints** such as `maximum_length` or `regex`.
|
||||
* A very powerful and easy to use **<dfn title="also known as components, resources, providers, services, injectables">Dependency Injection</dfn>** system.
|
||||
* Security and authentication, including support for **OAuth2** with **JWT tokens** and **HTTP Basic** auth.
|
||||
* More advanced (but equally easy) techniques for declaring **deeply nested JSON models** (thanks to Pydantic).
|
||||
* **GraphQL** integration with [Strawberry](https://strawberry.rocks) and other libraries.
|
||||
* Many extra features (thanks to Starlette) as:
|
||||
* Many extra features (thanks to Starlette) such as:
|
||||
* **WebSockets**
|
||||
* extremely easy tests based on HTTPX and `pytest`
|
||||
* **CORS**
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# Full Stack FastAPI Template { #full-stack-fastapi-template }
|
||||
|
||||
Templates, while typically come with a specific setup, are designed to be flexible and customizable. This allows you to modify and adapt them to your project's requirements, making them an excellent starting point. 🏁
|
||||
Templates, while they typically come with a specific setup, are designed to be flexible and customizable. This allows you to modify and adapt them to your project's requirements, making them an excellent starting point. 🏁
|
||||
|
||||
You can use this template to get started, as it includes a lot of the initial set up, security, database and some API endpoints already done for you.
|
||||
You can use this template to get started, as it includes a lot of the initial setup, security, database and some API endpoints already done for you.
|
||||
|
||||
GitHub Repository: [Full Stack FastAPI Template](https://github.com/tiangolo/full-stack-fastapi-template)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
Python has support for optional "type hints" (also called "type annotations").
|
||||
|
||||
These **"type hints"** or annotations are a special syntax that allow declaring the <dfn title="for example: str, int, float, bool">type</dfn> of a variable.
|
||||
These **"type hints"** or annotations are a special syntax that allows declaring the <dfn title="for example: str, int, float, bool">type</dfn> of a variable.
|
||||
|
||||
By declaring types for your variables, editors and tools can give you better support.
|
||||
|
||||
@@ -44,7 +44,7 @@ It's a very simple program.
|
||||
|
||||
But now imagine that you were writing it from scratch.
|
||||
|
||||
At some point you would have started the definition of the function, you had the parameters ready...
|
||||
At some point you start defining the function, and you have the parameters ready...
|
||||
|
||||
But then you have to call "that method that converts the first letter to upper case".
|
||||
|
||||
@@ -80,7 +80,7 @@ Those are the "type hints":
|
||||
|
||||
{* ../../docs_src/python_types/tutorial002_py310.py hl[1] *}
|
||||
|
||||
That is not the same as declaring default values like would be with:
|
||||
That is not the same as declaring default values like it would be with:
|
||||
|
||||
```Python
|
||||
first_name="john", last_name="doe"
|
||||
|
||||
@@ -16,7 +16,7 @@ For example:
|
||||
* 403: `status.HTTP_403_FORBIDDEN`
|
||||
* etc.
|
||||
|
||||
It can be convenient to quickly access HTTP (and WebSocket) status codes in your app, using autocompletion for the name without having to remember the integer status codes by memory.
|
||||
It can be convenient to quickly access HTTP (and WebSocket) status codes in your app, using autocompletion for the name without having to memorize the integer status codes.
|
||||
|
||||
Read more about it in the [FastAPI docs about Response Status Code](https://fastapi.tiangolo.com/tutorial/response-status-code/).
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ When you want to define dependencies that should be compatible with both HTTP an
|
||||
|
||||
Additional classes for handling WebSockets.
|
||||
|
||||
Provided directly by Starlette, but you can import it from `fastapi`:
|
||||
Provided directly by Starlette, but you can import them from `fastapi`:
|
||||
|
||||
```python
|
||||
from fastapi.websockets import WebSocketDisconnect, WebSocketState
|
||||
@@ -60,7 +60,7 @@ from fastapi.websockets import WebSocketDisconnect, WebSocketState
|
||||
|
||||
When a client disconnects, a `WebSocketDisconnect` exception is raised, you can catch it.
|
||||
|
||||
You can import it directly form `fastapi`:
|
||||
You can import it directly from `fastapi`:
|
||||
|
||||
```python
|
||||
from fastapi import WebSocketDisconnect
|
||||
|
||||
@@ -7,6 +7,19 @@ hide:
|
||||
|
||||
## Latest Changes
|
||||
|
||||
### Docs
|
||||
|
||||
* ✏️ Fix grammar, typos, and broken links in docs. PR [#15694](https://github.com/fastapi/fastapi/pull/15694) by [@YuriiMotov](https://github.com/YuriiMotov).
|
||||
|
||||
### Translations
|
||||
|
||||
* 🌐 Enable Hindi docs translations. PR [#15554](https://github.com/fastapi/fastapi/pull/15554) by [@YuriiMotov](https://github.com/YuriiMotov).
|
||||
|
||||
### Internal
|
||||
|
||||
* 📝 Tweak wording about deploying to FastAPI Cloud. PR [#15793](https://github.com/fastapi/fastapi/pull/15793) by [@tiangolo](https://github.com/tiangolo).
|
||||
* 🔨 Use `gpt-5.5` model in `translate.py`, specify `-chat` to avoid warnings. PR [#15792](https://github.com/fastapi/fastapi/pull/15792) by [@YuriiMotov](https://github.com/YuriiMotov).
|
||||
|
||||
## 0.137.2 (2026-06-18)
|
||||
|
||||
### Features
|
||||
@@ -63,7 +76,7 @@ Before this, `router.include_router(other_router)` would take each path operatio
|
||||
|
||||
This would mean that in the end there was only one top level router, part of the app.
|
||||
|
||||
The way it is structured here is that there are a few additional classes to handle intermediate metadata for router and route inclusion. That way the information of "router X includes Y and Y includes Z" is stored somewhere, without affecting (recreating / clonning) the final route.
|
||||
The way it is structured here is that there are a few additional classes to handle intermediate metadata for router and route inclusion. That way the information of "router X includes Y and Y includes Z" is stored somewhere, without affecting (recreating / cloning) the final route.
|
||||
|
||||
#### Non Objectives
|
||||
|
||||
@@ -82,7 +95,7 @@ Additionally, any logic that iterated on `router.routes` to modify them would no
|
||||
#### Features
|
||||
|
||||
* Adding routes (path operations) after a router is included now works, they are reflected as they are not copied.
|
||||
* Including `subrouter` in `mainrouter` can be done before adding routes (path operations) to `subrouter`, because now the the entire object is stored instead of copying the routes.
|
||||
* Including `subrouter` in `mainrouter` can be done before adding routes (path operations) to `subrouter`, because now the entire object is stored instead of copying the routes.
|
||||
* As routes are not copied, in some cases that might save some memory.
|
||||
|
||||
#### Alpha Features
|
||||
@@ -97,7 +110,7 @@ Still, for now, consider this very experimental and potentially changing and bre
|
||||
|
||||
#### Future Features Enabled
|
||||
|
||||
* Custom `APIRoute` subclasses (undocumented, but alraedy works as desccribed above)
|
||||
* Custom `APIRoute` subclasses (undocumented, but already works as described above)
|
||||
* Custom `APIRouter` subclasses (undocumented, but already works as described above)
|
||||
* Dependencies per router
|
||||
* Exception handlers per router
|
||||
@@ -404,7 +417,7 @@ Still, for now, consider this very experimental and potentially changing and bre
|
||||
### Features
|
||||
|
||||
* ✨ Add support for streaming JSON Lines and binary data with `yield`. PR [#15022](https://github.com/fastapi/fastapi/pull/15022) by [@tiangolo](https://github.com/tiangolo).
|
||||
* This also upgrades Starlette from `>=0.40.0` to `>=0.46.0`, as it's needed to properly unrwap and re-raise exceptions from exception groups.
|
||||
* This also upgrades Starlette from `>=0.40.0` to `>=0.46.0`, as it's needed to properly unwrap and re-raise exceptions from exception groups.
|
||||
* New docs: [Stream JSON Lines](https://fastapi.tiangolo.com/tutorial/stream-json-lines/).
|
||||
* And new docs: [Stream Data](https://fastapi.tiangolo.com/advanced/stream-data/).
|
||||
|
||||
@@ -1427,7 +1440,7 @@ You can read more about it in the docs for [Advanced Dependencies - Dependencies
|
||||
* 🌐 Add Persian translation for `docs/fa/docs/python-types.md`. PR [#13524](https://github.com/fastapi/fastapi/pull/13524) by [@Mohammad222PR](https://github.com/Mohammad222PR).
|
||||
* 🌐 Update Portuguese Translation for `docs/pt/docs/project-generation.md`. PR [#13875](https://github.com/fastapi/fastapi/pull/13875) by [@EdmilsonRodrigues](https://github.com/EdmilsonRodrigues).
|
||||
* 🌐 Add Persian translation for `docs/fa/docs/async.md`. PR [#13541](https://github.com/fastapi/fastapi/pull/13541) by [@Mohammad222PR](https://github.com/Mohammad222PR).
|
||||
* 🌐 Add Bangali translation for `docs/bn/about/index.md`. PR [#13882](https://github.com/fastapi/fastapi/pull/13882) by [@sajjadrahman56](https://github.com/sajjadrahman56).
|
||||
* 🌐 Add Bengali translation for `docs/bn/about/index.md`. PR [#13882](https://github.com/fastapi/fastapi/pull/13882) by [@sajjadrahman56](https://github.com/sajjadrahman56).
|
||||
|
||||
### Internal
|
||||
|
||||
@@ -2269,7 +2282,7 @@ If you want to install `fastapi` with the standard dependencies but without `fas
|
||||
* ➕ Add docs dependency: markdown-include-variants. PR [#12399](https://github.com/fastapi/fastapi/pull/12399) by [@tiangolo](https://github.com/tiangolo).
|
||||
* 📝 Fix extra mdx-base-path paths. PR [#12397](https://github.com/fastapi/fastapi/pull/12397) by [@tiangolo](https://github.com/tiangolo).
|
||||
* 👷 Tweak labeler to not override custom labels. PR [#12398](https://github.com/fastapi/fastapi/pull/12398) by [@tiangolo](https://github.com/tiangolo).
|
||||
* 👷 Update worfkow deploy-docs-notify URL. PR [#12392](https://github.com/fastapi/fastapi/pull/12392) by [@tiangolo](https://github.com/tiangolo).
|
||||
* 👷 Update workflow deploy-docs-notify URL. PR [#12392](https://github.com/fastapi/fastapi/pull/12392) by [@tiangolo](https://github.com/tiangolo).
|
||||
* 👷 Update Cloudflare GitHub Action. PR [#12387](https://github.com/fastapi/fastapi/pull/12387) by [@tiangolo](https://github.com/tiangolo).
|
||||
* ⬆ Bump pypa/gh-action-pypi-publish from 1.10.1 to 1.10.3. PR [#12386](https://github.com/fastapi/fastapi/pull/12386) by [@dependabot[bot]](https://github.com/apps/dependabot).
|
||||
* ⬆ Bump mkdocstrings[python] from 0.25.1 to 0.26.1. PR [#12371](https://github.com/fastapi/fastapi/pull/12371) by [@dependabot[bot]](https://github.com/apps/dependabot).
|
||||
@@ -2785,7 +2798,7 @@ Discussed here: [#11522](https://github.com/fastapi/fastapi/pull/11522) and here
|
||||
* 📝 Update `security/first-steps.md`. PR [#11673](https://github.com/tiangolo/fastapi/pull/11673) by [@alejsdev](https://github.com/alejsdev).
|
||||
* 📝 Update note in `path-params-numeric-validations.md`. PR [#11672](https://github.com/tiangolo/fastapi/pull/11672) by [@alejsdev](https://github.com/alejsdev).
|
||||
* 📝 Tweak intro docs about `Annotated` and `Query()` params. PR [#11664](https://github.com/tiangolo/fastapi/pull/11664) by [@tiangolo](https://github.com/tiangolo).
|
||||
* 📝 Update JWT auth documentation to use PyJWT instead of pyhon-jose. PR [#11589](https://github.com/tiangolo/fastapi/pull/11589) by [@estebanx64](https://github.com/estebanx64).
|
||||
* 📝 Update JWT auth documentation to use PyJWT instead of python-jose. PR [#11589](https://github.com/tiangolo/fastapi/pull/11589) by [@estebanx64](https://github.com/estebanx64).
|
||||
* 📝 Update docs. PR [#11603](https://github.com/tiangolo/fastapi/pull/11603) by [@alejsdev](https://github.com/alejsdev).
|
||||
* ✏️ Fix typo: convert every 're-use' to 'reuse'.. PR [#11598](https://github.com/tiangolo/fastapi/pull/11598) by [@hasansezertasan](https://github.com/hasansezertasan).
|
||||
* ✏️ Fix typo in `fastapi/applications.py`. PR [#11593](https://github.com/tiangolo/fastapi/pull/11593) by [@petarmaric](https://github.com/petarmaric).
|
||||
@@ -3224,7 +3237,7 @@ def my_dep():
|
||||
|
||||
### Security fixes
|
||||
|
||||
* ⬆️ Upgrade minimum version of `python-multipart` to `>=0.0.7` to fix a vulnerability when using form data with a ReDos attack. You can also simply upgrade `python-multipart`.
|
||||
* ⬆️ Upgrade minimum version of `python-multipart` to `>=0.0.7` to fix a vulnerability when using form data with a ReDoS attack. You can also simply upgrade `python-multipart`.
|
||||
|
||||
Read more in the [advisory: Content-Type Header ReDoS](https://github.com/tiangolo/fastapi/security/advisories/GHSA-qf9m-vfgh-m389).
|
||||
|
||||
@@ -4732,7 +4745,7 @@ You hopefully updated to a supported version of Python a while ago. If you haven
|
||||
### Fixes
|
||||
|
||||
* 🐛 Fix `RuntimeError` raised when `HTTPException` has a status code with no content. PR [#5365](https://github.com/tiangolo/fastapi/pull/5365) by [@iudeen](https://github.com/iudeen).
|
||||
* 🐛 Fix empty response body when default `status_code` is empty but the a `Response` parameter with `response.status_code` is set. PR [#5360](https://github.com/tiangolo/fastapi/pull/5360) by [@tmeckel](https://github.com/tmeckel).
|
||||
* 🐛 Fix empty response body when default `status_code` is empty but the `Response` parameter with `response.status_code` is set. PR [#5360](https://github.com/tiangolo/fastapi/pull/5360) by [@tmeckel](https://github.com/tmeckel).
|
||||
|
||||
### Docs
|
||||
|
||||
@@ -5094,7 +5107,7 @@ def main(
|
||||
### Internal
|
||||
|
||||
* ♻ Refactor dict value extraction to minimize key lookups `fastapi/utils.py`. PR [#3139](https://github.com/tiangolo/fastapi/pull/3139) by [@ShahriyarR](https://github.com/ShahriyarR).
|
||||
* ✅ Add tests for required nonable parameters and body fields. PR [#4907](https://github.com/tiangolo/fastapi/pull/4907) by [@tiangolo](https://github.com/tiangolo).
|
||||
* ✅ Add tests for required `None`-able parameters and body fields. PR [#4907](https://github.com/tiangolo/fastapi/pull/4907) by [@tiangolo](https://github.com/tiangolo).
|
||||
* 👷 Fix installing Material for MkDocs Insiders in CI. PR [#4897](https://github.com/tiangolo/fastapi/pull/4897) by [@tiangolo](https://github.com/tiangolo).
|
||||
* 👷 Add pre-commit CI instead of custom GitHub Action. PR [#4896](https://github.com/tiangolo/fastapi/pull/4896) by [@tiangolo](https://github.com/tiangolo).
|
||||
* 👷 Add pre-commit GitHub Action workflow. PR [#4895](https://github.com/tiangolo/fastapi/pull/4895) by [@tiangolo](https://github.com/tiangolo).
|
||||
@@ -5487,7 +5500,7 @@ Soon there will be a new FastAPI release upgrading Starlette to take advantage o
|
||||
|
||||
### Internal
|
||||
|
||||
* ✨ Update GitHub Action: notify-translations, to avoid a race conditions. PR [#3989](https://github.com/tiangolo/fastapi/pull/3989) by [@tiangolo](https://github.com/tiangolo).
|
||||
* ✨ Update GitHub Action: notify-translations, to avoid race conditions. PR [#3989](https://github.com/tiangolo/fastapi/pull/3989) by [@tiangolo](https://github.com/tiangolo).
|
||||
* ⬆️ Upgrade development `autoflake`, supporting multi-line imports. PR [#3988](https://github.com/tiangolo/fastapi/pull/3988) by [@tiangolo](https://github.com/tiangolo).
|
||||
* ⬆️ Increase dependency ranges for tests and docs: pytest-cov, pytest-asyncio, black, httpx, sqlalchemy, databases, mkdocs-markdownextradata-plugin. PR [#3987](https://github.com/tiangolo/fastapi/pull/3987) by [@tiangolo](https://github.com/tiangolo).
|
||||
* 👥 Update FastAPI People. PR [#3986](https://github.com/tiangolo/fastapi/pull/3986) by [@github-actions[bot]](https://github.com/apps/github-actions).
|
||||
@@ -5875,7 +5888,7 @@ router = APIRouter(prefix="/users", dependencies=[Depends(some_dependency)])
|
||||
|
||||
Most of these settings are now supported in `APIRouter`, which normally lives closer to the related code, so it is recommended to use `APIRouter` when possible.
|
||||
|
||||
But `include_router` is still useful to, for example, adding options (like `dependencies`, `prefix`, and `tags`) when including a third party router, or a generic router that is shared between several projects.
|
||||
But `include_router` is still useful to, for example, add options (like `dependencies`, `prefix`, and `tags`) when including a third party router, or a generic router that is shared between several projects.
|
||||
|
||||
This PR allows setting the (mostly new) parameters (additionally to the already existing parameters):
|
||||
|
||||
@@ -5937,7 +5950,7 @@ Note: all the previous parameters are still there, so it's still possible to dec
|
||||
|
||||
* ✏️ Fix typo in Tutorial - Path Parameters. PR [#2231](https://github.com/tiangolo/fastapi/pull/2231) by [@mariacamilagl](https://github.com/mariacamilagl).
|
||||
* ✏ Fix a stylistic error in docs. PR [#2206](https://github.com/tiangolo/fastapi/pull/2206) by [@ddobrinskiy](https://github.com/ddobrinskiy).
|
||||
* ✏ Fix capitalizaiton typo in docs. PR [#2204](https://github.com/tiangolo/fastapi/pull/2204) by [@imba-tjd](https://github.com/imba-tjd).
|
||||
* ✏ Fix capitalization typo in docs. PR [#2204](https://github.com/tiangolo/fastapi/pull/2204) by [@imba-tjd](https://github.com/imba-tjd).
|
||||
* ✏ Fix typo in docs. PR [#2179](https://github.com/tiangolo/fastapi/pull/2179) by [@ammarasmro](https://github.com/ammarasmro).
|
||||
* 📝 Update/fix links in docs to use HTTPS. PR [#2165](https://github.com/tiangolo/fastapi/pull/2165) by [@imba-tjd](https://github.com/imba-tjd).
|
||||
* ✏ Fix typos and add rewording in docs. PR [#2159](https://github.com/tiangolo/fastapi/pull/2159) by [@nukopy](https://github.com/nukopy).
|
||||
@@ -6065,7 +6078,7 @@ Note: all the previous parameters are still there, so it's still possible to dec
|
||||
* Fix typo in docs for query parameters. PR [#1832](https://github.com/tiangolo/fastapi/pull/1832) by [@ycd](https://github.com/ycd).
|
||||
* Add docs about [Async Tests](https://fastapi.tiangolo.com/advanced/async-tests/). PR [#1619](https://github.com/tiangolo/fastapi/pull/1619) by [@empicano](https://github.com/empicano).
|
||||
* Raise an exception when using form data (`Form`, `File`) without having `python-multipart` installed.
|
||||
* Up to now the application would run, and raise an exception only when receiving a request with form data, the new behavior, raising early, will prevent from deploying applications with broken dependencies.
|
||||
* Up to now the application would run, and raise an exception only when receiving a request with form data, the new behavior, raising early, will prevent deploying applications with broken dependencies.
|
||||
* It also detects if the correct package `python-multipart` is installed instead of the incorrect `multipart` (both importable as `multipart`).
|
||||
* PR [#1851](https://github.com/tiangolo/fastapi/pull/1851) based on original PR [#1627](https://github.com/tiangolo/fastapi/pull/1627) by [@chrisngyn](https://github.com/chrisngyn), [@YKo20010](https://github.com/YKo20010), [@kx-chen](https://github.com/kx-chen).
|
||||
* Re-enable Gitter releases bot. PR [#1831](https://github.com/tiangolo/fastapi/pull/1831).
|
||||
@@ -6215,7 +6228,7 @@ Note: all the previous parameters are still there, so it's still possible to dec
|
||||
* [Using FastAPI with Django](https://www.stavros.io/posts/fastapi-with-django/) by [Stavros Korokithakis](https://x.com/Stavros).
|
||||
* [Introducing Dispatch](https://netflixtechblog.com/introducing-dispatch-da4b8a2a8072) by [Netflix](https://netflixtechblog.com/).
|
||||
* **Podcasts**:
|
||||
* [Build The Next Generation Of Python Web Applications With FastAPI - Episode 259 - interview to Sebastían Ramírez (tiangolo)](https://www.pythonpodcast.com/fastapi-web-application-framework-episode-259/) by [Podcast.`__init__`](https://www.pythonpodcast.com/).
|
||||
* [Build The Next Generation Of Python Web Applications With FastAPI - Episode 259 - interview to Sebastián Ramírez (tiangolo)](https://www.pythonpodcast.com/fastapi-web-application-framework-episode-259/) by [Podcast.`__init__`](https://www.pythonpodcast.com/).
|
||||
* **Talks**:
|
||||
* [PyConBY 2020: Serve ML models easily with FastAPI](https://www.youtube.com/watch?v=z9K5pwb0rt8) by [Sebastián Ramírez (tiangolo)](https://x.com/tiangolo).
|
||||
* [[VIRTUAL] Py.Amsterdam's flying Software Circus: Intro to FastAPI](https://www.youtube.com/watch?v=PnpTY1f4k2U) by [Sebastián Ramírez (tiangolo)](https://x.com/tiangolo).
|
||||
@@ -6230,7 +6243,7 @@ Note: all the previous parameters are still there, so it's still possible to dec
|
||||
|
||||
* Allow enums to allow them to have their own schemas in OpenAPI. To support [pydantic/pydantic#1432](https://github.com/pydantic/pydantic/pull/1432) in FastAPI. PR [#1461](https://github.com/tiangolo/fastapi/pull/1461).
|
||||
* Add links for funding through [GitHub sponsors](https://github.com/sponsors/tiangolo). PR [#1425](https://github.com/tiangolo/fastapi/pull/1425).
|
||||
* Update issue template for for questions. PR [#1344](https://github.com/tiangolo/fastapi/pull/1344) by [@retnikt](https://github.com/retnikt).
|
||||
* Update issue template for questions. PR [#1344](https://github.com/tiangolo/fastapi/pull/1344) by [@retnikt](https://github.com/retnikt).
|
||||
* Update warning about storing passwords in docs. PR [#1336](https://github.com/tiangolo/fastapi/pull/1336) by [@skorokithakis](https://github.com/skorokithakis).
|
||||
* Fix typo. PR [#1326](https://github.com/tiangolo/fastapi/pull/1326) by [@chenl](https://github.com/chenl).
|
||||
* Add translation to Portuguese for [Alternatives, Inspiration and Comparisons - Alternativas, Inspiração e Comparações](https://fastapi.tiangolo.com/pt/alternatives/). PR [#1325](https://github.com/tiangolo/fastapi/pull/1325) by [@Serrones](https://github.com/Serrones).
|
||||
@@ -6621,7 +6634,7 @@ Note: all the previous parameters are still there, so it's still possible to dec
|
||||
* When declaring a `response_model` it is used directly to generate the response content, from whatever was returned from the *path operation function*.
|
||||
* Before this, the return content was first passed through `jsonable_encoder` to ensure it was a "jsonable" object, like a `dict`, instead of an arbitrary object with attributes (like an ORM model). That's why you should make sure to update your Pydantic models for objects with attributes to use `orm_mode = True`.
|
||||
* If you don't have a `response_model`, the return object will still be passed through `jsonable_encoder` first.
|
||||
* When a `response_model` is declared, the same `response_model` type declaration won't be used as is, it will be "cloned" to create an new one (a cloned Pydantic `Field` with all the submodels cloned as well).
|
||||
* When a `response_model` is declared, the same `response_model` type declaration won't be used as is, it will be "cloned" to create a new one (a cloned Pydantic `Field` with all the submodels cloned as well).
|
||||
* This avoids/fixes a potential security issue: as the returned object is passed directly to Pydantic, if the returned object was a subclass of the `response_model` (e.g. you return a `UserInDB` that inherits from `User` but contains extra fields, like `hashed_password`, and `User` is used in the `response_model`), it would still pass the validation (because `UserInDB` is a subclass of `User`) and the object would be returned as-is, including the `hashed_password`. To fix this, the declared `response_model` is cloned, if it is a Pydantic model class (or contains Pydantic model classes in it, e.g. in a `List[Item]`), the Pydantic model class(es) will be a different one (the "cloned" one). So, an object that is a subclass won't simply pass the validation and returned as-is, because it is no longer a sub-class of the cloned `response_model`. Instead, a new Pydantic model object will be created with the contents of the returned object. So, it will be a new object (made with the data from the returned one), and will be filtered by the cloned `response_model`, containing only the declared fields as normally.
|
||||
* PR [#322](https://github.com/tiangolo/fastapi/pull/322).
|
||||
|
||||
@@ -6764,7 +6777,7 @@ Note: all the previous parameters are still there, so it's still possible to dec
|
||||
* Add support for `dependencies` parameter:
|
||||
* A parameter in *path operation decorators*, for dependencies that should be executed but the return value is not important or not used in the *path operation function*.
|
||||
* A parameter in the `.include_router()` method of FastAPI applications and routers, to include dependencies that should be executed in each *path operation* in a router.
|
||||
* This is useful, for example, to require authentication or permissions in specific group of *path operations*.
|
||||
* This is useful, for example, to require authentication or permissions in a specific group of *path operations*.
|
||||
* Different `dependencies` can be applied to different routers.
|
||||
* These `dependencies` are run before the normal parameter dependencies. And normal dependencies are run too. They can be combined.
|
||||
* Dependencies declared in a router are executed first, then the ones defined in *path operation decorators*, and then the ones declared in normal parameters. They are all combined and executed.
|
||||
|
||||
@@ -16,10 +16,10 @@ PRs with suggestions to the language-specific LLM prompt require approval from a
|
||||
|
||||
Let's say that you want to request translations for a language that is not yet translated, not even some pages. For example, Latin.
|
||||
|
||||
* The first step would be for you to find other 2 people that would be willing to be reviewing translation PRs for that language with you.
|
||||
* The first step would be for you to find 2 other people who would be willing to be reviewing translation PRs for that language with you.
|
||||
* Once there are at least 3 people that would be willing to commit to help maintain that language, you can continue the next steps.
|
||||
* Create a new discussion following the template.
|
||||
* Tag the other 2 people that will help with the language, and ask them to confirm there they will help.
|
||||
* Tag the other 2 people that will help with the language, and ask them to confirm in the comments that they will help.
|
||||
|
||||
Once there are several people in the discussion, the FastAPI team can evaluate it and can make it an official translation.
|
||||
|
||||
|
||||
@@ -232,7 +232,7 @@ would mean:
|
||||
|
||||
But that file doesn't exist, our dependencies are in a file at `app/dependencies.py`.
|
||||
|
||||
Remember how our app/file structure looks like:
|
||||
Remember what our app/file structure looks like:
|
||||
|
||||
<img src="/img/tutorial/bigger-applications/package.drawio.svg">
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ Again, doing just that declaration, with **FastAPI** you get:
|
||||
|
||||
Apart from normal singular types like `str`, `int`, `float`, etc. you can use more complex singular types that inherit from `str`.
|
||||
|
||||
To see all the options you have, checkout [Pydantic's Type Overview](https://docs.pydantic.dev/latest/concepts/types/). You will see some examples in the next chapter.
|
||||
To see all the options you have, check out [Pydantic's Type Overview](https://docs.pydantic.dev/latest/concepts/types/). You will see some examples in the next chapter.
|
||||
|
||||
For example, as in the `Image` model we have a `url` field, we can declare it to be an instance of Pydantic's `HttpUrl` instead of a `str`:
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ To declare a **request** body, you use [Pydantic](https://docs.pydantic.dev/) mo
|
||||
|
||||
/// note
|
||||
|
||||
To send data, you should use one of: `POST` (the more common), `PUT`, `DELETE` or `PATCH`.
|
||||
To send data, you should use one of: `POST` (the most common), `PUT`, `DELETE` or `PATCH`.
|
||||
|
||||
Sending a body with a `GET` request has an undefined behavior in the specifications, nevertheless, it is supported by FastAPI, only for very complex/extreme use cases.
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ Here's how it might look:
|
||||
|
||||
---
|
||||
|
||||
If you use Pycharm, you can:
|
||||
If you use PyCharm, you can:
|
||||
|
||||
* Open the "Run" menu.
|
||||
* Select the option "Debug...".
|
||||
|
||||
@@ -234,6 +234,7 @@ participant operation as Path Operation
|
||||
Dependencies with `yield` have evolved over time to cover different use cases and fix some issues.
|
||||
|
||||
If you want to see what has changed in different versions of FastAPI, you can read more about it in the advanced guide, in [Advanced Dependencies - Dependencies with `yield`, `HTTPException`, `except` and Background Tasks](../../advanced/advanced-dependencies.md#dependencies-with-yield-httpexception-except-and-background-tasks).
|
||||
|
||||
## Context Managers { #context-managers }
|
||||
|
||||
### What are "Context Managers" { #what-are-context-managers }
|
||||
|
||||
@@ -36,7 +36,7 @@ Here are some of the additional data types you can use:
|
||||
* `datetime.timedelta`:
|
||||
* A Python `datetime.timedelta`.
|
||||
* In requests and responses will be represented as a `float` of total seconds.
|
||||
* Pydantic also allows representing it as a "ISO 8601 time diff encoding", [see the docs for more info](https://docs.pydantic.dev/latest/concepts/serialization/#custom-serializers).
|
||||
* Pydantic also allows representing it as an "ISO 8601 time diff encoding", [see the docs for more info](https://docs.pydantic.dev/latest/concepts/serialization/#custom-serializers).
|
||||
* `frozenset`:
|
||||
* In requests and responses, treated the same as a `set`:
|
||||
* In requests, a list will be read, eliminating duplicates and converting it to a `set`.
|
||||
|
||||
@@ -18,7 +18,7 @@ If you don't know, you will learn what a "password hash" is in the [security cha
|
||||
|
||||
## Multiple models { #multiple-models }
|
||||
|
||||
Here's a general idea of how the models could look like with their password fields and the places where they are used:
|
||||
Here's a general idea of what the models could look like with their password fields and the places where they are used:
|
||||
|
||||
{* ../../docs_src/extra_models/tutorial001_py310.py hl[7,9,14,20,22,27:28,31:33,38:39] *}
|
||||
|
||||
@@ -142,7 +142,7 @@ The supporting additional functions `fake_password_hasher` and `fake_save_user`
|
||||
|
||||
Reducing code duplication is one of the core ideas in **FastAPI**.
|
||||
|
||||
As code duplication increments the chances of bugs, security issues, code desynchronization issues (when you update in one place but not in the others), etc.
|
||||
As code duplication increases the chances of bugs, security issues, code desynchronization issues (when you update in one place but not in the others), etc.
|
||||
|
||||
And these models are all sharing a lot of the data and duplicating attribute names and types.
|
||||
|
||||
@@ -208,4 +208,4 @@ In this case, you can use `dict`:
|
||||
|
||||
Use multiple Pydantic models and inherit freely for each case.
|
||||
|
||||
You don't need to have a single data model per entity if that entity must be able to have different "states". As the case with the user "entity" with a state including `password`, `password_hash` and no password.
|
||||
You don't need to have a single data model per entity if that entity must be able to have different "states". The **user** "entity" is an example, with states that include `password`, `password_hash`, or no password.
|
||||
|
||||
@@ -108,7 +108,7 @@ OpenAPI defines an API schema for your API. And that schema includes definitions
|
||||
|
||||
#### Check the `openapi.json` { #check-the-openapi-json }
|
||||
|
||||
If you are curious about how the raw OpenAPI schema looks like, FastAPI automatically generates a JSON (schema) with the descriptions of all your API.
|
||||
If you are curious about what the raw OpenAPI schema looks like, FastAPI automatically generates a JSON (schema) with the descriptions of all your API.
|
||||
|
||||
You can see it directly at: [http://127.0.0.1:8000/openapi.json](http://127.0.0.1:8000/openapi.json).
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Handling Errors { #handling-errors }
|
||||
|
||||
There are many situations in which you need to notify an error to a client that is using your API.
|
||||
There are many situations in which you need to report an error to a client that is using your API.
|
||||
|
||||
This client could be a browser with a frontend, a code from someone else, an IoT device, etc.
|
||||
|
||||
@@ -71,7 +71,7 @@ They are handled automatically by **FastAPI** and converted to JSON.
|
||||
|
||||
## Add custom headers { #add-custom-headers }
|
||||
|
||||
There are some situations in where it's useful to be able to add custom headers to the HTTP error. For example, for some types of security.
|
||||
There are some situations where it's useful to be able to add custom headers to the HTTP error. For example, for some types of security.
|
||||
|
||||
You probably won't need to use it directly in your code.
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ FastAPI has an [official extension for VS Code](https://marketplace.visualstudio
|
||||
|
||||
## Advanced User Guide { #advanced-user-guide }
|
||||
|
||||
There is also an **Advanced User Guide** that you can read later after this **Tutorial - User guide**.
|
||||
There is also an **Advanced User Guide** that you can read later after this **Tutorial - User Guide**.
|
||||
|
||||
The **Advanced User Guide** builds on this one, uses the same concepts, and teaches you some extra features.
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ You can set the following fields that are used in the OpenAPI specification and
|
||||
| `title` | `str` | The title of the API. |
|
||||
| `summary` | `str` | A short summary of the API. <small>Available since OpenAPI 3.1.0, FastAPI 0.99.0.</small> |
|
||||
| `description` | `str` | A short description of the API. It can use Markdown. |
|
||||
| `version` | `string` | The version of the API. This is the version of your own application, not of OpenAPI. For example `2.5.0`. |
|
||||
| `version` | `str` | The version of the API. This is the version of your own application, not of OpenAPI. For example `2.5.0`. |
|
||||
| `terms_of_service` | `str` | A URL to the Terms of Service for the API. If provided, this has to be a URL. |
|
||||
| `contact` | `dict` | The contact information for the exposed API. It can contain several fields. <details><summary><code>contact</code> fields</summary><table><thead><tr><th>Parameter</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><code>name</code></td><td><code>str</code></td><td>The identifying name of the contact person/organization.</td></tr><tr><td><code>url</code></td><td><code>str</code></td><td>The URL pointing to the contact information. MUST be in the format of a URL.</td></tr><tr><td><code>email</code></td><td><code>str</code></td><td>The email address of the contact person/organization. MUST be in the format of an email address.</td></tr></tbody></table></details> |
|
||||
| `license_info` | `dict` | The license information for the exposed API. It can contain several fields. <details><summary><code>license_info</code> fields</summary><table><thead><tr><th>Parameter</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><code>name</code></td><td><code>str</code></td><td><strong>REQUIRED</strong> (if a <code>license_info</code> is set). The license name used for the API.</td></tr><tr><td><code>identifier</code></td><td><code>str</code></td><td>An [SPDX](https://spdx.org/licenses/) license expression for the API. The <code>identifier</code> field is mutually exclusive of the <code>url</code> field. <small>Available since OpenAPI 3.1.0, FastAPI 0.99.0.</small></td></tr><tr><td><code>url</code></td><td><code>str</code></td><td>A URL to the license used for the API. MUST be in the format of a URL.</td></tr></tbody></table></details> |
|
||||
|
||||
@@ -98,7 +98,7 @@ It will be clearly marked as deprecated in the interactive docs:
|
||||
|
||||
<img src="/img/tutorial/path-operation-configuration/image04.png">
|
||||
|
||||
Check how deprecated and non-deprecated *path operations* look like:
|
||||
Check how deprecated and non-deprecated *path operations* look:
|
||||
|
||||
<img src="/img/tutorial/path-operation-configuration/image05.png">
|
||||
|
||||
|
||||
@@ -406,7 +406,7 @@ But if you're curious about this specific code example and you're still entertai
|
||||
|
||||
#### String with `value.startswith()` { #string-with-value-startswith }
|
||||
|
||||
Did you notice? a string using `value.startswith()` can take a tuple, and it will check each value in the tuple:
|
||||
Did you notice? A string using `value.startswith()` can take a tuple, and it will check each value in the tuple:
|
||||
|
||||
{* ../../docs_src/query_params_str_validations/tutorial015_an_py310.py ln[16:19] hl[17] *}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ As they are part of the URL, they are "naturally" strings.
|
||||
|
||||
But when you declare them with Python types (in the example above, as `int`), they are converted to that type and validated against it.
|
||||
|
||||
All the same process that applied for path parameters also applies for query parameters:
|
||||
All the same processes that apply to path parameters also apply to query parameters:
|
||||
|
||||
* Editor support (obviously)
|
||||
* Data <dfn title="converting the string that comes from an HTTP request into Python data">"parsing"</dfn>
|
||||
|
||||
@@ -60,7 +60,7 @@ Using `UploadFile` has several advantages over `bytes`:
|
||||
|
||||
* You don't have to use `File()` in the default value of the parameter.
|
||||
* It uses a "spooled" file:
|
||||
* A file stored in memory up to a maximum size limit, and after passing this limit it will be stored in disk.
|
||||
* A file stored in memory up to a maximum size limit, and after passing this limit it will be stored on disk.
|
||||
* This means that it will work well for large files like images, videos, large binaries, etc. without consuming all the memory.
|
||||
* You can get metadata from the uploaded file.
|
||||
* It has a [file-like](https://docs.python.org/3/glossary.html#term-file-like-object) `async` interface.
|
||||
@@ -111,7 +111,7 @@ When you use the `async` methods, **FastAPI** runs the file methods in a threadp
|
||||
|
||||
## What is "Form Data" { #what-is-form-data }
|
||||
|
||||
The way HTML forms (`<form></form>`) sends the data to the server normally uses a "special" encoding for that data, it's different from JSON.
|
||||
The way HTML forms (`<form></form>`) send the data to the server normally uses a "special" encoding for that data, it's different from JSON.
|
||||
|
||||
**FastAPI** will make sure to read that data from the right place instead of JSON.
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ To declare form bodies, you need to use `Form` explicitly, because without it th
|
||||
|
||||
## About "Form Fields" { #about-form-fields }
|
||||
|
||||
The way HTML forms (`<form></form>`) sends the data to the server normally uses a "special" encoding for that data, it's different from JSON.
|
||||
The way HTML forms (`<form></form>`) send the data to the server normally uses a "special" encoding for that data, it's different from JSON.
|
||||
|
||||
**FastAPI** will make sure to read that data from the right place instead of JSON.
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ If you already know what HTTP status codes are, skip to the next section.
|
||||
|
||||
In HTTP, you send a numeric status code of 3 digits as part of the response.
|
||||
|
||||
These status codes have a name associated to recognize them, but the important part is the number.
|
||||
These status codes have an associated name to help recognize them, but the important part is the number.
|
||||
|
||||
In short:
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ Nevertheless, at the <dfn title="2023-08-26">time of writing this</dfn>, Swagger
|
||||
|
||||
### OpenAPI-specific `examples` { #openapi-specific-examples }
|
||||
|
||||
Since before **JSON Schema** supported `examples` OpenAPI had support for a different field also called `examples`.
|
||||
Since before **JSON Schema** supported `examples`, OpenAPI had support for a different field also called `examples`.
|
||||
|
||||
This **OpenAPI-specific** `examples` goes in another section in the OpenAPI specification. It goes in the **details for each *path operation***, not inside each JSON Schema.
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ And it can also be used by yourself, to debug, check and test the same applicati
|
||||
|
||||
## The `password` flow { #the-password-flow }
|
||||
|
||||
Now let's go back a bit and understand what is all that.
|
||||
Now let's go back a bit and understand what all that is.
|
||||
|
||||
The `password` "flow" is one of the ways ("flows") defined in OAuth2, to handle security and authentication.
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ First, let's create a Pydantic user model.
|
||||
|
||||
The same way we use Pydantic to declare bodies, we can use it anywhere else:
|
||||
|
||||
{* ../../docs_src/security/tutorial002_an_py310.py hl[5,12:6] *}
|
||||
{* ../../docs_src/security/tutorial002_an_py310.py hl[5,12:16] *}
|
||||
|
||||
## Create a `get_current_user` dependency { #create-a-get-current-user-dependency }
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ This ensures the endpoint takes roughly the same amount of time to respond wheth
|
||||
|
||||
/// note
|
||||
|
||||
If you check the new (fake) database `fake_users_db`, you will see how the hashed password looks like now: `"$argon2id$v=19$m=65536,t=3,p=4$wagCPXjifgvUFBzq4hqe3w$CYaIb8sB+wtD+Vu/P4uod1+Qof8h+1g7bbDlBID48Rc"`.
|
||||
If you check the new (fake) database `fake_users_db`, you will see what the hashed password looks like now: `"$argon2id$v=19$m=65536,t=3,p=4$wagCPXjifgvUFBzq4hqe3w$CYaIb8sB+wtD+Vu/P4uod1+Qof8h+1g7bbDlBID48Rc"`.
|
||||
|
||||
///
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ Now let's build from the previous chapter and add the missing parts to have a co
|
||||
|
||||
We are going to use **FastAPI** security utilities to get the `username` and `password`.
|
||||
|
||||
OAuth2 specifies that when using the "password flow" (that we are using) the client/user must send a `username` and `password` fields as form data.
|
||||
OAuth2 specifies that when using the "password flow" (that we are using) the client/user must send `username` and `password` fields as form data.
|
||||
|
||||
And the spec says that the fields have to be named like that. So `user-name` or `email` wouldn't work.
|
||||
|
||||
@@ -146,7 +146,7 @@ UserInDB(
|
||||
|
||||
/// note
|
||||
|
||||
For a more complete explanation of `**user_dict` check back in [the documentation for **Extra Models**](../extra-models.md#about-user-in-dict).
|
||||
For a more complete explanation of `**user_dict` check back in [the documentation for **Extra Models**](../extra-models.md#about-user-in-model-dump).
|
||||
|
||||
///
|
||||
|
||||
@@ -190,7 +190,7 @@ We want to get the `current_user` *only* if this user is active.
|
||||
|
||||
So, we create an additional dependency `get_current_active_user` that in turn uses `get_current_user` as a dependency.
|
||||
|
||||
Both of these dependencies will just return an HTTP error if the user doesn't exist, or if is inactive.
|
||||
Both of these dependencies will just return an HTTP error if the user doesn't exist, or is inactive.
|
||||
|
||||
So, in our endpoint, we will only get a user if the user exists, was correctly authenticated, and is active:
|
||||
|
||||
|
||||
@@ -201,7 +201,7 @@ Then let's create `Hero`, the actual *table model*, with the **extra fields** th
|
||||
* `id`
|
||||
* `secret_name`
|
||||
|
||||
Because `Hero` inherits form `HeroBase`, it **also** has the **fields** declared in `HeroBase`, so all the fields for `Hero` are:
|
||||
Because `Hero` inherits from `HeroBase`, it **also** has the **fields** declared in `HeroBase`, so all the fields for `Hero` are:
|
||||
|
||||
* `id`
|
||||
* `name`
|
||||
|
||||
@@ -33,7 +33,7 @@ The `directory="static"` refers to the name of the directory that contains your
|
||||
|
||||
The `name="static"` gives it a name that can be used internally by **FastAPI**.
|
||||
|
||||
All these parameters can be different than "`static`", adjust them with the needs and specific details of your own application.
|
||||
All these parameters can be different than "`static`", adjust them to the needs and specific details of your own application.
|
||||
|
||||
## More info { #more-info }
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ Import `TestClient`.
|
||||
|
||||
Create a `TestClient` by passing your **FastAPI** application to it.
|
||||
|
||||
Create functions with a name that starts with `test_` (this is standard `pytest` conventions).
|
||||
Create functions with a name that starts with `test_` (this is a standard `pytest` convention).
|
||||
|
||||
Use the `TestClient` object the same way as you do with `httpx`.
|
||||
|
||||
|
||||
@@ -100,7 +100,7 @@ $ uv venv
|
||||
|
||||
By default, `uv` will create a virtual environment in a directory called `.venv`.
|
||||
|
||||
But you could customize it passing an additional argument with the directory name.
|
||||
But you could customize it by passing an additional argument with the directory name.
|
||||
|
||||
///
|
||||
|
||||
@@ -258,7 +258,7 @@ $ python -m ensurepip --upgrade
|
||||
|
||||
</div>
|
||||
|
||||
This command will install pip if it is not already installed and also ensures that the installed version of pip is at least as recent as the one available in `ensurepip`.
|
||||
This command will install pip if it is not already installed and also ensure that the installed version of pip is at least as recent as the one available in `ensurepip`.
|
||||
|
||||
///
|
||||
|
||||
@@ -447,7 +447,7 @@ Now you're ready to start working on your project.
|
||||
|
||||
/// tip
|
||||
|
||||
Do you want to understand what's all that above?
|
||||
Do you want to understand what all that above is?
|
||||
|
||||
Continue reading. 👇🤓
|
||||
|
||||
@@ -548,7 +548,7 @@ Also, depending on your operating system (e.g. Linux, Windows, macOS), it could
|
||||
|
||||
## Where are Packages Installed { #where-are-packages-installed }
|
||||
|
||||
When you install Python, it creates some directories with some files in your computer.
|
||||
When you install Python, it creates some directories with some files on your computer.
|
||||
|
||||
Some of these directories are the ones in charge of having all the packages you install.
|
||||
|
||||
@@ -568,7 +568,7 @@ That will download a compressed file with the FastAPI code, normally from [PyPI]
|
||||
|
||||
It will also **download** files for other packages that FastAPI depends on.
|
||||
|
||||
Then it will **extract** all those files and put them in a directory in your computer.
|
||||
Then it will **extract** all those files and put them in a directory on your computer.
|
||||
|
||||
By default, it will put those files downloaded and extracted in the directory that comes with your Python installation, that's the **global environment**.
|
||||
|
||||
@@ -846,7 +846,7 @@ This is a simple guide to get you started and teach you how everything works **u
|
||||
|
||||
There are many **alternatives** to managing virtual environments, package dependencies (requirements), projects.
|
||||
|
||||
Once you are ready and want to use a tool to **manage the entire project**, packages dependencies, virtual environments, etc. I would suggest you try [uv](https://github.com/astral-sh/uv).
|
||||
Once you are ready and want to use a tool to **manage the entire project**, package dependencies, virtual environments, etc. I would suggest you try [uv](https://github.com/astral-sh/uv).
|
||||
|
||||
`uv` can do a lot of things, it can:
|
||||
|
||||
|
||||
@@ -303,6 +303,8 @@ extra:
|
||||
name: es - español
|
||||
- link: /fr/
|
||||
name: fr - français
|
||||
- link: /hi/
|
||||
name: hi - हिन्दी
|
||||
- link: /ja/
|
||||
name: ja - 日本語
|
||||
- link: /ko/
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<a class="announce-link" href="https://fastapicloud.com" target="_blank">
|
||||
<span class="twemoji">
|
||||
{% include ".icons/material/cloud-arrow-up.svg" %}
|
||||
</span> Join the <strong>FastAPI Cloud</strong> waiting list 🚀
|
||||
</span> Deploy on <strong>FastAPI Cloud</strong> 🚀
|
||||
</a>
|
||||
</div>
|
||||
<div class="item">
|
||||
|
||||
495
docs/hi/docs/_llm-test.md
Normal file
495
docs/hi/docs/_llm-test.md
Normal file
@@ -0,0 +1,495 @@
|
||||
# LLM परीक्षण फ़ाइल { #llm-test-file }
|
||||
|
||||
यह दस्तावेज़ यह परखता है कि <abbr title="Large Language Model - बड़ा भाषा मॉडल">LLM</abbr>, जो डॉक्यूमेंटेशन का अनुवाद करता है, `scripts/translate.py` में दिए गए `general_prompt` और `docs/{language code}/llm-prompt.md` में दिए गए भाषा-विशिष्ट प्रॉम्प्ट को समझता है या नहीं। भाषा-विशिष्ट प्रॉम्प्ट को `general_prompt` के साथ जोड़ा जाता है।
|
||||
|
||||
यहाँ जो परीक्षण जोड़े गए हैं, वे भाषा-विशिष्ट प्रॉम्प्ट के सभी डिज़ाइनर्स को दिखाई देंगे।
|
||||
|
||||
उपयोग इस प्रकार करें:
|
||||
|
||||
* एक भाषा-विशिष्ट प्रॉम्प्ट रखें - `docs/{language code}/llm-prompt.md`।
|
||||
* इस दस्तावेज़ का अपने इच्छित लक्ष्य-भाषा में नया अनुवाद करें (उदाहरण के लिए `translate.py` के `translate-page` कमांड को देखें)। यह अनुवाद `docs/{language code}/docs/_llm-test.md` के अंतर्गत बना देगा।
|
||||
* जाँचें कि अनुवाद में सब कुछ ठीक है।
|
||||
* आवश्यकता होने पर, अपने भाषा-विशिष्ट प्रॉम्प्ट, जनरल प्रॉम्प्ट या अंग्रेज़ी दस्तावेज़ में सुधार करें।
|
||||
* फिर अनुवाद में बचे हुए मुद्दों को हाथ से ठीक करें ताकि यह एक अच्छा अनुवाद बन जाए।
|
||||
* दुबारा अनुवाद करें, इस बार अच्छा अनुवाद जगह पर रहते हुए। आदर्श परिणाम होगा कि LLM अब अनुवाद में कोई परिवर्तन न करे। इसका मतलब है कि जनरल प्रॉम्प्ट और आपका भाषा-विशिष्ट प्रॉम्प्ट जितने अच्छे हो सकते हैं उतने अच्छे हैं (कभी-कभी यह कुछ यादृच्छिक-से परिवर्तन कर देगा, कारण यह है कि [LLM नियतात्मक एल्गोरिथ्म नहीं हैं](https://doublespeak.chat/#/handbook#deterministic-output))।
|
||||
|
||||
परीक्षण:
|
||||
|
||||
## कोड स्निपेट्स { #code-snippets }
|
||||
|
||||
//// tab | परीक्षण
|
||||
|
||||
यह एक कोड स्निपेट है: `foo`। और यह एक और कोड स्निपेट है: `bar`। और एक और: `baz quux`।
|
||||
|
||||
////
|
||||
|
||||
//// tab | जानकारी
|
||||
|
||||
कोड स्निपेट्स की सामग्री को ज्यों का त्यों छोड़ देना चाहिए।
|
||||
|
||||
`scripts/translate.py` में जनरल प्रॉम्प्ट के सेक्शन `### Content of code snippets` को देखें।
|
||||
|
||||
////
|
||||
|
||||
## उद्धरण { #quotes }
|
||||
|
||||
//// tab | परीक्षण
|
||||
|
||||
कल, मेरे दोस्त ने लिखा: "अगर आप 'गलत' को सही लिखते हैं, तो आपने उसे गलत लिखा है"। जिसके जवाब में मैंने कहा: "सही, लेकिन 'गलत' गलत है '"गलत"' नहीं"।
|
||||
|
||||
/// note | टिप्पणी
|
||||
|
||||
LLM संभवतः इसे गलत अनुवादित करेगा। दिलचस्प यह है कि पुनः-अनुवाद करने पर क्या यह ठीक किया हुआ अनुवाद बनाए रखता है।
|
||||
|
||||
///
|
||||
|
||||
////
|
||||
|
||||
//// tab | जानकारी
|
||||
|
||||
प्रॉम्प्ट डिज़ाइनर यह चुन सकते हैं कि वे साधारण कोट्स को टाइपोग्राफ़िक कोट्स में बदलना चाहते हैं या नहीं। उन्हें ज्यों का त्यों छोड़ना भी ठीक है।
|
||||
|
||||
उदाहरण के लिए `docs/de/llm-prompt.md` में सेक्शन `### Quotes` देखें।
|
||||
|
||||
////
|
||||
|
||||
## कोड स्निपेट्स में उद्धरण { #quotes-in-code-snippets }
|
||||
|
||||
//// tab | परीक्षण
|
||||
|
||||
`pip install "foo[bar]"`
|
||||
|
||||
कोड स्निपेट्स में स्ट्रिंग लिटरल्स के उदाहरण: `"this"`, `'that'`.
|
||||
|
||||
कोड स्निपेट्स में स्ट्रिंग लिटरल्स का एक कठिन उदाहरण: `f"I like {'oranges' if orange else "apples"}"`
|
||||
|
||||
हार्डकोर: `Yesterday, my friend wrote: "If you spell incorrectly correctly, you have spelled it incorrectly". To which I answered: "Correct, but 'incorrectly' is incorrectly not '"incorrectly"'"`
|
||||
|
||||
////
|
||||
|
||||
//// tab | जानकारी
|
||||
|
||||
... लेकिन, कोड स्निपेट्स के अंदर के उद्धरण ज्यों के त्यों रहने चाहिए।
|
||||
|
||||
////
|
||||
|
||||
## कोड ब्लॉक्स { #code-blocks }
|
||||
|
||||
//// tab | परीक्षण
|
||||
|
||||
एक Bash कोड उदाहरण...
|
||||
|
||||
```bash
|
||||
# ब्रह्मांड के लिए अभिवादन प्रिंट करें
|
||||
echo "Hello universe"
|
||||
```
|
||||
|
||||
...और एक कंसोल कोड उदाहरण...
|
||||
|
||||
```console
|
||||
$ <font color="#4E9A06">fastapi</font> run <u style="text-decoration-style:solid">main.py</u>
|
||||
<span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting server
|
||||
Searching for package file structure
|
||||
```
|
||||
|
||||
...और एक अन्य कंसोल कोड उदाहरण...
|
||||
|
||||
```console
|
||||
// "Code" नाम की डायरेक्टरी बनाएँ
|
||||
$ mkdir code
|
||||
// उस डायरेक्टरी में जाएँ
|
||||
$ cd code
|
||||
```
|
||||
|
||||
...और एक Python कोड उदाहरण...
|
||||
|
||||
```Python
|
||||
wont_work() # यह काम नहीं करेगा 😱
|
||||
works(foo="bar") # यह काम करता है 🎉
|
||||
```
|
||||
|
||||
...और बस इतना ही।
|
||||
|
||||
////
|
||||
|
||||
//// tab | जानकारी
|
||||
|
||||
कोड ब्लॉक्स के अंदर के कोड में बदलाव नहीं होना चाहिए, सिवाय टिप्पणियों (comments) के।
|
||||
|
||||
`scripts/translate.py` में जनरल प्रॉम्प्ट के सेक्शन `### Content of code blocks` को देखें।
|
||||
|
||||
////
|
||||
|
||||
## टैब और रंगीन बॉक्स { #tabs-and-colored-boxes }
|
||||
|
||||
//// tab | परीक्षण
|
||||
|
||||
/// note | टिप्पणी
|
||||
कुछ पाठ
|
||||
///
|
||||
|
||||
/// note | तकनीकी विवरण
|
||||
कुछ पाठ
|
||||
///
|
||||
|
||||
/// tip | सुझाव
|
||||
कुछ पाठ
|
||||
///
|
||||
|
||||
/// warning | चेतावनी
|
||||
कुछ पाठ
|
||||
///
|
||||
|
||||
/// danger | खतरा
|
||||
कुछ पाठ
|
||||
///
|
||||
|
||||
////
|
||||
|
||||
//// tab | जानकारी
|
||||
|
||||
टैब और `Info`/`Note`/`Warning`/आदि ब्लॉक्स में उनके शीर्षक का अनुवाद ऊर्ध्वाधर रेखा (`|`) के बाद जोड़ा जाना चाहिए।
|
||||
|
||||
`scripts/translate.py` में जनरल प्रॉम्प्ट के सेक्शन `### Special blocks` और `### Tab blocks` देखें।
|
||||
|
||||
////
|
||||
|
||||
## वेब और आंतरिक लिंक { #web-and-internal-links }
|
||||
|
||||
//// tab | परीक्षण
|
||||
|
||||
लिंक का टेक्स्ट अनुवादित होना चाहिए, लिंक का पता अपरिवर्तित रहे:
|
||||
|
||||
* [ऊपर दिए गए शीर्षक का लिंक](#code-snippets)
|
||||
* [आंतरिक लिंक](index.md#installation)
|
||||
* [बाहरी लिंक](https://sqlmodel.tiangolo.com/)
|
||||
* [एक स्टाइल का लिंक](https://fastapi.tiangolo.com/css/styles.css)
|
||||
* [एक स्क्रिप्ट का लिंक](https://fastapi.tiangolo.com/js/logic.js)
|
||||
* [एक छवि का लिंक](https://fastapi.tiangolo.com/img/foo.jpg)
|
||||
|
||||
लिंक का टेक्स्ट अनुवादित होना चाहिए, लिंक का पता अनुवाद की ओर इशारा करना चाहिए:
|
||||
|
||||
* [FastAPI लिंक](https://fastapi.tiangolo.com/hi/)
|
||||
|
||||
////
|
||||
|
||||
//// tab | जानकारी
|
||||
|
||||
लिंक अनुवादित होने चाहिए, लेकिन उनके पते अपरिवर्तित रहें। अपवाद है FastAPI डॉक्यूमेंटेशन के पेजों के पूर्ण (absolute) लिंक। उस स्थिति में लिंक अनुवाद की ओर इशारा करना चाहिए।
|
||||
|
||||
`scripts/translate.py` में जनरल प्रॉम्प्ट के सेक्शन `### Links` देखें।
|
||||
|
||||
////
|
||||
|
||||
## HTML "abbr" एलिमेंट्स { #html-abbr-elements }
|
||||
|
||||
//// tab | परीक्षण
|
||||
|
||||
यहाँ HTML "abbr" एलिमेंट्स में लिपटी कुछ चीज़ें हैं (कुछ गढ़ी हुई भी):
|
||||
|
||||
### abbr एक पूरा वाक्यांश देता है { #the-abbr-gives-a-full-phrase }
|
||||
|
||||
* <abbr title="Getting Things Done - काम पूरे करना">GTD</abbr>
|
||||
* <abbr title="less than - से कम"><code>lt</code></abbr>
|
||||
* <abbr title="XML Web Token - XML वेब टोकन">XWT</abbr>
|
||||
* <abbr title="Parallel Server Gateway Interface - समानांतर सर्वर गेटवे इंटरफ़ेस">PSGI</abbr>
|
||||
|
||||
### abbr एक पूरा वाक्यांश और उसका स्पष्टीकरण देता है { #the-abbr-gives-a-full-phrase-and-an-explanation }
|
||||
|
||||
* <abbr title="Mozilla Developer Network - मोज़िला डेवलपर नेटवर्क: डेवलपर्स के लिए प्रलेखन, फ़ायरफ़ॉक्स टीम द्वारा लिखा गया">MDN</abbr>
|
||||
* <abbr title="Input/Output - इनपुट/आउटपुट: डिस्क का पढ़ना या लिखना, नेटवर्क संचार।">I/O</abbr>.
|
||||
|
||||
////
|
||||
|
||||
//// tab | जानकारी
|
||||
|
||||
"abbr" एलिमेंट्स के "title" ऐट्रिब्यूट्स का अनुवाद कुछ विशिष्ट निर्देशों का पालन करते हुए किया जाता है।
|
||||
|
||||
अनुवाद अपने स्वयं के "abbr" एलिमेंट्स जोड़ सकते हैं जिन्हें LLM को हटाना नहीं चाहिए। जैसे अंग्रेज़ी शब्दों को समझाने के लिए।
|
||||
|
||||
`scripts/translate.py` में जनरल प्रॉम्प्ट के सेक्शन `### HTML abbr elements` देखें।
|
||||
|
||||
////
|
||||
|
||||
## HTML "dfn" एलिमेंट्स { #html-dfn-elements }
|
||||
|
||||
* <dfn title="ऐसी मशीनों का समूह जिन्हें किसी तरह से एक-दूसरे से जुड़ने और साथ काम करने के लिए कॉन्फ़िगर किया गया है।">क्लस्टर</dfn>
|
||||
* <dfn title="मशीन लर्निंग की एक विधि जो इनपुट और आउटपुट लेयर्स के बीच कई छुपी हुई लेयर्स वाले कृत्रिम न्यूरल नेटवर्क्स का उपयोग करती है, इस प्रकार एक व्यापक आंतरिक संरचना विकसित करती है">डीप लर्निंग</dfn>
|
||||
|
||||
## शीर्षक { #headings }
|
||||
|
||||
//// tab | परीक्षण
|
||||
|
||||
### एक वेबऐप विकसित करें - एक ट्यूटोरियल { #develop-a-webapp-a-tutorial }
|
||||
|
||||
नमस्ते।
|
||||
|
||||
### टाइप हिंट्स और -एनोटेशन्स { #type-hints-and-annotations }
|
||||
|
||||
फिर से नमस्ते।
|
||||
|
||||
### सुपर- और सबक्लासेज़ { #super-and-subclasses }
|
||||
|
||||
फिर से नमस्ते।
|
||||
|
||||
////
|
||||
|
||||
//// tab | जानकारी
|
||||
|
||||
शीर्षकों के लिए एकमात्र कड़ा नियम यह है कि LLM कर्ली ब्रैकेट्स के अंदर के हैश-पार्ट को अपरिवर्तित छोड़े, जिससे लिंक न टूटें।
|
||||
|
||||
`scripts/translate.py` में जनरल प्रॉम्प्ट के सेक्शन `### Headings` देखें।
|
||||
|
||||
कुछ भाषा-विशिष्ट निर्देशों के लिए, जैसे `docs/de/llm-prompt.md` में सेक्शन `### Headings` देखें।
|
||||
|
||||
////
|
||||
|
||||
## डॉक्स में प्रयुक्त शब्द { #terms-used-in-the-docs }
|
||||
|
||||
//// tab | परीक्षण
|
||||
|
||||
* आप
|
||||
* आपका
|
||||
|
||||
* उदा.
|
||||
* आदि
|
||||
|
||||
* `foo` एक `int` के रूप में
|
||||
* `bar` एक `str` के रूप में
|
||||
* `baz` एक `list` के रूप में
|
||||
|
||||
* ट्यूटोरियल - उपयोगकर्ता गाइड
|
||||
* उन्नत उपयोगकर्ता गाइड
|
||||
* SQLModel डॉक्स
|
||||
* API डॉक्स
|
||||
* स्वचालित डॉक्स
|
||||
|
||||
* डेटा साइंस
|
||||
* डीप लर्निंग
|
||||
* मशीन लर्निंग
|
||||
* डिपेंडेंसी इंजेक्शन
|
||||
* HTTP बेसिक ऑथेंटिकेशन
|
||||
* HTTP डाइजेस्ट
|
||||
* ISO फ़ॉरमैट
|
||||
* JSON Schema मानक
|
||||
* JSON स्कीमा
|
||||
* स्कीमा परिभाषा
|
||||
* पासवर्ड फ्लो
|
||||
* मोबाइल
|
||||
|
||||
* अप्रचलित
|
||||
* डिज़ाइन किया गया
|
||||
* अमान्य
|
||||
* तुरंत
|
||||
* मानक
|
||||
* डिफ़ॉल्ट
|
||||
* केस-संवेदी
|
||||
* केस-असंवेदी
|
||||
|
||||
* एप्लिकेशन को सर्व करना
|
||||
* पेज को सर्व करना
|
||||
|
||||
* ऐप
|
||||
* एप्लिकेशन
|
||||
|
||||
* रिक्वेस्ट
|
||||
* रिस्पांस
|
||||
* त्रुटि रिस्पांस
|
||||
|
||||
* पाथ ऑपरेशन
|
||||
* पाथ ऑपरेशन डेकोरेटर
|
||||
* पाथ ऑपरेशन फ़ंक्शन
|
||||
|
||||
* बॉडी
|
||||
* रिक्वेस्ट बॉडी
|
||||
* रिस्पांस बॉडी
|
||||
* JSON बॉडी
|
||||
* फॉर्म बॉडी
|
||||
* फ़ाइल बॉडी
|
||||
* फ़ंक्शन बॉडी
|
||||
|
||||
* पैरामीटर
|
||||
* बॉडी पैरामीटर
|
||||
* पाथ पैरामीटर
|
||||
* क्वेरी पैरामीटर
|
||||
* कुकी पैरामीटर
|
||||
* हेडर पैरामीटर
|
||||
* फॉर्म पैरामीटर
|
||||
* फ़ंक्शन पैरामीटर
|
||||
|
||||
* इवेंट
|
||||
* स्टार्टअप इवेंट
|
||||
* सर्वर का स्टार्टअप
|
||||
* शटडाउन इवेंट
|
||||
* लाइफस्पैन इवेंट
|
||||
|
||||
* हैंडलर
|
||||
* इवेंट हैंडलर
|
||||
* एक्सेप्शन हैंडलर
|
||||
* हैंडल करना
|
||||
|
||||
* मॉडल
|
||||
* Pydantic मॉडल
|
||||
* डेटा मॉडल
|
||||
* डेटाबेस मॉडल
|
||||
* फॉर्म मॉडल
|
||||
* मॉडल ऑब्जेक्ट
|
||||
|
||||
* क्लास
|
||||
* बेस क्लास
|
||||
* पैरेंट क्लास
|
||||
* सबक्लास
|
||||
* चाइल्ड क्लास
|
||||
* सिब्लिंग क्लास
|
||||
* क्लास मेथड
|
||||
|
||||
* हेडर
|
||||
* हेडर्स
|
||||
* ऑथराइज़ेशन हेडर
|
||||
* `Authorization` हेडर
|
||||
* फॉरवर्डेड हेडर
|
||||
|
||||
* डिपेंडेंसी इंजेक्शन सिस्टम
|
||||
* डिपेंडेंसी
|
||||
* डिपेंडेबल
|
||||
* डिपेन्डन्ट
|
||||
|
||||
* I/O बाउंड
|
||||
* CPU बाउंड
|
||||
* समकालिकता
|
||||
* समान्तरता
|
||||
* मल्टीप्रोसेसिंग
|
||||
|
||||
* env var
|
||||
* पर्यावरण चर
|
||||
* `PATH`
|
||||
* `PATH` वेरिएबल
|
||||
|
||||
* प्रमाणीकरण
|
||||
* प्रमाणीकरण प्रदाता
|
||||
* अधिकारीकरण
|
||||
* अधिकारीकरण फॉर्म
|
||||
* अधिकारीकरण प्रदाता
|
||||
* उपयोगकर्ता प्रमाणीकरण करता है
|
||||
* सिस्टम उपयोगकर्ता का प्रमाणीकरण करता है
|
||||
|
||||
* CLI
|
||||
* कमांड लाइन इंटरफेस
|
||||
|
||||
* सर्वर
|
||||
* क्लाइंट
|
||||
|
||||
* क्लाउड प्रदाता
|
||||
* क्लाउड सेवा
|
||||
|
||||
* विकास
|
||||
* विकास चरण
|
||||
|
||||
* dict
|
||||
* डिक्शनरी
|
||||
* एन्युमरेशन
|
||||
* एनम
|
||||
* एनम सदस्य
|
||||
|
||||
* एन्कोडर
|
||||
* डीकोडर
|
||||
* एन्कोड करना
|
||||
* डीकोड करना
|
||||
|
||||
* एक्सेप्शन
|
||||
* रेज़ करना
|
||||
|
||||
* एक्सप्रेशन
|
||||
* स्टेटमेंट
|
||||
|
||||
* फ्रंटएंड
|
||||
* बैकएंड
|
||||
|
||||
* GitHub चर्चा
|
||||
* GitHub इश्यू
|
||||
|
||||
* प्रदर्शन
|
||||
* प्रदर्शन अनुकूलन
|
||||
|
||||
* रिटर्न टाइप
|
||||
* रिटर्न वैल्यू
|
||||
|
||||
* सुरक्षा
|
||||
* सुरक्षा स्कीम
|
||||
|
||||
* टास्क
|
||||
* बैकग्राउंड टास्क
|
||||
* टास्क फ़ंक्शन
|
||||
|
||||
* टेम्पलेट
|
||||
* टेम्पलेट इंजन
|
||||
|
||||
* टाइप एनोटेशन
|
||||
* टाइप हिंट
|
||||
|
||||
* सर्वर वर्कर
|
||||
* Uvicorn वर्कर
|
||||
* Gunicorn Worker
|
||||
* वर्कर प्रोसेस
|
||||
* वर्कर क्लास
|
||||
* वर्कलोड
|
||||
|
||||
* डिप्लॉयमेंट
|
||||
* डिप्लॉय करना
|
||||
|
||||
* SDK
|
||||
* सॉफ़्टवेयर डेवलपमेंट किट
|
||||
|
||||
* `APIRouter`
|
||||
* `requirements.txt`
|
||||
* Bearer Token
|
||||
* ब्रेकिंग चेंज
|
||||
* बग
|
||||
* बटन
|
||||
* कॉल करने योग्य
|
||||
* कोड
|
||||
* कमिट
|
||||
* कॉन्टेक्स्ट मैनेजर
|
||||
* कोरूटीन
|
||||
* डेटाबेस सेशन
|
||||
* डिस्क
|
||||
* डोमेन
|
||||
* इंजन
|
||||
* नकली X
|
||||
* HTTP GET मेथड
|
||||
* आइटम
|
||||
* लाइब्रेरी
|
||||
* लाइफस्पैन
|
||||
* लॉक
|
||||
* मिडलवेयर
|
||||
* मोबाइल एप्लिकेशन
|
||||
* मॉड्यूल
|
||||
* माउंटिंग
|
||||
* नेटवर्क
|
||||
* ओरिजिन
|
||||
* ओवरराइड
|
||||
* पेलोड
|
||||
* प्रोसेसर
|
||||
* प्रॉपर्टी
|
||||
* प्रॉक्सी
|
||||
* पुल रिक्वेस्ट
|
||||
* क्वेरी
|
||||
* RAM
|
||||
* रिमोट मशीन
|
||||
* स्टेटस कोड
|
||||
* स्ट्रिंग
|
||||
* टैग
|
||||
* वेब फ़्रेमवर्क
|
||||
* वाइल्डकार्ड
|
||||
* वापस करना
|
||||
* सत्यापित करना
|
||||
|
||||
////
|
||||
|
||||
//// tab | जानकारी
|
||||
|
||||
यह डॉक्स में दिखने वाले (ज़्यादातर) तकनीकी शब्दों की न तो पूर्ण और न ही मानक सूची है। यह प्रॉम्प्ट डिज़ाइनर को यह समझने में मदद कर सकती है कि किन शब्दों के लिए LLM को सहायक निर्देशों की ज़रूरत है। उदाहरण के लिए जब यह एक अच्छे अनुवाद को कमतर अनुवाद में वापस बदल देता है। या जब इसे आपकी भाषा में किसी शब्द का रूपांतरण/विभक्ति करने में समस्या होती है।
|
||||
|
||||
उदाहरण के लिए `docs/de/llm-prompt.md` में सेक्शन `### List of English terms and their preferred German translations` देखें।
|
||||
|
||||
////
|
||||
485
docs/hi/docs/alternatives.md
Normal file
485
docs/hi/docs/alternatives.md
Normal file
@@ -0,0 +1,485 @@
|
||||
# विकल्प, प्रेरणा और तुलनाएँ { #alternatives-inspiration-and-comparisons }
|
||||
|
||||
**FastAPI** को किससे प्रेरणा मिली, यह विकल्पों की तुलना में कैसा है और उनसे इसने क्या सीखा।
|
||||
|
||||
## परिचय { #intro }
|
||||
|
||||
दूसरों के पिछले काम के बिना **FastAPI** अस्तित्व में नहीं होता।
|
||||
|
||||
इससे पहले कई टूल बनाए गए हैं जिन्होंने इसके निर्माण को प्रेरित करने में मदद की।
|
||||
|
||||
मैं कई वर्षों तक एक नया framework बनाने से बचता रहा। पहले मैंने **FastAPI** द्वारा कवर की गई सभी विशेषताओं को कई अलग-अलग frameworks, plug-ins और tools का उपयोग करके हल करने की कोशिश की।
|
||||
|
||||
लेकिन एक समय ऐसा आया जब ऐसा कुछ बनाने के अलावा कोई विकल्प नहीं था जो ये सभी सुविधाएँ प्रदान करे, पिछले tools से सर्वोत्तम विचारों को लेकर, और उन्हें सबसे अच्छे तरीके से मिलाकर, उन language features का उपयोग करते हुए जो पहले उपलब्ध भी नहीं थे (Python 3.6+ type hints)।
|
||||
|
||||
## पिछले tools { #previous-tools }
|
||||
|
||||
### [Django](https://www.djangoproject.com/) { #django }
|
||||
|
||||
यह सबसे लोकप्रिय Python framework है और व्यापक रूप से भरोसेमंद है। इसका उपयोग Instagram जैसे systems बनाने के लिए किया जाता है।
|
||||
|
||||
यह relational databases (जैसे MySQL या PostgreSQL) के साथ अपेक्षाकृत tightly coupled है, इसलिए मुख्य store engine के रूप में NoSQL database (जैसे Couchbase, MongoDB, Cassandra, आदि) रखना बहुत आसान नहीं है।
|
||||
|
||||
इसे backend में HTML generate करने के लिए बनाया गया था, न कि किसी modern frontend (जैसे React, Vue.js और Angular) या इसके साथ संचार करने वाले अन्य systems (जैसे <abbr title="Internet of Things - इंटरनेट ऑफ थिंग्स">IoT</abbr> devices) द्वारा उपयोग की जाने वाली APIs बनाने के लिए।
|
||||
|
||||
### [Django REST Framework](https://www.django-rest-framework.org/) { #django-rest-framework }
|
||||
|
||||
Django REST Framework को Django के ऊपर Web APIs बनाने के लिए एक flexible toolkit के रूप में बनाया गया था, ताकि इसकी API क्षमताओं में सुधार हो सके।
|
||||
|
||||
इसका उपयोग Mozilla, Red Hat और Eventbrite सहित कई कंपनियाँ करती हैं।
|
||||
|
||||
यह **automatic API documentation** के पहले उदाहरणों में से एक था, और यह विशेष रूप से उन पहले विचारों में से एक था जिसने **FastAPI** की "खोज" को प्रेरित किया।
|
||||
|
||||
/// note | नोट
|
||||
|
||||
Django REST Framework को Tom Christie ने बनाया था। वही Starlette और Uvicorn के creator हैं, जिन पर **FastAPI** आधारित है।
|
||||
|
||||
///
|
||||
|
||||
/// tip | **FastAPI** को इससे प्रेरणा मिली कि
|
||||
|
||||
एक automatic API documentation web user interface हो।
|
||||
|
||||
///
|
||||
|
||||
### [Flask](https://flask.palletsprojects.com) { #flask }
|
||||
|
||||
Flask एक "microframework" है, इसमें database integrations या Django में default रूप से आने वाली कई चीज़ें शामिल नहीं हैं।
|
||||
|
||||
यह simplicity और flexibility मुख्य data storage system के रूप में NoSQL databases का उपयोग करने जैसी चीज़ें करने की अनुमति देती है।
|
||||
|
||||
क्योंकि यह बहुत सरल है, इसे सीखना अपेक्षाकृत सहज है, हालांकि documentation कुछ बिंदुओं पर थोड़ा technical हो जाता है।
|
||||
|
||||
इसका उपयोग आमतौर पर उन अन्य applications के लिए भी किया जाता है जिन्हें जरूरी नहीं कि database, user management, या Django में पहले से built-in आने वाली कई features की आवश्यकता हो। हालांकि इनमें से कई features plug-ins के साथ जोड़े जा सकते हैं।
|
||||
|
||||
Parts का यह decoupling, और एक "microframework" होना जिसे ठीक वही कवर करने के लिए extend किया जा सके जिसकी आवश्यकता है, एक key feature था जिसे मैं बनाए रखना चाहता था।
|
||||
|
||||
Flask की simplicity को देखते हुए, यह APIs बनाने के लिए अच्छा match लगा। अगली चीज़ जो खोजनी थी वह Flask के लिए एक "Django REST Framework" था।
|
||||
|
||||
/// tip | **FastAPI** को इससे प्रेरणा मिली कि
|
||||
|
||||
एक micro-framework हो। आवश्यक tools और parts को mix and match करना आसान बनाया जाए।
|
||||
|
||||
एक simple और उपयोग में आसान routing system हो।
|
||||
|
||||
///
|
||||
|
||||
### [Requests](https://requests.readthedocs.io) { #requests }
|
||||
|
||||
**FastAPI** वास्तव में **Requests** का विकल्प नहीं है। उनका scope बहुत अलग है।
|
||||
|
||||
वास्तव में FastAPI application के *अंदर* Requests का उपयोग करना सामान्य बात होगी।
|
||||
|
||||
लेकिन फिर भी, FastAPI को Requests से काफी प्रेरणा मिली।
|
||||
|
||||
**Requests** APIs के साथ *interact* करने के लिए (client के रूप में) एक library है, जबकि **FastAPI** APIs *बनाने* के लिए (server के रूप में) एक library है।
|
||||
|
||||
वे कमोबेश विपरीत सिरों पर हैं, एक-दूसरे को पूरक करते हुए।
|
||||
|
||||
Requests का design बहुत simple और intuitive है, sensible defaults के साथ इसका उपयोग करना बहुत आसान है। लेकिन साथ ही, यह बहुत powerful और customizable है।
|
||||
|
||||
इसीलिए, जैसा कि official website में कहा गया है:
|
||||
|
||||
> Requests अब तक के सबसे अधिक downloaded Python packages में से एक है
|
||||
|
||||
आप इसे जिस तरह उपयोग करते हैं वह बहुत सरल है। उदाहरण के लिए, `GET` request करने के लिए, आप लिखेंगे:
|
||||
|
||||
```Python
|
||||
response = requests.get("http://example.com/some/url")
|
||||
```
|
||||
|
||||
FastAPI में इसके समकक्ष API *path operation* इस तरह दिख सकता है:
|
||||
|
||||
```Python hl_lines="1"
|
||||
@app.get("/some/url")
|
||||
def read_url():
|
||||
return {"message": "Hello World"}
|
||||
```
|
||||
|
||||
`requests.get(...)` और `@app.get(...)` में समानताएँ देखें।
|
||||
|
||||
/// tip | **FastAPI** को इससे प्रेरणा मिली कि
|
||||
|
||||
* एक simple और intuitive API हो।
|
||||
* HTTP method names (operations) को सीधे, straightforward और intuitive तरीके से उपयोग किया जाए।
|
||||
* sensible defaults हों, लेकिन powerful customizations भी हों।
|
||||
|
||||
///
|
||||
|
||||
### [Swagger](https://swagger.io/) / [OpenAPI](https://github.com/OAI/OpenAPI-Specification/) { #swagger-openapi }
|
||||
|
||||
Django REST Framework से जो मुख्य feature मैं चाहता था वह automatic API documentation था।
|
||||
|
||||
फिर मुझे पता चला कि APIs को document करने के लिए JSON (या YAML, JSON का एक extension) का उपयोग करने वाला एक standard था, जिसे Swagger कहा जाता था।
|
||||
|
||||
और Swagger APIs के लिए एक web user interface पहले से बनाया जा चुका था। इसलिए, किसी API के लिए Swagger documentation generate कर पाना इस web user interface का automatically उपयोग करने की अनुमति देता।
|
||||
|
||||
एक समय पर, Swagger को Linux Foundation को दे दिया गया, ताकि उसका नाम बदलकर OpenAPI रखा जा सके।
|
||||
|
||||
इसीलिए version 2.0 के बारे में बात करते समय "Swagger" कहना आम है, और version 3+ के लिए "OpenAPI"।
|
||||
|
||||
/// tip | **FastAPI** को इससे प्रेरणा मिली कि
|
||||
|
||||
Custom schema के बजाय API specifications के लिए एक open standard अपनाया और उपयोग किया जाए।
|
||||
|
||||
और standards-based user interface tools को integrate किया जाए:
|
||||
|
||||
* [Swagger UI](https://github.com/swagger-api/swagger-ui)
|
||||
* [ReDoc](https://github.com/Rebilly/ReDoc)
|
||||
|
||||
इन दोनों को इसलिए चुना गया क्योंकि ये काफी popular और stable थे, लेकिन एक quick search करने पर, आप OpenAPI के लिए दर्जनों alternative user interfaces पा सकते हैं (जिन्हें आप **FastAPI** के साथ उपयोग कर सकते हैं)।
|
||||
|
||||
///
|
||||
|
||||
### Flask REST frameworks { #flask-rest-frameworks }
|
||||
|
||||
कई Flask REST frameworks हैं, लेकिन उनकी जाँच में समय और काम लगाने के बाद, मैंने पाया कि कई discontinue या abandon हो चुके हैं, और उनमें कई unresolved issues हैं जिन्होंने उन्हें अनुपयुक्त बना दिया।
|
||||
|
||||
### [Marshmallow](https://marshmallow.readthedocs.io/en/stable/) { #marshmallow }
|
||||
|
||||
API systems द्वारा आवश्यक मुख्य features में से एक data "<dfn title="मार्शलिंग, रूपांतरण भी कहा जाता है">serialization</dfn>" है, जिसमें code (Python) से data लेकर उसे ऐसी चीज़ में बदला जाता है जिसे network के माध्यम से भेजा जा सके। उदाहरण के लिए, database से data रखने वाले object को JSON object में बदलना। `datetime` objects को strings में बदलना, आदि।
|
||||
|
||||
APIs द्वारा आवश्यक एक और बड़ा feature data validation है, यह सुनिश्चित करना कि data निश्चित parameters के अनुसार valid है। उदाहरण के लिए, कोई field `int` है, कोई random string नहीं। यह incoming data के लिए विशेष रूप से उपयोगी है।
|
||||
|
||||
Data validation system के बिना, आपको सभी checks हाथ से, code में करने पड़ते।
|
||||
|
||||
ये features वही हैं जिन्हें प्रदान करने के लिए Marshmallow बनाया गया था। यह एक बेहतरीन library है, और मैंने पहले इसका बहुत उपयोग किया है।
|
||||
|
||||
लेकिन इसे Python type hints के अस्तित्व में आने से पहले बनाया गया था। इसलिए, हर <dfn title="डेटा कैसे बना होना चाहिए इसकी परिभाषा">schema</dfn> को define करने के लिए आपको Marshmallow द्वारा प्रदान किए गए specific utils और classes का उपयोग करना पड़ता है।
|
||||
|
||||
/// tip | **FastAPI** को इससे प्रेरणा मिली कि
|
||||
|
||||
"schemas" को define करने के लिए code का उपयोग किया जाए जो data types और validation, automatically प्रदान करे।
|
||||
|
||||
///
|
||||
|
||||
### [Webargs](https://webargs.readthedocs.io/en/latest/) { #webargs }
|
||||
|
||||
APIs द्वारा आवश्यक एक और बड़ा feature incoming requests से data <dfn title="Python डेटा में पढ़ना और बदलना">parsing</dfn> करना है।
|
||||
|
||||
Webargs एक tool है जिसे Flask सहित कई frameworks के ऊपर यह प्रदान करने के लिए बनाया गया था।
|
||||
|
||||
यह data validation करने के लिए नीचे Marshmallow का उपयोग करता है। और इसे उन्हीं developers ने बनाया था।
|
||||
|
||||
यह एक बेहतरीन tool है और **FastAPI** से पहले मैंने इसका भी बहुत उपयोग किया था।
|
||||
|
||||
/// note | नोट
|
||||
|
||||
Webargs को उन्हीं Marshmallow developers ने बनाया था।
|
||||
|
||||
///
|
||||
|
||||
/// tip | **FastAPI** को इससे प्रेरणा मिली कि
|
||||
|
||||
Incoming request data का automatic validation हो।
|
||||
|
||||
///
|
||||
|
||||
### [APISpec](https://apispec.readthedocs.io/en/stable/) { #apispec }
|
||||
|
||||
Marshmallow और Webargs plug-ins के रूप में validation, parsing और serialization प्रदान करते हैं।
|
||||
|
||||
लेकिन documentation अभी भी missing है। फिर APISpec बनाया गया।
|
||||
|
||||
यह कई frameworks के लिए एक plug-in है (और Starlette के लिए भी एक plug-in है)।
|
||||
|
||||
यह जिस तरह काम करता है वह यह है कि आप route handle करने वाली प्रत्येक function की docstring के अंदर YAML format का उपयोग करके schema की definition लिखते हैं।
|
||||
|
||||
और यह OpenAPI schemas generate करता है।
|
||||
|
||||
Flask, Starlette, Responder, आदि में यह इसी तरह काम करता है।
|
||||
|
||||
लेकिन फिर, हमारे पास फिर से Python string (एक बड़ा YAML) के अंदर micro-syntax होने की समस्या है।
|
||||
|
||||
Editor इसमें ज्यादा मदद नहीं कर सकता। और अगर हम parameters या Marshmallow schemas modify करते हैं और उस YAML docstring को भी modify करना भूल जाते हैं, तो generated schema obsolete हो जाएगा।
|
||||
|
||||
/// note | नोट
|
||||
|
||||
APISpec को उन्हीं Marshmallow developers ने बनाया था।
|
||||
|
||||
///
|
||||
|
||||
/// tip | **FastAPI** को इससे प्रेरणा मिली कि
|
||||
|
||||
APIs के लिए open standard, OpenAPI को support किया जाए।
|
||||
|
||||
///
|
||||
|
||||
### [Flask-apispec](https://flask-apispec.readthedocs.io/en/latest/) { #flask-apispec }
|
||||
|
||||
यह एक Flask plug-in है, जो Webargs, Marshmallow और APISpec को एक साथ जोड़ता है।
|
||||
|
||||
यह APISpec का उपयोग करके OpenAPI schemas automatically generate करने के लिए Webargs और Marshmallow की जानकारी का उपयोग करता है।
|
||||
|
||||
यह एक बेहतरीन tool है, बहुत underrated। इसे वहाँ मौजूद कई Flask plug-ins से कहीं अधिक popular होना चाहिए। यह शायद इसकी documentation के बहुत concise और abstract होने के कारण हो सकता है।
|
||||
|
||||
इसने Python docstrings के अंदर YAML (एक और syntax) लिखने की आवश्यकता को हल कर दिया।
|
||||
|
||||
Flask, Flask-apispec को Marshmallow और Webargs के साथ मिलाकर यह combination **FastAPI** बनाने तक मेरा पसंदीदा backend stack था।
|
||||
|
||||
इसका उपयोग करने से कई Flask full-stack generators बने। ये वे main stacks हैं जिन्हें मैं (और कई external teams) अब तक उपयोग कर रहे हैं:
|
||||
|
||||
* [https://github.com/tiangolo/full-stack](https://github.com/tiangolo/full-stack)
|
||||
* [https://github.com/tiangolo/full-stack-flask-couchbase](https://github.com/tiangolo/full-stack-flask-couchbase)
|
||||
* [https://github.com/tiangolo/full-stack-flask-couchdb](https://github.com/tiangolo/full-stack-flask-couchdb)
|
||||
|
||||
और यही full-stack generators [**FastAPI** Project Generators](project-generation.md) का base थे।
|
||||
|
||||
/// note | नोट
|
||||
|
||||
Flask-apispec को उन्हीं Marshmallow developers ने बनाया था।
|
||||
|
||||
///
|
||||
|
||||
/// tip | **FastAPI** को इससे प्रेरणा मिली कि
|
||||
|
||||
OpenAPI schema को automatically generate किया जाए, उसी code से जो serialization और validation define करता है।
|
||||
|
||||
///
|
||||
|
||||
### [NestJS](https://nestjs.com/) (और [Angular](https://angular.io/)) { #nestjs-and-angular }
|
||||
|
||||
यह Python भी नहीं है, NestJS Angular से प्रेरित एक JavaScript (TypeScript) NodeJS framework है।
|
||||
|
||||
यह कुछ ऐसा हासिल करता है जो Flask-apispec के साथ किए जा सकने वाले काम जैसा है।
|
||||
|
||||
इसमें Angular 2 से प्रेरित एक integrated dependency injection system है। इसमें "injectables" को pre-register करना आवश्यक है (जैसे मुझे ज्ञात सभी अन्य dependency injection systems में), इसलिए, यह verbosity और code repetition बढ़ाता है।
|
||||
|
||||
क्योंकि parameters को TypeScript types (Python type hints के समान) के साथ describe किया जाता है, editor support काफी अच्छा है।
|
||||
|
||||
लेकिन क्योंकि TypeScript data compilation के बाद JavaScript में preserve नहीं रहता, यह validation, serialization और documentation को एक ही समय पर define करने के लिए types पर निर्भर नहीं हो सकता। इसके कारण और कुछ design decisions के कारण, validation, serialization और automatic schema generation पाने के लिए, कई जगह decorators जोड़ने की आवश्यकता होती है। इसलिए, यह काफी verbose हो जाता है।
|
||||
|
||||
यह nested models को बहुत अच्छी तरह handle नहीं कर सकता। इसलिए, अगर request में JSON body एक JSON object है जिसमें inner fields हैं जो खुद nested JSON objects हैं, तो इसे ठीक से document और validate नहीं किया जा सकता।
|
||||
|
||||
/// tip | **FastAPI** को इससे प्रेरणा मिली कि
|
||||
|
||||
बेहतरीन editor support के लिए Python types का उपयोग किया जाए।
|
||||
|
||||
एक powerful dependency injection system हो। Code repetition को minimize करने का तरीका खोजा जाए।
|
||||
|
||||
///
|
||||
|
||||
### [Sanic](https://sanic.readthedocs.io/en/latest/) { #sanic }
|
||||
|
||||
यह `asyncio` पर आधारित पहले बेहद तेज़ Python frameworks में से एक था। इसे Flask के बहुत समान बनाया गया था।
|
||||
|
||||
/// note | तकनीकी विवरण
|
||||
|
||||
इसने default Python `asyncio` loop के बजाय [`uvloop`](https://github.com/MagicStack/uvloop) का उपयोग किया। यही इसे इतना तेज़ बनाता था।
|
||||
|
||||
इसने स्पष्ट रूप से Uvicorn और Starlette को प्रेरित किया, जो वर्तमान में open benchmarks में Sanic से तेज़ हैं।
|
||||
|
||||
///
|
||||
|
||||
/// tip | **FastAPI** को इससे प्रेरणा मिली कि
|
||||
|
||||
बेहद तेज़ performance हासिल करने का तरीका खोजा जाए।
|
||||
|
||||
इसीलिए **FastAPI** Starlette पर आधारित है, क्योंकि यह उपलब्ध सबसे तेज़ framework है (third-party benchmarks द्वारा tested)।
|
||||
|
||||
///
|
||||
|
||||
### [Falcon](https://falconframework.org/) { #falcon }
|
||||
|
||||
Falcon एक और high performance Python framework है, इसे minimal होने और Hug जैसे अन्य frameworks की foundation के रूप में काम करने के लिए design किया गया है।
|
||||
|
||||
इसे ऐसी functions रखने के लिए design किया गया है जो दो parameters receive करती हैं, एक "request" और एक "response"। फिर आप request से parts "read" करते हैं, और response में parts "write" करते हैं। इस design के कारण, standard Python type hints के साथ function parameters के रूप में request parameters और bodies declare करना संभव नहीं है।
|
||||
|
||||
इसलिए, data validation, serialization, और documentation को code में करना पड़ता है, automatically नहीं। या उन्हें Falcon के ऊपर एक framework के रूप में implement करना पड़ता है, जैसे Hug। यही अंतर उन अन्य frameworks में भी होता है जो Falcon के design से प्रेरित हैं, जहाँ parameters के रूप में एक request object और एक response object होता है।
|
||||
|
||||
/// tip | **FastAPI** को इससे प्रेरणा मिली कि
|
||||
|
||||
बेहतरीन performance पाने के तरीके खोजे जाएँ।
|
||||
|
||||
Hug के साथ (क्योंकि Hug Falcon पर आधारित है) इसने **FastAPI** को functions में `response` parameter declare करने के लिए प्रेरित किया।
|
||||
|
||||
हालांकि FastAPI में यह optional है, और मुख्य रूप से headers, cookies, और alternative status codes set करने के लिए उपयोग किया जाता है।
|
||||
|
||||
///
|
||||
|
||||
### [Molten](https://moltenframework.com/) { #molten }
|
||||
|
||||
मैंने **FastAPI** बनाने के शुरुआती चरणों में Molten खोजा। और इसमें काफी समान विचार हैं:
|
||||
|
||||
* Python type hints पर आधारित।
|
||||
* इन types से validation और documentation।
|
||||
* Dependency Injection system।
|
||||
|
||||
यह Pydantic जैसी data validation, serialization और documentation third-party library का उपयोग नहीं करता, इसकी अपनी library है। इसलिए, ये data type definitions उतनी आसानी से reusable नहीं होंगी।
|
||||
|
||||
इसे थोड़ी अधिक verbose configurations की आवश्यकता होती है। और क्योंकि यह WSGI (ASGI के बजाय) पर आधारित है, इसे Uvicorn, Starlette और Sanic जैसे tools द्वारा प्रदान किए गए high performance का लाभ उठाने के लिए design नहीं किया गया है।
|
||||
|
||||
Dependency injection system को dependencies की pre-registration की आवश्यकता होती है और dependencies declared types के आधार पर solve की जाती हैं। इसलिए, किसी निश्चित type को provide करने वाले एक से अधिक "component" declare करना संभव नहीं है।
|
||||
|
||||
Routes एक ही जगह declare किए जाते हैं, दूसरी जगहों पर declared functions का उपयोग करके (decorators का उपयोग करने के बजाय जिन्हें endpoint handle करने वाली function के ठीक ऊपर रखा जा सकता है)। यह Flask (और Starlette) के तरीके की तुलना में Django के तरीके के अधिक करीब है। यह code में उन चीज़ों को अलग करता है जो अपेक्षाकृत tightly coupled हैं।
|
||||
|
||||
/// tip | **FastAPI** को इससे प्रेरणा मिली कि
|
||||
|
||||
Model attributes के "default" value का उपयोग करके data types के लिए extra validations define किए जाएँ। यह editor support को बेहतर बनाता है, और यह पहले Pydantic में उपलब्ध नहीं था।
|
||||
|
||||
इसने वास्तव में Pydantic के parts को update करने के लिए प्रेरित किया, ताकि वही validation declaration style support किया जा सके (यह सारी functionality अब Pydantic में पहले से उपलब्ध है)।
|
||||
|
||||
///
|
||||
|
||||
### [Hug](https://github.com/hugapi/hug) { #hug }
|
||||
|
||||
Hug उन पहले frameworks में से एक था जिसने Python type hints का उपयोग करके API parameter types की declaration implement की। यह एक बेहतरीन विचार था जिसने अन्य tools को भी ऐसा ही करने के लिए प्रेरित किया।
|
||||
|
||||
इसने अपनी declarations में standard Python types के बजाय custom types का उपयोग किया, लेकिन फिर भी यह एक बहुत बड़ा कदम आगे था।
|
||||
|
||||
यह पूरे API को JSON में declare करने वाला custom schema generate करने वाले पहले frameworks में से भी एक था।
|
||||
|
||||
यह OpenAPI और JSON Schema जैसे standard पर आधारित नहीं था। इसलिए इसे Swagger UI जैसे अन्य tools के साथ integrate करना straightforward नहीं होता। लेकिन फिर भी, यह एक बहुत innovative idea था।
|
||||
|
||||
इसमें एक दिलचस्प, uncommon feature है: उसी framework का उपयोग करके APIs और CLIs भी बनाना संभव है।
|
||||
|
||||
क्योंकि यह synchronous Python web frameworks (WSGI) के पिछले standard पर आधारित है, यह Websockets और अन्य चीज़ों को handle नहीं कर सकता, हालांकि इसका performance भी high है।
|
||||
|
||||
/// note | नोट
|
||||
|
||||
Hug को Timothy Crosley ने बनाया था, वही [`isort`](https://github.com/timothycrosley/isort) के creator हैं, जो Python files में imports को automatically sort करने के लिए एक बेहतरीन tool है।
|
||||
|
||||
///
|
||||
|
||||
/// tip | **FastAPI** को प्रेरित करने वाले विचार
|
||||
|
||||
Hug ने APIStar के parts को प्रेरित किया, और APIStar के साथ-साथ यह उन tools में से एक था जो मुझे सबसे promising लगे।
|
||||
|
||||
Hug ने **FastAPI** को parameters declare करने के लिए Python type hints का उपयोग करने, और API को automatically define करने वाला schema generate करने के लिए प्रेरित किया।
|
||||
|
||||
Hug ने **FastAPI** को headers और cookies set करने के लिए functions में `response` parameter declare करने के लिए प्रेरित किया।
|
||||
|
||||
///
|
||||
|
||||
### [APIStar](https://github.com/encode/apistar) (<= 0.5) { #apistar-0-5 }
|
||||
|
||||
**FastAPI** बनाने का निर्णय लेने से ठीक पहले मुझे **APIStar** server मिला। इसमें लगभग वह सब कुछ था जिसकी मुझे तलाश थी और इसका design बेहतरीन था।
|
||||
|
||||
यह उन पहले implementations में से एक था जो मैंने कभी देखे, जिसमें parameters और requests declare करने के लिए Python type hints का उपयोग करने वाला framework था (NestJS और Molten से पहले)। मुझे यह Hug के लगभग उसी समय मिला। लेकिन APIStar ने OpenAPI standard का उपयोग किया।
|
||||
|
||||
इसमें कई जगहों पर उन्हीं type hints के आधार पर automatic data validation, data serialization और OpenAPI schema generation था।
|
||||
|
||||
Body schema definitions Pydantic जैसे Python type hints का उपयोग नहीं करती थीं, यह Marshmallow के थोड़ा अधिक समान था, इसलिए editor support उतना अच्छा नहीं होता, लेकिन फिर भी, APIStar उपलब्ध सबसे अच्छा विकल्प था।
|
||||
|
||||
उस समय इसके performance benchmarks सबसे अच्छे थे (सिर्फ Starlette ने surpass किया था)।
|
||||
|
||||
शुरुआत में, इसमें automatic API documentation web UI नहीं था, लेकिन मुझे पता था कि मैं इसमें Swagger UI जोड़ सकता हूँ।
|
||||
|
||||
इसमें dependency injection system था। ऊपर चर्चा किए गए अन्य tools की तरह, इसमें components की pre-registration आवश्यक थी। लेकिन फिर भी, यह एक बेहतरीन feature था।
|
||||
|
||||
मैं कभी भी इसे full project में उपयोग नहीं कर पाया, क्योंकि इसमें security integration नहीं था, इसलिए मैं Flask-apispec पर आधारित full-stack generators के साथ मौजूद सभी features को replace नहीं कर सका। मेरे projects backlog में उस functionality को जोड़ने वाला pull request बनाने का विचार था।
|
||||
|
||||
लेकिन फिर, project का focus shift हो गया।
|
||||
|
||||
यह अब API web framework नहीं रहा, क्योंकि creator को Starlette पर focus करना था।
|
||||
|
||||
अब APIStar OpenAPI specifications validate करने के लिए tools का एक set है, web framework नहीं।
|
||||
|
||||
/// note | नोट
|
||||
|
||||
APIStar को Tom Christie ने बनाया था। वही व्यक्ति जिन्होंने बनाया:
|
||||
|
||||
* Django REST Framework
|
||||
* Starlette (जिस पर **FastAPI** आधारित है)
|
||||
* Uvicorn (Starlette और **FastAPI** द्वारा उपयोग किया जाता है)
|
||||
|
||||
///
|
||||
|
||||
/// tip | **FastAPI** को इससे प्रेरणा मिली कि
|
||||
|
||||
अस्तित्व में आए।
|
||||
|
||||
एक ही Python types के साथ कई चीज़ें (data validation, serialization और documentation) declare करने का विचार, जो साथ ही बेहतरीन editor support भी देता था, मुझे एक शानदार विचार लगा।
|
||||
|
||||
और लंबे समय तक समान framework की खोज करने और कई अलग-अलग alternatives को test करने के बाद, APIStar उपलब्ध सबसे अच्छा विकल्प था।
|
||||
|
||||
फिर APIStar ने server के रूप में अस्तित्व में रहना बंद कर दिया और Starlette बनाया गया, और यह ऐसे system के लिए एक नई बेहतर foundation था। यही **FastAPI** बनाने की अंतिम प्रेरणा थी।
|
||||
|
||||
मैं **FastAPI** को APIStar का "spiritual successor" मानता हूँ, जो इन सभी पिछले tools से मिली सीख के आधार पर features, typing system, और अन्य parts को improve और increase करता है।
|
||||
|
||||
///
|
||||
|
||||
## **FastAPI** द्वारा उपयोग किया गया { #used-by-fastapi }
|
||||
|
||||
### [Pydantic](https://docs.pydantic.dev/) { #pydantic }
|
||||
|
||||
Pydantic Python type hints के आधार पर data validation, serialization और documentation (JSON Schema का उपयोग करके) define करने के लिए एक library है।
|
||||
|
||||
यह इसे बेहद intuitive बनाता है।
|
||||
|
||||
यह Marshmallow से comparable है। हालांकि benchmarks में यह Marshmallow से तेज़ है। और क्योंकि यह उन्हीं Python type hints पर आधारित है, editor support बेहतरीन है।
|
||||
|
||||
/// tip | **FastAPI** इसे इन कामों के लिए उपयोग करता है
|
||||
|
||||
सभी data validation, data serialization और automatic model documentation (JSON Schema पर आधारित) handle करना।
|
||||
|
||||
फिर **FastAPI** उस JSON Schema data को लेता है और उसे OpenAPI में डालता है, उन सभी अन्य चीज़ों के अलावा जो यह करता है।
|
||||
|
||||
///
|
||||
|
||||
### [Starlette](https://www.starlette.dev/) { #starlette }
|
||||
|
||||
Starlette एक lightweight <dfn title="असिंक्रोनस Python web applications बनाने के लिए नया मानक">ASGI</dfn> framework/toolkit है, जो high-performance asyncio services बनाने के लिए ideal है।
|
||||
|
||||
यह बहुत simple और intuitive है। इसे आसानी से extensible होने और modular components रखने के लिए design किया गया है।
|
||||
|
||||
इसमें है:
|
||||
|
||||
* Seriously impressive performance.
|
||||
* WebSocket support.
|
||||
* In-process background tasks.
|
||||
* Startup और shutdown events.
|
||||
* HTTPX पर built test client.
|
||||
* CORS, GZip, Static Files, Streaming responses.
|
||||
* Session और Cookie support.
|
||||
* 100% test coverage.
|
||||
* 100% type annotated codebase.
|
||||
* Few hard dependencies.
|
||||
|
||||
Starlette वर्तमान में tested सबसे तेज़ Python framework है। केवल Uvicorn ने इसे surpass किया है, जो framework नहीं, बल्कि server है।
|
||||
|
||||
Starlette सभी basic web microframework functionality प्रदान करता है।
|
||||
|
||||
लेकिन यह automatic data validation, serialization या documentation प्रदान नहीं करता।
|
||||
|
||||
यह उन मुख्य चीज़ों में से एक है जो **FastAPI** ऊपर से जोड़ता है, सब Python type hints (Pydantic का उपयोग करके) पर आधारित। इसके साथ dependency injection system, security utilities, OpenAPI schema generation, आदि।
|
||||
|
||||
/// note | तकनीकी विवरण
|
||||
|
||||
ASGI एक नया "standard" है जिसे Django core team members द्वारा develop किया जा रहा है। यह अभी भी "Python standard" (एक PEP) नहीं है, हालांकि वे ऐसा करने की प्रक्रिया में हैं।
|
||||
|
||||
फिर भी, इसे पहले से ही कई tools द्वारा "standard" के रूप में उपयोग किया जा रहा है। यह interoperability को बहुत बेहतर बनाता है, क्योंकि आप Uvicorn को किसी अन्य ASGI server (जैसे Daphne या Hypercorn) से switch कर सकते हैं, या आप ASGI compatible tools, जैसे `python-socketio`, जोड़ सकते हैं।
|
||||
|
||||
///
|
||||
|
||||
/// tip | **FastAPI** इसे इन कामों के लिए उपयोग करता है
|
||||
|
||||
सभी core web parts को handle करना। ऊपर से features जोड़ना।
|
||||
|
||||
Class `FastAPI` खुद सीधे class `Starlette` से inherit करती है।
|
||||
|
||||
इसलिए, जो कुछ भी आप Starlette के साथ कर सकते हैं, उसे आप सीधे **FastAPI** के साथ कर सकते हैं, क्योंकि यह मूल रूप से steroids पर Starlette है।
|
||||
|
||||
///
|
||||
|
||||
### [Uvicorn](https://www.uvicorn.dev/) { #uvicorn }
|
||||
|
||||
Uvicorn एक lightning-fast ASGI server है, जो uvloop और httptools पर बना है।
|
||||
|
||||
यह web framework नहीं, बल्कि server है। उदाहरण के लिए, यह paths द्वारा routing के लिए tools प्रदान नहीं करता। यह ऐसी चीज़ है जो Starlette (या **FastAPI**) जैसा framework ऊपर से प्रदान करेगा।
|
||||
|
||||
यह Starlette और **FastAPI** के लिए recommended server है।
|
||||
|
||||
/// tip | **FastAPI** इसे इस रूप में अनुशंसित करता है
|
||||
|
||||
**FastAPI** applications चलाने के लिए main web server।
|
||||
|
||||
आप asynchronous multi-process server पाने के लिए `--workers` command line option का भी उपयोग कर सकते हैं।
|
||||
|
||||
अधिक विवरण [Deployment](deployment/index.md) section में देखें।
|
||||
|
||||
///
|
||||
|
||||
## Benchmarks और speed { #benchmarks-and-speed }
|
||||
|
||||
Uvicorn, Starlette और FastAPI के बीच समझने, तुलना करने, और अंतर देखने के लिए, [Benchmarks](benchmarks.md) के बारे में section देखें।
|
||||
444
docs/hi/docs/async.md
Normal file
444
docs/hi/docs/async.md
Normal file
@@ -0,0 +1,444 @@
|
||||
# Concurrency और async / await { #concurrency-and-async-await }
|
||||
|
||||
*path operation functions* के लिए `async def` syntax के बारे में विवरण और asynchronous code, concurrency, और parallelism के बारे में कुछ पृष्ठभूमि।
|
||||
|
||||
## जल्दी में हैं? { #in-a-hurry }
|
||||
|
||||
<abbr title="too long; didn't read - बहुत लंबा; नहीं पढ़ा"><strong>TL;DR:</strong></abbr>
|
||||
|
||||
अगर आप third party libraries का उपयोग कर रहे हैं जो आपको उन्हें `await` के साथ call करने को कहती हैं, जैसे:
|
||||
|
||||
```Python
|
||||
results = await some_library()
|
||||
```
|
||||
|
||||
तो, अपने *path operation functions* को `async def` के साथ declare करें, जैसे:
|
||||
|
||||
```Python hl_lines="2"
|
||||
@app.get('/')
|
||||
async def read_results():
|
||||
results = await some_library()
|
||||
return results
|
||||
```
|
||||
|
||||
/// note | नोट
|
||||
|
||||
आप `await` का उपयोग केवल `async def` के साथ बनाए गए functions के अंदर ही कर सकते हैं।
|
||||
|
||||
///
|
||||
|
||||
---
|
||||
|
||||
अगर आप ऐसी third party library का उपयोग कर रहे हैं जो किसी चीज़ (database, API, file system, आदि) से communicate करती है और `await` का उपयोग करने का support नहीं रखती, (वर्तमान में अधिकांश database libraries के साथ यही स्थिति है), तो अपने *path operation functions* को सामान्य रूप से, केवल `def` के साथ declare करें, जैसे:
|
||||
|
||||
```Python hl_lines="2"
|
||||
@app.get('/')
|
||||
def results():
|
||||
results = some_library()
|
||||
return results
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
अगर आपके application को (किसी तरह) किसी और चीज़ से communicate करने और उसके response का इंतज़ार करने की ज़रूरत नहीं है, तो `async def` का उपयोग करें, भले ही आपको अंदर `await` का उपयोग करने की ज़रूरत न हो।
|
||||
|
||||
---
|
||||
|
||||
अगर आपको पता नहीं है, तो सामान्य `def` का उपयोग करें।
|
||||
|
||||
---
|
||||
|
||||
**नोट**: आप अपनी ज़रूरत के अनुसार अपने *path operation functions* में `def` और `async def` को mix कर सकते हैं और हर एक को अपने लिए सबसे अच्छे option का उपयोग करके define कर सकते हैं। FastAPI उनके साथ सही काम करेगा।
|
||||
|
||||
किसी भी स्थिति में, ऊपर दिए गए किसी भी case में, FastAPI फिर भी asynchronously काम करेगा और बेहद तेज़ होगा।
|
||||
|
||||
लेकिन ऊपर दिए गए steps को follow करने से, यह कुछ performance optimizations कर पाएगा।
|
||||
|
||||
## तकनीकी विवरण { #technical-details }
|
||||
|
||||
Python के आधुनिक versions **"asynchronous code"** का support करते हैं, जो **"coroutines"** नाम की चीज़ का उपयोग करता है, **`async` और `await`** syntax के साथ।
|
||||
|
||||
आइए नीचे के sections में इस phrase को हिस्सों में देखते हैं:
|
||||
|
||||
* **Asynchronous Code**
|
||||
* **`async` और `await`**
|
||||
* **Coroutines**
|
||||
|
||||
## Asynchronous Code { #asynchronous-code }
|
||||
|
||||
Asynchronous code का मतलब बस यह है कि language 💬 के पास computer / program 🤖 को यह बताने का एक तरीका होता है कि code में किसी point पर, उसे 🤖 कहीं और *किसी और चीज़* के finish होने का इंतज़ार करना होगा। मान लें कि उस *किसी और चीज़* को "slow-file" 📝 कहा जाता है।
|
||||
|
||||
तो, उस समय के दौरान, computer कोई और काम कर सकता है, जबकि "slow-file" 📝 finish हो रही होती है।
|
||||
|
||||
फिर computer / program 🤖 हर बार वापस आएगा जब उसे मौका मिलेगा क्योंकि वह फिर से इंतज़ार कर रहा होगा, या जब भी वह 🤖 उस point पर अपना सारा काम finish कर लेगा। और वह 🤖 देखेगा कि जिन tasks का वह इंतज़ार कर रहा था, उनमें से कोई पहले ही finish हो चुका है या नहीं, फिर वह जो भी करना था करेगा।
|
||||
|
||||
इसके बाद, वह 🤖 finish होने वाला पहला task लेता है (मान लें, हमारी "slow-file" 📝) और उसके साथ जो भी करना था उसे जारी रखता है।
|
||||
|
||||
वह "किसी और चीज़ का इंतज़ार" आम तौर पर <abbr title="Input and Output - इनपुट और आउटपुट">I/O</abbr> operations को refer करता है जो अपेक्षाकृत "slow" होते हैं (processor और RAM memory की speed की तुलना में), जैसे इंतज़ार करना:
|
||||
|
||||
* client से data network के माध्यम से भेजे जाने का
|
||||
* आपके program द्वारा भेजा गया data client द्वारा network के माध्यम से receive किए जाने का
|
||||
* disk पर किसी file की contents system द्वारा पढ़े जाने और आपके program को दिए जाने का
|
||||
* आपके program द्वारा system को दी गई contents disk पर लिखे जाने का
|
||||
* किसी remote API operation का
|
||||
* किसी database operation के finish होने का
|
||||
* किसी database query द्वारा results return किए जाने का
|
||||
* आदि।
|
||||
|
||||
क्योंकि execution time ज़्यादातर <abbr title="Input and Output - इनपुट और आउटपुट">I/O</abbr> operations का इंतज़ार करने में consume होता है, उन्हें "I/O bound" operations कहा जाता है।
|
||||
|
||||
इसे "asynchronous" इसलिए कहा जाता है क्योंकि computer / program को slow task के साथ "synchronized" होने की ज़रूरत नहीं होती, task के finish होने के exact moment का इंतज़ार करते हुए, कुछ भी न करते हुए, ताकि वह task result ले सके और काम जारी रख सके।
|
||||
|
||||
इसके बजाय, "asynchronous" system होने के कारण, task finish होने के बाद, computer / program के वापस आने तक थोड़ा सा line में wait कर सकता है (कुछ microseconds), ताकि computer / program जो काम करने गया था उसे finish करे, और फिर वापस आकर results ले और उनके साथ काम जारी रखे।
|
||||
|
||||
"Synchronous" ("asynchronous" के विपरीत) के लिए वे आमतौर पर "sequential" term भी use करते हैं, क्योंकि computer / program किसी अलग task पर switch करने से पहले sequence में सभी steps follow करता है, भले ही उन steps में इंतज़ार शामिल हो।
|
||||
|
||||
### Concurrency और Burgers { #concurrency-and-burgers }
|
||||
|
||||
ऊपर describe किए गए **asynchronous** code के इस idea को कभी-कभी **"concurrency"** भी कहा जाता है। यह **"parallelism"** से अलग है।
|
||||
|
||||
**Concurrency** और **parallelism** दोनों "अलग-अलग चीज़ें लगभग एक ही समय पर हो रही हैं" से related हैं।
|
||||
|
||||
लेकिन *concurrency* और *parallelism* के बीच के details काफी अलग हैं।
|
||||
|
||||
अंतर देखने के लिए, burgers के बारे में निम्न story imagine करें:
|
||||
|
||||
### Concurrent Burgers { #concurrent-burgers }
|
||||
|
||||
आप अपनी crush के साथ fast food लेने जाते हैं, आप line में खड़े होते हैं जबकि cashier आपके आगे के लोगों से orders ले रहा होता है। 😍
|
||||
|
||||
<img src="/img/async/concurrent-burgers/concurrent-burgers-01.png" class="illustration">
|
||||
|
||||
फिर आपकी बारी आती है, आप अपनी crush और अपने लिए 2 बहुत fancy burgers का order देते हैं। 🍔🍔
|
||||
|
||||
<img src="/img/async/concurrent-burgers/concurrent-burgers-02.png" class="illustration">
|
||||
|
||||
Cashier kitchen में cook से कुछ कहता है ताकि उन्हें पता हो कि उन्हें आपके burgers prepare करने हैं (भले ही वे currently previous clients के burgers prepare कर रहे हों)।
|
||||
|
||||
<img src="/img/async/concurrent-burgers/concurrent-burgers-03.png" class="illustration">
|
||||
|
||||
आप pay करते हैं। 💸
|
||||
|
||||
Cashier आपको आपकी turn का number देता है।
|
||||
|
||||
<img src="/img/async/concurrent-burgers/concurrent-burgers-04.png" class="illustration">
|
||||
|
||||
जब आप wait कर रहे होते हैं, आप अपनी crush के साथ एक table चुनते हैं, बैठते हैं और अपनी crush से लंबे समय तक बात करते हैं (क्योंकि आपके burgers बहुत fancy हैं और prepare होने में कुछ समय लेते हैं)।
|
||||
|
||||
जब आप अपनी crush के साथ table पर बैठे होते हैं, burgers का इंतज़ार करते हुए, आप उस समय को यह admire करने में spend कर सकते हैं कि आपकी crush कितनी awesome, cute और smart है ✨😍✨।
|
||||
|
||||
<img src="/img/async/concurrent-burgers/concurrent-burgers-05.png" class="illustration">
|
||||
|
||||
Wait करते हुए और अपनी crush से बात करते हुए, time to time, आप counter पर displayed number check करते हैं कि क्या आपकी turn आ गई है।
|
||||
|
||||
फिर किसी point पर, आखिरकार आपकी turn आ जाती है। आप counter पर जाते हैं, अपने burgers लेते हैं और table पर वापस आते हैं।
|
||||
|
||||
<img src="/img/async/concurrent-burgers/concurrent-burgers-06.png" class="illustration">
|
||||
|
||||
आप और आपकी crush burgers खाते हैं और अच्छा समय बिताते हैं। ✨
|
||||
|
||||
<img src="/img/async/concurrent-burgers/concurrent-burgers-07.png" class="illustration">
|
||||
|
||||
/// note | नोट
|
||||
|
||||
सुंदर illustrations [Ketrina Thompson](https://www.instagram.com/ketrinadrawsalot) द्वारा। 🎨
|
||||
|
||||
///
|
||||
|
||||
---
|
||||
|
||||
कल्पना करें कि उस story में आप computer / program 🤖 हैं।
|
||||
|
||||
जब आप line में होते हैं, आप बस idle 😴 होते हैं, अपनी turn का इंतज़ार करते हुए, कुछ बहुत "productive" नहीं कर रहे होते। लेकिन line fast है क्योंकि cashier केवल orders ले रहा है (उन्हें prepare नहीं कर रहा), इसलिए यह ठीक है।
|
||||
|
||||
फिर, जब आपकी turn आती है, आप actual "productive" work करते हैं, menu process करते हैं, decide करते हैं कि आपको क्या चाहिए, अपनी crush की choice लेते हैं, pay करते हैं, check करते हैं कि आप correct bill या card दे रहे हैं, check करते हैं कि आपसे सही charge किया गया है, check करते हैं कि order में correct items हैं, आदि।
|
||||
|
||||
लेकिन फिर, भले ही आपके पास अभी burgers नहीं हैं, cashier के साथ आपका work "on pause" ⏸ है, क्योंकि आपको अपने burgers ready होने का wait 🕙 करना है।
|
||||
|
||||
लेकिन जब आप counter से दूर जाते हैं और अपनी turn के number के साथ table पर बैठते हैं, आप अपना attention अपनी crush पर switch 🔀 कर सकते हैं, और उस पर "work" ⏯ 🤓 कर सकते हैं। फिर आप दोबारा कुछ बहुत "productive" कर रहे होते हैं, जैसे अपनी crush 😍 के साथ flirting।
|
||||
|
||||
फिर cashier 💁 counter के display पर आपका number डालकर कहता है "मैंने burgers बना लिए हैं", लेकिन displayed number आपकी turn number में बदलते ही आप तुरंत पागलों की तरह jump नहीं करते। आपको पता है कि कोई आपके burgers नहीं चुराएगा क्योंकि आपके पास आपकी turn का number है, और उनके पास उनका।
|
||||
|
||||
तो आप अपनी crush के story finish करने का इंतज़ार करते हैं (current work ⏯ / process हो रहा task 🤓 finish होना), gentle smile करते हैं और कहते हैं कि आप burgers लेने जा रहे हैं ⏸।
|
||||
|
||||
फिर आप counter 🔀 पर जाते हैं, उस initial task पर जो अब finish हो चुका है ⏯, burgers उठाते हैं, thanks कहते हैं और उन्हें table पर ले जाते हैं। इससे counter के साथ interaction का वह step / task finish हो जाता है ⏹। यह बदले में, "eating burgers" 🔀 ⏯ का एक नया task create करता है, लेकिन "getting burgers" वाला previous task finish हो चुका है ⏹।
|
||||
|
||||
### Parallel Burgers { #parallel-burgers }
|
||||
|
||||
अब imagine करें कि ये "Concurrent Burgers" नहीं, बल्कि "Parallel Burgers" हैं।
|
||||
|
||||
आप अपनी crush के साथ parallel fast food लेने जाते हैं।
|
||||
|
||||
आप line में खड़े होते हैं जबकि कई (मान लें 8) cashiers, जो उसी समय cooks भी हैं, आपके आगे के लोगों से orders ले रहे होते हैं।
|
||||
|
||||
आपसे पहले हर कोई counter छोड़ने से पहले अपने burgers ready होने का wait कर रहा है क्योंकि 8 cashiers में से हर एक next order लेने से पहले तुरंत जाकर burger prepare करता है।
|
||||
|
||||
<img src="/img/async/parallel-burgers/parallel-burgers-01.png" class="illustration">
|
||||
|
||||
फिर आखिरकार आपकी turn आती है, आप अपनी crush और अपने लिए 2 बहुत fancy burgers का order देते हैं।
|
||||
|
||||
आप pay करते हैं 💸।
|
||||
|
||||
<img src="/img/async/parallel-burgers/parallel-burgers-02.png" class="illustration">
|
||||
|
||||
Cashier kitchen में जाता है।
|
||||
|
||||
आप counter 🕙 के सामने खड़े होकर wait करते हैं, ताकि आपसे पहले कोई और आपके burgers न ले जाए, क्योंकि turns के लिए कोई numbers नहीं हैं।
|
||||
|
||||
<img src="/img/async/parallel-burgers/parallel-burgers-03.png" class="illustration">
|
||||
|
||||
क्योंकि आप और आपकी crush इस बात में busy हैं कि कोई आपके आगे न आ जाए और आपके burgers आते ही उन्हें न ले जाए, आप अपनी crush पर attention नहीं दे सकते। 😞
|
||||
|
||||
यह "synchronous" work है, आप cashier/cook 👨🍳 के साथ "synchronized" हैं। आपको wait 🕙 करना है और exact moment पर वहाँ होना है जब cashier/cook 👨🍳 burgers finish करता है और आपको देता है, नहीं तो कोई और उन्हें ले सकता है।
|
||||
|
||||
<img src="/img/async/parallel-burgers/parallel-burgers-04.png" class="illustration">
|
||||
|
||||
फिर आपका cashier/cook 👨🍳 लंबे समय तक counter के सामने wait 🕙 कराने के बाद आखिरकार आपके burgers लेकर वापस आता है।
|
||||
|
||||
<img src="/img/async/parallel-burgers/parallel-burgers-05.png" class="illustration">
|
||||
|
||||
आप अपने burgers लेते हैं और अपनी crush के साथ table पर जाते हैं।
|
||||
|
||||
आप बस उन्हें खाते हैं, और आपका काम हो जाता है। ⏹
|
||||
|
||||
<img src="/img/async/parallel-burgers/parallel-burgers-06.png" class="illustration">
|
||||
|
||||
ज़्यादा बात या flirting नहीं हुई क्योंकि अधिकतर समय counter के सामने wait 🕙 करने में spend हुआ। 😞
|
||||
|
||||
/// note | नोट
|
||||
|
||||
सुंदर illustrations [Ketrina Thompson](https://www.instagram.com/ketrinadrawsalot) द्वारा। 🎨
|
||||
|
||||
///
|
||||
|
||||
---
|
||||
|
||||
Parallel burgers के इस scenario में, आप एक computer / program 🤖 हैं जिसमें दो processors हैं (आप और आपकी crush), दोनों wait 🕙 कर रहे हैं और लंबे समय तक "counter पर waiting" 🕙 में अपना attention ⏯ dedicate कर रहे हैं।
|
||||
|
||||
Fast food store में 8 processors (cashiers/cooks) हैं। जबकि concurrent burgers store में शायद केवल 2 (एक cashier और एक cook) रहे होंगे।
|
||||
|
||||
लेकिन फिर भी, final experience सबसे अच्छा नहीं है। 😞
|
||||
|
||||
---
|
||||
|
||||
यह burgers के लिए parallel equivalent story होगी। 🍔
|
||||
|
||||
इसके एक और "real life" example के लिए, एक bank imagine करें।
|
||||
|
||||
हाल तक, अधिकांश banks में multiple cashiers 👨💼👨💼👨💼👨💼 और एक बड़ी line 🕙🕙🕙🕙🕙🕙🕙🕙 होती थी।
|
||||
|
||||
सभी cashiers एक client के बाद दूसरे client के साथ सारा काम कर रहे होते थे 👨💼⏯।
|
||||
|
||||
और आपको line में लंबे समय तक wait 🕙 करना पड़ता है या आप अपनी turn खो देते हैं।
|
||||
|
||||
आप शायद अपनी crush 😍 को bank 🏦 में errands करने के लिए अपने साथ नहीं ले जाना चाहेंगे।
|
||||
|
||||
### Burger निष्कर्ष { #burger-conclusion }
|
||||
|
||||
"अपनी crush के साथ fast food burgers" के इस scenario में, क्योंकि बहुत waiting 🕙 है, concurrent system ⏸🔀⏯ रखना कहीं अधिक sensible है।
|
||||
|
||||
अधिकांश web applications के लिए यही case है।
|
||||
|
||||
बहुत, बहुत सारे users, लेकिन आपका server उनकी not-so-good connection द्वारा उनकी requests भेजने का wait 🕙 कर रहा है।
|
||||
|
||||
और फिर responses वापस आने का फिर से wait 🕙 कर रहा है।
|
||||
|
||||
यह "waiting" 🕙 microseconds में measure की जाती है, लेकिन फिर भी, सबको जोड़ने पर, अंत में काफी waiting हो जाती है।
|
||||
|
||||
इसीलिए web APIs के लिए asynchronous ⏸🔀⏯ code use करना बहुत sensible है।
|
||||
|
||||
इसी तरह की asynchronicity ने NodeJS को popular बनाया (भले ही NodeJS parallel नहीं है) और यही Go की programming language के रूप में strength है।
|
||||
|
||||
और यही same level की performance आपको **FastAPI** के साथ मिलती है।
|
||||
|
||||
और क्योंकि आपके पास parallelism और asynchronicity एक ही समय पर हो सकते हैं, आपको tested NodeJS frameworks में से अधिकांश से higher performance मिलती है और Go के बराबर, जो C के करीब एक compiled language है [(यह सब Starlette के कारण)](https://www.techempower.com/benchmarks/#section=data-r17&hw=ph&test=query&l=zijmkf-1)।
|
||||
|
||||
### क्या concurrency parallelism से बेहतर है? { #is-concurrency-better-than-parallelism }
|
||||
|
||||
नहीं! यह story की moral नहीं है।
|
||||
|
||||
Concurrency, parallelism से अलग है। और यह उन **specific** scenarios में बेहतर है जिनमें बहुत waiting शामिल होती है। इसी कारण, web application development के लिए यह आम तौर पर parallelism से बहुत बेहतर है। लेकिन हर चीज़ के लिए नहीं।
|
||||
|
||||
तो, इसे balance करने के लिए, निम्न short story imagine करें:
|
||||
|
||||
> आपको एक बड़ा, गंदा house clean करना है।
|
||||
|
||||
*हाँ, यही पूरी story है*।
|
||||
|
||||
---
|
||||
|
||||
कहीं भी कोई waiting 🕙 नहीं है, बस बहुत सारा काम करना है, house के multiple places में।
|
||||
|
||||
आप burgers example की तरह turns रख सकते थे, पहले living room, फिर kitchen, लेकिन क्योंकि आप किसी चीज़ का wait 🕙 नहीं कर रहे, बस cleaning and cleaning कर रहे हैं, turns किसी चीज़ को affect नहीं करेंगे।
|
||||
|
||||
Turns (concurrency) के साथ या बिना finish करने में उतना ही time लगेगा और आपने उतना ही काम किया होगा।
|
||||
|
||||
लेकिन इस case में, अगर आप 8 ex-cashier/cooks/now-cleaners ला सकें, और उनमें से हर एक (plus आप) house का एक zone clean करने के लिए ले सके, तो आप extra help के साथ सारा काम **parallel** में कर सकते हैं, और बहुत जल्दी finish कर सकते हैं।
|
||||
|
||||
इस scenario में, cleaners में से हर एक (आप सहित) एक processor होगा, job का अपना part कर रहा होगा।
|
||||
|
||||
और क्योंकि execution time का अधिकतर हिस्सा actual work (waiting के बजाय) में लगता है, और computer में work <abbr title="Central Processing Unit - केंद्रीय प्रसंस्करण इकाई">CPU</abbr> द्वारा किया जाता है, वे इन problems को "CPU bound" कहते हैं।
|
||||
|
||||
---
|
||||
|
||||
CPU bound operations के common examples ऐसी चीज़ें हैं जिन्हें complex math processing की ज़रूरत होती है।
|
||||
|
||||
उदाहरण के लिए:
|
||||
|
||||
* **Audio** या **image processing**।
|
||||
* **Computer vision**: एक image millions of pixels से composed होती है, हर pixel में 3 values / colors होते हैं, उसे process करने के लिए आम तौर पर उन pixels पर कुछ compute करना पड़ता है, सब एक ही समय पर।
|
||||
* **Machine Learning**: इसमें आम तौर पर बहुत सारे "matrix" और "vector" multiplications की ज़रूरत होती है। Numbers वाली एक huge spreadsheet के बारे में सोचें और उन सभी को एक ही समय पर multiply करना।
|
||||
* **Deep Learning**: यह Machine Learning का sub-field है, इसलिए, वही लागू होता है। बस इतना है कि multiply करने के लिए numbers की एक single spreadsheet नहीं होती, बल्कि उनका huge set होता है, और कई cases में, आप उन models को build और / या use करने के लिए एक special processor का उपयोग करते हैं।
|
||||
|
||||
### Concurrency + Parallelism: Web + Machine Learning { #concurrency-parallelism-web-machine-learning }
|
||||
|
||||
**FastAPI** के साथ आप concurrency का advantage ले सकते हैं जो web development के लिए बहुत common है (NodeJS का वही main attraction)।
|
||||
|
||||
लेकिन आप Machine Learning systems जैसे **CPU bound** workloads के लिए parallelism और multiprocessing (multiple processes parallel में चलाना) के benefits का भी exploit कर सकते हैं।
|
||||
|
||||
वह, साथ ही यह simple fact कि Python **Data Science**, Machine Learning और खासकर Deep Learning की main language है, FastAPI को Data Science / Machine Learning web APIs और applications (कई अन्य के बीच) के लिए बहुत अच्छा match बनाता है।
|
||||
|
||||
Production में इस parallelism को कैसे achieve करें, यह देखने के लिए [Deployment](deployment/index.md) के बारे में section देखें।
|
||||
|
||||
## `async` और `await` { #async-and-await }
|
||||
|
||||
Python के आधुनिक versions में asynchronous code define करने का बहुत intuitive तरीका है। इससे यह सामान्य "sequential" code जैसा दिखता है और सही moments पर आपके लिए "awaiting" करता है।
|
||||
|
||||
जब कोई operation results देने से पहले waiting require करेगा और इन नए Python features का support रखता है, तो आप उसे ऐसे code कर सकते हैं:
|
||||
|
||||
```Python
|
||||
burgers = await get_burgers(2)
|
||||
```
|
||||
|
||||
यहाँ key `await` है। यह Python को बताता है कि `burgers` में results store करने से पहले उसे `get_burgers(2)` के अपना काम 🕙 finish करने का wait ⏸ करना है। इससे, Python जान जाएगा कि वह meanwhile कुछ और 🔀 ⏯ कर सकता है (जैसे कोई दूसरी request receive करना)।
|
||||
|
||||
`await` के work करने के लिए, इसे ऐसे function के अंदर होना चाहिए जो इस asynchronicity को support करता हो। ऐसा करने के लिए, आप बस इसे `async def` के साथ declare करते हैं:
|
||||
|
||||
```Python hl_lines="1"
|
||||
async def get_burgers(number: int):
|
||||
# Burgers बनाने के लिए कुछ asynchronous काम करें
|
||||
return burgers
|
||||
```
|
||||
|
||||
...`def` के बजाय:
|
||||
|
||||
```Python hl_lines="2"
|
||||
# यह asynchronous नहीं है
|
||||
def get_sequential_burgers(number: int):
|
||||
# Burgers बनाने के लिए कुछ sequential काम करें
|
||||
return burgers
|
||||
```
|
||||
|
||||
`async def` के साथ, Python जानता है कि उस function के अंदर उसे `await` expressions के बारे में aware रहना है, और वह उस function के execution को "pause" ⏸ कर सकता है और वापस आने से पहले कुछ और 🔀 कर सकता है।
|
||||
|
||||
जब आप किसी `async def` function को call करना चाहते हैं, तो आपको उसे "await" करना होता है। इसलिए, यह काम नहीं करेगा:
|
||||
|
||||
```Python
|
||||
# यह काम नहीं करेगा, क्योंकि get_burgers को async def के साथ define किया गया था
|
||||
burgers = get_burgers(2)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
तो, अगर आप कोई library use कर रहे हैं जो आपको बताती है कि आप उसे `await` के साथ call कर सकते हैं, तो आपको उसका उपयोग करने वाले *path operation functions* को `async def` के साथ create करना होगा, जैसे:
|
||||
|
||||
```Python hl_lines="2-3"
|
||||
@app.get('/burgers')
|
||||
async def read_burgers():
|
||||
burgers = await get_burgers(2)
|
||||
return burgers
|
||||
```
|
||||
|
||||
### अधिक तकनीकी विवरण { #more-technical-details }
|
||||
|
||||
आपने notice किया होगा कि `await` केवल `async def` के साथ defined functions के अंदर ही use किया जा सकता है।
|
||||
|
||||
लेकिन उसी समय, `async def` के साथ defined functions को "awaited" होना पड़ता है। इसलिए, `async def` वाले functions केवल `async def` के साथ defined functions के अंदर ही call किए जा सकते हैं।
|
||||
|
||||
तो, egg और chicken के बारे में, आप first `async` function को कैसे call करते हैं?
|
||||
|
||||
अगर आप **FastAPI** के साथ काम कर रहे हैं तो आपको इसकी चिंता करने की ज़रूरत नहीं है, क्योंकि वह "first" function आपका *path operation function* होगा, और FastAPI जानता होगा कि सही काम कैसे करना है।
|
||||
|
||||
लेकिन अगर आप FastAPI के बिना `async` / `await` use करना चाहते हैं, तो आप ऐसा भी कर सकते हैं।
|
||||
|
||||
### अपना async code लिखें { #write-your-own-async-code }
|
||||
|
||||
Starlette (और **FastAPI**) [AnyIO](https://anyio.readthedocs.io/en/stable/) पर based हैं, जो इसे Python की standard library [asyncio](https://docs.python.org/3/library/asyncio-task.html) और [Trio](https://trio.readthedocs.io/en/stable/) दोनों के साथ compatible बनाता है।
|
||||
|
||||
विशेष रूप से, आप अपने advanced concurrency use cases के लिए सीधे [AnyIO](https://anyio.readthedocs.io/en/stable/) use कर सकते हैं जिन्हें आपके अपने code में अधिक advanced patterns की ज़रूरत होती है।
|
||||
|
||||
और भले ही आप FastAPI use नहीं कर रहे हों, आप high compatibility और इसके benefits (जैसे *structured concurrency*) पाने के लिए [AnyIO](https://anyio.readthedocs.io/en/stable/) के साथ अपने खुद के async applications भी लिख सकते हैं।
|
||||
|
||||
मैंने AnyIO के ऊपर एक और library बनाई, ऊपर एक thin layer के रूप में, ताकि type annotations को थोड़ा improve किया जा सके और बेहतर **autocompletion**, **inline errors**, आदि मिल सकें। इसमें एक friendly introduction और tutorial भी है ताकि आपको **समझने** और **अपना async code** लिखने में मदद मिले: [Asyncer](https://asyncer.tiangolo.com/)। यह विशेष रूप से useful होगा अगर आपको **async code को regular** (blocking/synchronous) code के साथ **combine** करना हो।
|
||||
|
||||
### Asynchronous code के अन्य forms { #other-forms-of-asynchronous-code }
|
||||
|
||||
`async` और `await` use करने की यह style language में relatively new है।
|
||||
|
||||
लेकिन यह asynchronous code के साथ काम करना बहुत आसान बनाती है।
|
||||
|
||||
यही same syntax (या लगभग identical) हाल ही में JavaScript के modern versions (Browser और NodeJS में) में भी शामिल किया गया था।
|
||||
|
||||
लेकिन उससे पहले, asynchronous code handle करना कहीं अधिक complex और difficult था।
|
||||
|
||||
Python के previous versions में, आप threads या [Gevent](https://www.gevent.org/) use कर सकते थे। लेकिन code समझने, debug करने और उसके बारे में सोचने के लिए कहीं अधिक complex होता है।
|
||||
|
||||
NodeJS / Browser JavaScript के previous versions में, आप "callbacks" use करते। जो "callback hell" तक ले जाता है।
|
||||
|
||||
## Coroutines { #coroutines }
|
||||
|
||||
**Coroutine** बस उस चीज़ के लिए बहुत fancy term है जो किसी `async def` function द्वारा return की जाती है। Python जानता है कि यह function जैसी कोई चीज़ है, जिसे वह start कर सकता है और जो किसी point पर end होगी, लेकिन यह internally pause ⏸ भी हो सकती है, जब भी इसके अंदर कोई `await` हो।
|
||||
|
||||
लेकिन asynchronous code को `async` और `await` के साथ use करने की यह सारी functionality कई बार "coroutines" use करने के रूप में summarize की जाती है। यह Go की main key feature, "Goroutines" से comparable है।
|
||||
|
||||
## निष्कर्ष { #conclusion }
|
||||
|
||||
आइए ऊपर वाला वही phrase देखें:
|
||||
|
||||
> Python के आधुनिक versions **"asynchronous code"** का support करते हैं, जो **"coroutines"** नाम की चीज़ का उपयोग करता है, **`async` और `await`** syntax के साथ।
|
||||
|
||||
अब यह अधिक sense बनाना चाहिए। ✨
|
||||
|
||||
यह सब FastAPI (Starlette के माध्यम से) को power करता है और इसे इतनी impressive performance देता है।
|
||||
|
||||
## बहुत तकनीकी विवरण { #very-technical-details }
|
||||
|
||||
/// warning | चेतावनी
|
||||
|
||||
आप शायद इसे skip कर सकते हैं।
|
||||
|
||||
ये **FastAPI** अंदर से कैसे काम करता है, इसके बहुत technical details हैं।
|
||||
|
||||
अगर आपके पास काफी technical knowledge (coroutines, threads, blocking, आदि) है और आप curious हैं कि FastAPI `async def` बनाम normal `def` को कैसे handle करता है, तो आगे बढ़ें।
|
||||
|
||||
///
|
||||
|
||||
### Path operation functions { #path-operation-functions }
|
||||
|
||||
जब आप किसी *path operation function* को `async def` के बजाय normal `def` के साथ declare करते हैं, तो इसे directly call करने के बजाय (क्योंकि यह server को block करेगा), एक external threadpool में run किया जाता है जिसे फिर awaited किया जाता है।
|
||||
|
||||
अगर आप किसी दूसरे async framework से आ रहे हैं जो ऊपर described तरीके से काम नहीं करता और आप tiny performance gain (लगभग 100 nanoseconds) के लिए trivial compute-only *path operation functions* को plain `def` के साथ define करने के आदी हैं, तो कृपया ध्यान दें कि **FastAPI** में effect बिल्कुल उल्टा होगा। इन cases में, `async def` use करना बेहतर है, जब तक कि आपके *path operation functions* ऐसा code use न करें जो blocking <abbr title="Input/Output - इनपुट/आउटपुट: डिस्क पढ़ना या लिखना, network communications.">I/O</abbr> perform करता हो।
|
||||
|
||||
फिर भी, दोनों situations में, संभावना है कि **FastAPI** आपके previous framework से [फिर भी तेज़ होगा](index.md#performance) (या कम से कम comparable होगा)।
|
||||
|
||||
### Dependencies { #dependencies }
|
||||
|
||||
[Dependencies](tutorial/dependencies/index.md) के लिए भी यही लागू होता है। अगर कोई dependency `async def` के बजाय standard `def` function है, तो उसे external threadpool में run किया जाता है।
|
||||
|
||||
### Sub-dependencies { #sub-dependencies }
|
||||
|
||||
आपके पास multiple dependencies और [sub-dependencies](tutorial/dependencies/sub-dependencies.md) हो सकती हैं जो एक-दूसरे को require करती हैं (function definitions के parameters के रूप में), उनमें से कुछ `async def` के साथ created हो सकती हैं और कुछ normal `def` के साथ। यह फिर भी work करेगा, और normal `def` के साथ created ones को "awaited" होने के बजाय external thread (threadpool से) पर call किया जाएगा।
|
||||
|
||||
### अन्य utility functions { #other-utility-functions }
|
||||
|
||||
कोई भी अन्य utility function जिसे आप directly call करते हैं, normal `def` या `async def` के साथ created हो सकता है और FastAPI आपके उसे call करने के तरीके को affect नहीं करेगा।
|
||||
|
||||
यह उन functions के contrast में है जिन्हें FastAPI आपके लिए call करता है: *path operation functions* और dependencies।
|
||||
|
||||
अगर आपका utility function `def` वाला normal function है, तो उसे directly call किया जाएगा (जैसा आप अपने code में लिखते हैं), threadpool में नहीं; अगर function `async def` के साथ created है तो आपको अपने code में उसे call करते समय उस function को `await` करना चाहिए।
|
||||
|
||||
---
|
||||
|
||||
फिर से, ये बहुत technical details हैं जो शायद useful होंगे अगर आप इन्हें search करते हुए आए हैं।
|
||||
|
||||
अन्यथा, आपको ऊपर के section की guidelines के साथ ठीक होना चाहिए: <a href="#in-a-hurry">जल्दी में हैं?</a>।
|
||||
34
docs/hi/docs/benchmarks.md
Normal file
34
docs/hi/docs/benchmarks.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# Benchmarks { #benchmarks }
|
||||
|
||||
स्वतंत्र TechEmpower benchmarks दिखाते हैं कि Uvicorn के अंतर्गत चलने वाले **FastAPI** applications [उपलब्ध सबसे तेज़ Python frameworks में से एक](https://www.techempower.com/benchmarks/#section=test&runid=7464e520-0dc2-473d-bd34-dbdfd7e85911&hw=ph&test=query&l=zijzen-7) हैं, केवल Starlette और Uvicorn स्वयं से नीचे (जिनका उपयोग FastAPI द्वारा internally किया जाता है)।
|
||||
|
||||
लेकिन benchmarks और comparisons देखते समय आपको निम्न बातों को ध्यान में रखना चाहिए।
|
||||
|
||||
## Benchmarks और speed { #benchmarks-and-speed }
|
||||
|
||||
जब आप benchmarks देखते हैं, तो अलग-अलग प्रकार के कई tools को equivalent मानकर compare होते देखना आम बात है।
|
||||
|
||||
विशेष रूप से, Uvicorn, Starlette और FastAPI को एक साथ compare होते देखना (कई अन्य tools के साथ)।
|
||||
|
||||
Tool जितनी सरल समस्या हल करता है, उसे उतनी ही बेहतर performance मिलेगी। और अधिकांश benchmarks, tool द्वारा प्रदान की जाने वाली अतिरिक्त features को test नहीं करते।
|
||||
|
||||
Hierarchy इस प्रकार है:
|
||||
|
||||
* **Uvicorn**: एक ASGI server
|
||||
* **Starlette**: (Uvicorn का उपयोग करता है) एक web microframework
|
||||
* **FastAPI**: (Starlette का उपयोग करता है) APIs बनाने के लिए कई अतिरिक्त features वाला एक API microframework, जिसमें data validation आदि शामिल हैं।
|
||||
|
||||
* **Uvicorn**:
|
||||
* इसकी performance सबसे अच्छी होगी, क्योंकि इसमें server स्वयं के अलावा बहुत अधिक extra code नहीं होता।
|
||||
* आप सीधे Uvicorn में application नहीं लिखेंगे। इसका मतलब होगा कि आपके code में कमोबेश, कम से कम, Starlette (या **FastAPI**) द्वारा प्रदान किया गया सारा code शामिल करना पड़ेगा। और यदि आपने ऐसा किया, तो आपके final application में framework का उपयोग करने और अपने app code तथा bugs को कम करने जितना ही overhead होगा।
|
||||
* यदि आप Uvicorn की तुलना कर रहे हैं, तो इसकी तुलना Daphne, Hypercorn, uWSGI आदि से करें। Application servers से।
|
||||
* **Starlette**:
|
||||
* Uvicorn के बाद इसकी performance अगली सबसे अच्छी होगी। वास्तव में, Starlette चलने के लिए Uvicorn का उपयोग करता है। इसलिए, संभवतः यह केवल अधिक code execute करने के कारण Uvicorn से "धीमा" हो सकता है।
|
||||
* लेकिन यह आपको simple web applications बनाने के लिए tools देता है, जैसे paths पर आधारित routing आदि।
|
||||
* यदि आप Starlette की तुलना कर रहे हैं, तो इसकी तुलना Sanic, Flask, Django आदि से करें। Web frameworks (या microframeworks) से।
|
||||
* **FastAPI**:
|
||||
* जैसे Starlette, Uvicorn का उपयोग करता है और उससे तेज़ नहीं हो सकता, वैसे ही **FastAPI**, Starlette का उपयोग करता है, इसलिए यह उससे तेज़ नहीं हो सकता।
|
||||
* FastAPI, Starlette के ऊपर और features प्रदान करता है। ऐसे features जिनकी आपको APIs बनाते समय लगभग हमेशा आवश्यकता होती है, जैसे data validation और serialization। और इसका उपयोग करके आपको automatic documentation मुफ्त में मिलती है (automatic documentation running applications में overhead भी नहीं जोड़ती, यह startup पर generate होती है)।
|
||||
* यदि आपने FastAPI का उपयोग नहीं किया और सीधे Starlette (या कोई अन्य tool, जैसे Sanic, Flask, Responder आदि) का उपयोग किया, तो आपको सभी data validation और serialization स्वयं implement करने पड़ेंगे। इसलिए, आपके final application में फिर भी उतना ही overhead होगा जितना FastAPI का उपयोग करके बनाए जाने पर होता। और कई मामलों में, यह data validation और serialization applications में लिखे गए code का सबसे बड़ा हिस्सा होता है।
|
||||
* इसलिए, FastAPI का उपयोग करके आप development time, bugs और lines of code बचाते हैं, और संभवतः आपको वही performance (या बेहतर) मिलेगी जो आपको इसे उपयोग न करने पर मिलती (क्योंकि तब आपको यह सब अपने code में implement करना पड़ता)।
|
||||
* यदि आप FastAPI की तुलना कर रहे हैं, तो इसकी तुलना ऐसे web application framework (या tools के set) से करें जो data validation, serialization और documentation प्रदान करता हो, जैसे Flask-apispec, NestJS, Molten आदि। ऐसे frameworks जिनमें integrated automatic data validation, serialization और documentation हो।
|
||||
23
docs/hi/docs/editor-support.md
Normal file
23
docs/hi/docs/editor-support.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# एडिटर सपोर्ट { #editor-support }
|
||||
|
||||
आधिकारिक [FastAPI Extension](https://marketplace.visualstudio.com/items?itemName=FastAPILabs.fastapi-vscode) आपके FastAPI development workflow को *पाथ ऑपरेशन* discovery, navigation, साथ ही FastAPI Cloud deployment और live log streaming के साथ बेहतर बनाता है।
|
||||
|
||||
Extension के बारे में अधिक जानकारी के लिए, [GitHub repository](https://github.com/fastapi/fastapi-vscode) पर README देखें।
|
||||
|
||||
## सेटअप और इंस्टॉलेशन { #setup-and-installation }
|
||||
|
||||
**FastAPI Extension** [VS Code](https://code.visualstudio.com/) और [Cursor](https://www.cursor.com/) दोनों के लिए उपलब्ध है। इसे हर editor के Extensions panel से सीधे "FastAPI" खोजकर और **FastAPI Labs** द्वारा प्रकाशित extension चुनकर install किया जा सकता है। यह extension browser-based editors जैसे [vscode.dev](https://vscode.dev) और [github.dev](https://github.dev) में भी काम करता है।
|
||||
|
||||
### एप्लिकेशन डिस्कवरी { #application-discovery }
|
||||
|
||||
Default रूप से, extension आपके workspace में `FastAPI()` instantiate करने वाली files को scan करके FastAPI applications को अपने-आप discover करेगा। यदि auto-detection आपके project structure के लिए काम नहीं करता, तो आप `pyproject.toml` में `[tool.fastapi]` के माध्यम से या module notation (जैसे `myapp.main:app`) का उपयोग करके `fastapi.entryPoint` VS Code setting में entrypoint specify कर सकते हैं।
|
||||
|
||||
## फीचर्स { #features }
|
||||
|
||||
- **Path Operation Explorer** - आपके application में सभी <dfn title="रूट्स, एंडपॉइंट्स">*पाथ ऑपरेशन्स*</dfn> का sidebar tree view। किसी भी route या router definition पर जाने के लिए click करें।
|
||||
- **Route Search** - <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>E</kbd> (macOS पर: <kbd>Cmd</kbd> + <kbd>Shift</kbd> + <kbd>E</kbd>) के साथ path, method, या name के आधार पर search करें।
|
||||
- **CodeLens Navigation** - test client calls (जैसे `client.get('/items')`) के ऊपर clickable links, जो tests और implementation के बीच quick navigation के लिए matching *पाथ ऑपरेशन* पर ले जाते हैं।
|
||||
- **Deploy to FastAPI Cloud** - आपकी app को [FastAPI Cloud](https://fastapicloud.com/) पर one-click deployment।
|
||||
- **Stream Application Logs** - level filtering और text search के साथ आपके FastAPI Cloud-deployed application से real-time log streaming।
|
||||
|
||||
यदि आप extension के features से परिचित होना चाहते हैं, तो Command Palette (<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd> या macOS पर: <kbd>Cmd</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd>) खोलकर और "Welcome: Open walkthrough..." चुनकर, फिर "Get started with FastAPI" walkthrough चुनकर extension walkthrough देख सकते हैं।
|
||||
134
docs/hi/docs/fastapi-cli.md
Normal file
134
docs/hi/docs/fastapi-cli.md
Normal file
@@ -0,0 +1,134 @@
|
||||
# FastAPI CLI { #fastapi-cli }
|
||||
|
||||
**FastAPI <abbr title="command line interface - कमांड लाइन इंटरफ़ेस">CLI</abbr>** एक command line प्रोग्राम है जिसका उपयोग आप अपने FastAPI ऐप को serve करने, अपने FastAPI प्रोजेक्ट को manage करने, और भी बहुत कुछ करने के लिए कर सकते हैं।
|
||||
|
||||
जब आप FastAPI इंस्टॉल करते हैं (जैसे `pip install "fastapi[standard]"` के साथ), तो इसके साथ एक command line प्रोग्राम आता है जिसे आप terminal में चला सकते हैं।
|
||||
|
||||
डेवलपमेंट के लिए अपना FastAPI ऐप चलाने के लिए, आप `fastapi dev` command का उपयोग कर सकते हैं:
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ <font color="#4E9A06">fastapi</font> dev
|
||||
|
||||
<span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting development server 🚀
|
||||
|
||||
Searching for package file structure from directories with
|
||||
<font color="#3465A4">__init__.py</font> files
|
||||
Importing from <font color="#75507B">/home/user/code/</font><font color="#AD7FA8">awesomeapp</font>
|
||||
|
||||
<span style="background-color:#007166"><font color="#D3D7CF"> module </font></span> 🐍 main.py
|
||||
|
||||
<span style="background-color:#007166"><font color="#D3D7CF"> code </font></span> Importing the FastAPI app object from the module with the
|
||||
following code:
|
||||
|
||||
<u style="text-decoration-style:solid">from </u><u style="text-decoration-style:solid"><b>main</b></u><u style="text-decoration-style:solid"> import </u><u style="text-decoration-style:solid"><b>app</b></u>
|
||||
|
||||
<span style="background-color:#007166"><font color="#D3D7CF"> app </font></span> Using import string: <font color="#3465A4">main:app</font>
|
||||
|
||||
<span style="background-color:#007166"><font color="#D3D7CF"> server </font></span> Server started at <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000</u></font>
|
||||
<span style="background-color:#007166"><font color="#D3D7CF"> server </font></span> Documentation at <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000/docs</u></font>
|
||||
|
||||
<span style="background-color:#007166"><font color="#D3D7CF"> tip </font></span> Running in development mode, for production use:
|
||||
<b>fastapi run</b>
|
||||
|
||||
Logs:
|
||||
|
||||
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Will watch for changes in these directories:
|
||||
<b>[</b><font color="#4E9A06">'/home/user/code/awesomeapp'</font><b>]</b>
|
||||
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Uvicorn running on <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000</u></font> <b>(</b>Press CTRL+C to
|
||||
quit<b>)</b>
|
||||
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started reloader process <b>[</b><font color="#34E2E2"><b>383138</b></font><b>]</b> using WatchFiles
|
||||
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started server process <b>[</b><font color="#34E2E2"><b>383153</b></font><b>]</b>
|
||||
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Waiting for application startup.
|
||||
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Application startup complete.
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
प्रोडक्शन के लिए आप `fastapi dev` की जगह `fastapi run` का उपयोग करेंगे। 🚀
|
||||
|
||||
///
|
||||
|
||||
आंतरिक रूप से, **FastAPI CLI** [Uvicorn](https://www.uvicorn.dev) का उपयोग करता है, जो एक high-performance, production-ready, ASGI server है। 😎
|
||||
|
||||
`fastapi` CLI चलाने के लिए FastAPI ऐप को अपने-आप detect करने की कोशिश करेगा, यह मानते हुए कि यह `main.py` फ़ाइल में `app` नाम का object है (या कुछ अन्य variants में से कोई एक)।
|
||||
|
||||
लेकिन आप उपयोग किए जाने वाले ऐप को स्पष्ट रूप से configure कर सकते हैं।
|
||||
|
||||
## ऐप `entrypoint` को `pyproject.toml` में configure करें { #configure-the-app-entrypoint-in-pyproject-toml }
|
||||
|
||||
आप `pyproject.toml` फ़ाइल में यह configure कर सकते हैं कि आपका ऐप कहाँ स्थित है, जैसे:
|
||||
|
||||
```toml
|
||||
[tool.fastapi]
|
||||
entrypoint = "main:app"
|
||||
```
|
||||
|
||||
वह `entrypoint` `fastapi` command को बताएगा कि उसे ऐप को इस तरह import करना चाहिए:
|
||||
|
||||
```python
|
||||
from main import app
|
||||
```
|
||||
|
||||
अगर आपका code इस तरह structured था:
|
||||
|
||||
```
|
||||
.
|
||||
├── backend
|
||||
│ ├── main.py
|
||||
│ ├── __init__.py
|
||||
```
|
||||
|
||||
तो आप `entrypoint` को इस तरह set करेंगे:
|
||||
|
||||
```toml
|
||||
[tool.fastapi]
|
||||
entrypoint = "backend.main:app"
|
||||
```
|
||||
|
||||
जो इसके बराबर होगा:
|
||||
|
||||
```python
|
||||
from backend.main import app
|
||||
```
|
||||
|
||||
### path के साथ या `--entrypoint` CLI option के साथ `fastapi dev` { #fastapi-dev-with-path-or-with-entrypoint-cli-option }
|
||||
|
||||
आप `fastapi dev` command को file path भी pass कर सकते हैं, और यह उपयोग किए जाने वाले FastAPI app object का अनुमान लगा लेगा:
|
||||
|
||||
```console
|
||||
$ fastapi dev main.py
|
||||
```
|
||||
|
||||
या, आप `fastapi dev` command को `--entrypoint` option भी pass कर सकते हैं:
|
||||
|
||||
```console
|
||||
$ fastapi dev --entrypoint main:app
|
||||
```
|
||||
|
||||
लेकिन आपको हर बार `fastapi` command call करते समय सही path\entrypoint pass करना याद रखना होगा।
|
||||
|
||||
इसके अतिरिक्त, अन्य tools इसे ढूँढने में सक्षम नहीं हो सकते हैं, उदाहरण के लिए [VS Code एक्सटेंशन](editor-support.md) या [FastAPI Cloud](https://fastapicloud.com), इसलिए `pyproject.toml` में `entrypoint` का उपयोग करने की सिफारिश की जाती है।
|
||||
|
||||
## `fastapi dev` { #fastapi-dev }
|
||||
|
||||
`fastapi dev` चलाने से development mode शुरू होता है।
|
||||
|
||||
Default रूप से, **auto-reload** enabled होता है, जो आपके code में changes करने पर server को अपने-आप reload कर देता है। यह resource-intensive है और disabled होने की तुलना में कम stable हो सकता है। आपको इसे केवल development के लिए ही उपयोग करना चाहिए। यह IP address `127.0.0.1` पर भी listen करता है, जो आपकी machine का खुद से ही communicate करने वाला IP है (`localhost`)।
|
||||
|
||||
## `fastapi run` { #fastapi-run }
|
||||
|
||||
`fastapi run` execute करने से FastAPI production mode में शुरू होता है।
|
||||
|
||||
Default रूप से, **auto-reload** disabled होता है। यह IP address `0.0.0.0` पर भी listen करता है, जिसका मतलब सभी available IP addresses है, इस तरह यह उस machine से communicate कर सकने वाले किसी भी व्यक्ति के लिए publicly accessible होगा। आम तौर पर आप production में इसे इसी तरह चलाएँगे, उदाहरण के लिए, एक container में।
|
||||
|
||||
अधिकतर मामलों में आपके पास ऊपर से HTTPS handle करने वाला "termination proxy" होगा (और होना चाहिए), यह इस पर निर्भर करेगा कि आप अपना application कैसे deploy करते हैं, आपका provider आपके लिए यह कर सकता है, या आपको इसे खुद set up करने की ज़रूरत हो सकती है।
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
आप इसके बारे में [deployment documentation](deployment/index.md) में और जान सकते हैं।
|
||||
|
||||
///
|
||||
201
docs/hi/docs/features.md
Normal file
201
docs/hi/docs/features.md
Normal file
@@ -0,0 +1,201 @@
|
||||
# विशेषताएँ { #features }
|
||||
|
||||
## FastAPI की विशेषताएँ { #fastapi-features }
|
||||
|
||||
**FastAPI** आपको निम्नलिखित देता है:
|
||||
|
||||
### खुले मानकों पर आधारित { #based-on-open-standards }
|
||||
|
||||
* API बनाने के लिए [**OpenAPI**](https://github.com/OAI/OpenAPI-Specification), जिसमें <dfn title="इन्हें भी कहा जाता है: endpoints, routes">पाथ</dfn> <dfn title="इन्हें HTTP methods भी कहा जाता है, जैसे POST, GET, PUT, DELETE">ऑपरेशन्स</dfn>, parameters, request bodies, security, आदि की घोषणाएँ शामिल हैं।
|
||||
* [**JSON Schema**](https://json-schema.org/) के साथ automatic data model documentation (क्योंकि OpenAPI स्वयं JSON Schema पर आधारित है)।
|
||||
* इन मानकों के इर्द-गिर्द डिज़ाइन किया गया, एक बहुत सावधानीपूर्वक अध्ययन के बाद। ऊपर से बाद में जोड़ी गई परत की तरह नहीं।
|
||||
* यह कई भाषाओं में automatic **client code generation** का उपयोग करने की भी अनुमति देता है।
|
||||
|
||||
### Automatic docs { #automatic-docs }
|
||||
|
||||
Interactive API documentation और exploration web user interfaces। क्योंकि framework OpenAPI पर आधारित है, कई विकल्प हैं, जिनमें 2 default रूप से शामिल हैं।
|
||||
|
||||
* [**Swagger UI**](https://github.com/swagger-api/swagger-ui), interactive exploration के साथ, अपने API को सीधे browser से call और test करें।
|
||||
|
||||

|
||||
|
||||
* [**ReDoc**](https://github.com/Rebilly/ReDoc) के साथ वैकल्पिक API documentation।
|
||||
|
||||

|
||||
|
||||
### सिर्फ़ आधुनिक Python { #just-modern-python }
|
||||
|
||||
यह सब standard **Python type** declarations पर आधारित है (Pydantic की बदौलत)। सीखने के लिए कोई नया syntax नहीं। सिर्फ़ standard modern Python।
|
||||
|
||||
अगर आपको Python types का उपयोग कैसे करें, इसका 2 मिनट का छोटा refresher चाहिए (भले ही आप FastAPI का उपयोग न करते हों), तो छोटा tutorial देखें: [Python Types](python-types.md)।
|
||||
|
||||
आप types के साथ standard Python लिखते हैं:
|
||||
|
||||
```Python
|
||||
from datetime import date
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
# किसी वेरिएबल को str के रूप में घोषित करें
|
||||
# और फ़ंक्शन के अंदर एडिटर सपोर्ट पाएँ
|
||||
def main(user_id: str):
|
||||
return user_id
|
||||
|
||||
|
||||
# एक Pydantic मॉडल
|
||||
class User(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
joined: date
|
||||
```
|
||||
|
||||
फिर उसे इस तरह उपयोग किया जा सकता है:
|
||||
|
||||
```Python
|
||||
my_user: User = User(id=3, name="John Doe", joined="2018-07-19")
|
||||
|
||||
second_user_data = {
|
||||
"id": 4,
|
||||
"name": "Mary",
|
||||
"joined": "2018-11-30",
|
||||
}
|
||||
|
||||
my_second_user: User = User(**second_user_data)
|
||||
```
|
||||
|
||||
/// note | नोट
|
||||
|
||||
`**second_user_data` का मतलब है:
|
||||
|
||||
`second_user_data` dict की keys और values को सीधे key-value arguments के रूप में पास करें, जो इसके बराबर है: `User(id=4, name="Mary", joined="2018-11-30")`
|
||||
|
||||
///
|
||||
|
||||
### एडिटर सपोर्ट { #editor-support }
|
||||
|
||||
पूरे framework को उपयोग में आसान और सहज बनाने के लिए डिज़ाइन किया गया था, विकास शुरू करने से पहले ही सभी निर्णयों को कई editors पर test किया गया, ताकि सबसे अच्छा development experience सुनिश्चित किया जा सके।
|
||||
|
||||
Python developer surveys में, यह स्पष्ट है [कि सबसे अधिक उपयोग की जाने वाली सुविधाओं में से एक "autocompletion" है](https://www.jetbrains.com/research/python-developers-survey-2017/#tools-and-features)।
|
||||
|
||||
पूरा **FastAPI** framework इसे पूरा करने के लिए डिज़ाइन किया गया है। Autocompletion हर जगह काम करता है।
|
||||
|
||||
आपको शायद ही कभी docs पर वापस आने की आवश्यकता होगी।
|
||||
|
||||
यहाँ बताया गया है कि आपका editor आपकी कैसे मदद कर सकता है:
|
||||
|
||||
* [Visual Studio Code](https://code.visualstudio.com/) में:
|
||||
|
||||

|
||||
|
||||
* [PyCharm](https://www.jetbrains.com/pycharm/) में:
|
||||
|
||||

|
||||
|
||||
आपको ऐसे code में completion मिलेगा जिसे आप पहले असंभव भी मान सकते थे। जैसे, किसी request से आने वाले JSON body (जो nested भी हो सकता था) के अंदर `price` key।
|
||||
|
||||
अब गलत key names टाइप करने, docs के बीच आगे-पीछे जाने, या यह खोजने के लिए ऊपर-नीचे scroll करने की ज़रूरत नहीं कि आपने आखिर `username` उपयोग किया था या `user_name`।
|
||||
|
||||
### संक्षिप्त { #short }
|
||||
|
||||
हर चीज़ के लिए इसके समझदार **defaults** हैं, और हर जगह optional configurations हैं। सभी parameters को आपकी ज़रूरत के अनुसार और आपकी ज़रूरत की API define करने के लिए fine-tune किया जा सकता है।
|
||||
|
||||
लेकिन default रूप से, सब कुछ **"बस काम करता है"**।
|
||||
|
||||
### Validation { #validation }
|
||||
|
||||
* अधिकांश (या सभी?) Python **data types** के लिए validation, जिनमें शामिल हैं:
|
||||
* JSON objects (`dict`)।
|
||||
* JSON array (`list`) जो item types define करता है।
|
||||
* String (`str`) fields, जिनमें min और max lengths define होती हैं।
|
||||
* Numbers (`int`, `float`) जिनमें min और max values, आदि।
|
||||
|
||||
* अधिक असामान्य types के लिए validation, जैसे:
|
||||
* URL।
|
||||
* Email।
|
||||
* UUID।
|
||||
* ...और अन्य।
|
||||
|
||||
सारी validation well-established और robust **Pydantic** द्वारा handle की जाती है।
|
||||
|
||||
### Security और authentication { #security-and-authentication }
|
||||
|
||||
Security और authentication integrated हैं। Databases या data models के साथ किसी भी compromise के बिना।
|
||||
|
||||
OpenAPI में define की गई सभी security schemes, जिनमें शामिल हैं:
|
||||
|
||||
* HTTP Basic।
|
||||
* **OAuth2** (**JWT tokens** के साथ भी)। [OAuth2 with JWT](tutorial/security/oauth2-jwt.md) पर tutorial देखें।
|
||||
* API keys:
|
||||
* Headers में।
|
||||
* Query parameters में।
|
||||
* Cookies में, आदि।
|
||||
|
||||
साथ ही Starlette की सभी security features (जिसमें **session cookies** भी शामिल हैं)।
|
||||
|
||||
सब reusable tools और components के रूप में बनाए गए हैं, जिन्हें आपके systems, data stores, relational और NoSQL databases, आदि के साथ integrate करना आसान है।
|
||||
|
||||
### Dependency Injection { #dependency-injection }
|
||||
|
||||
FastAPI में एक बेहद आसान, लेकिन बेहद शक्तिशाली <dfn title='इन्हें "components", "resources", "services", "providers" भी कहा जाता है'><strong>Dependency Injection</strong></dfn> system शामिल है।
|
||||
|
||||
* Dependencies की भी dependencies हो सकती हैं, जिससे dependencies की hierarchy या **"graph"** बनता है।
|
||||
* सब कुछ framework द्वारा **automatically handled** होता है।
|
||||
* सभी dependencies requests से data मांग सकती हैं और **path operation** constraints तथा automatic documentation को बढ़ा सकती हैं।
|
||||
* Dependencies में define किए गए *path operation* parameters के लिए भी **automatic validation**।
|
||||
* जटिल user authentication systems, **database connections**, आदि के लिए support।
|
||||
* Databases, frontends, आदि के साथ **कोई compromise नहीं**। लेकिन उन सभी के साथ आसान integration।
|
||||
|
||||
### असीमित "plug-ins" { #unlimited-plug-ins }
|
||||
|
||||
या दूसरे शब्दों में, उनकी आवश्यकता नहीं है, अपनी ज़रूरत का code import करें और उपयोग करें।
|
||||
|
||||
किसी भी integration को (dependencies के साथ) उपयोग में इतना सरल बनाने के लिए डिज़ाइन किया गया है कि आप अपनी application के लिए उसी structure और syntax का उपयोग करके 2 lines of code में एक "plug-in" बना सकते हैं, जो आपके *path operations* के लिए उपयोग होता है।
|
||||
|
||||
### Tested { #tested }
|
||||
|
||||
* 100% <dfn title="code की वह मात्रा जो automatically test की जाती है">test coverage</dfn>।
|
||||
* 100% <dfn title="Python type annotations, जिनसे आपका editor और external tools आपको बेहतर support दे सकते हैं">type annotated</dfn> code base।
|
||||
* Production applications में उपयोग किया गया।
|
||||
|
||||
## Starlette की विशेषताएँ { #starlette-features }
|
||||
|
||||
**FastAPI** [**Starlette**](https://www.starlette.dev/) के साथ पूरी तरह compatible है (और उसी पर आधारित है)। इसलिए, आपके पास जो भी अतिरिक्त Starlette code है, वह भी काम करेगा।
|
||||
|
||||
`FastAPI` वास्तव में `Starlette` का एक sub-class है। इसलिए, अगर आप पहले से Starlette जानते हैं या उपयोग करते हैं, तो अधिकांश functionality उसी तरह काम करेगी।
|
||||
|
||||
**FastAPI** के साथ आपको **Starlette** की सभी features मिलती हैं (क्योंकि FastAPI मूलतः steroids पर Starlette है):
|
||||
|
||||
* सचमुच प्रभावशाली performance। यह [उपलब्ध सबसे तेज़ Python frameworks में से एक है, **NodeJS** और **Go** के बराबर](https://github.com/encode/starlette#performance)।
|
||||
* **WebSocket** support।
|
||||
* In-process background tasks।
|
||||
* Startup और shutdown events।
|
||||
* HTTPX पर बना test client।
|
||||
* **CORS**, GZip, Static Files, Streaming responses।
|
||||
* **Session और Cookie** support।
|
||||
* 100% test coverage।
|
||||
* 100% type annotated codebase।
|
||||
|
||||
## Pydantic की विशेषताएँ { #pydantic-features }
|
||||
|
||||
**FastAPI** [**Pydantic**](https://docs.pydantic.dev/) के साथ पूरी तरह compatible है (और उसी पर आधारित है)। इसलिए, आपके पास जो भी अतिरिक्त Pydantic code है, वह भी काम करेगा।
|
||||
|
||||
इसमें Pydantic पर आधारित external libraries भी शामिल हैं, जैसे databases के लिए <abbr title="Object-Relational Mapper - ऑब्जेक्ट-रिलेशनल मैपर">ORM</abbr>s और <abbr title="Object-Document Mapper - ऑब्जेक्ट-डॉक्यूमेंट मैपर">ODM</abbr>s।
|
||||
|
||||
इसका यह भी मतलब है कि कई मामलों में आप request से मिलने वाले उसी object को **सीधे database में** पास कर सकते हैं, क्योंकि सब कुछ automatically validated होता है।
|
||||
|
||||
उसी तरह उल्टा भी लागू होता है, कई मामलों में आप database से मिलने वाले object को **सीधे client को** पास कर सकते हैं।
|
||||
|
||||
**FastAPI** के साथ आपको **Pydantic** की सभी features मिलती हैं (क्योंकि FastAPI सभी data handling के लिए Pydantic पर आधारित है):
|
||||
|
||||
* **कोई brainfuck नहीं**:
|
||||
* सीखने के लिए कोई नई schema definition micro-language नहीं।
|
||||
* अगर आप Python types जानते हैं, तो आप जानते हैं कि Pydantic का उपयोग कैसे करना है।
|
||||
* आपके **<abbr title="Integrated Development Environment - इंटीग्रेटेड डेवलपमेंट एनवायरनमेंट: code editor जैसा">IDE</abbr>/<dfn title="एक program जो code errors की जाँच करता है">लिंटर</dfn>/brain** के साथ अच्छी तरह काम करता है:
|
||||
* क्योंकि pydantic data structures केवल उन classes के instances होते हैं जिन्हें आप define करते हैं; auto-completion, linting, mypy और आपकी intuition, सभी आपके validated data के साथ सही ढंग से काम करने चाहिए।
|
||||
* **Complex structures** validate करें:
|
||||
* Hierarchical Pydantic models, Python `typing` के `List` और `Dict`, आदि का उपयोग।
|
||||
* और validators complex data schemas को JSON Schema के रूप में स्पष्ट और आसानी से define, check और document करने देते हैं।
|
||||
* आपके पास deeply **nested JSON** objects हो सकते हैं और वे सभी validated और annotated हो सकते हैं।
|
||||
* **Extensible**:
|
||||
* Pydantic custom data types को define करने देता है या आप validator decorator से decorated model पर methods के साथ validation extend कर सकते हैं।
|
||||
* 100% test coverage।
|
||||
79
docs/hi/docs/history-design-future.md
Normal file
79
docs/hi/docs/history-design-future.md
Normal file
@@ -0,0 +1,79 @@
|
||||
# इतिहास, डिज़ाइन और भविष्य { #history-design-and-future }
|
||||
|
||||
कुछ समय पहले, [एक **FastAPI** उपयोगकर्ता ने पूछा](https://github.com/fastapi/fastapi/issues/3#issuecomment-454956920):
|
||||
|
||||
> इस प्रोजेक्ट का इतिहास क्या है? ऐसा लगता है कि यह कुछ ही हफ्तों में कहीं से भी सीधे शानदार बन गया [...]
|
||||
|
||||
यहाँ उस इतिहास का एक छोटा सा हिस्सा है।
|
||||
|
||||
## विकल्प { #alternatives }
|
||||
|
||||
मैं कई वर्षों से जटिल आवश्यकताओं वाली APIs बना रहा हूँ (Machine Learning, distributed systems, asynchronous jobs, NoSQL databases, आदि), और डेवलपर्स की कई टीमों का नेतृत्व कर चुका हूँ।
|
||||
|
||||
इसके हिस्से के रूप में, मुझे कई विकल्पों की जाँच, परीक्षण और उपयोग करना पड़ा।
|
||||
|
||||
**FastAPI** का इतिहास काफी हद तक इसके पूर्ववर्तियों का इतिहास है।
|
||||
|
||||
जैसा कि [विकल्प](alternatives.md) सेक्शन में कहा गया है:
|
||||
|
||||
<blockquote markdown="1">
|
||||
|
||||
दूसरों के पिछले काम के बिना **FastAPI** मौजूद नहीं होता।
|
||||
|
||||
इससे पहले कई टूल बनाए गए हैं जिन्होंने इसके निर्माण को प्रेरित करने में मदद की है।
|
||||
|
||||
मैं कई वर्षों से एक नया framework बनाने से बचता रहा। पहले मैंने **FastAPI** द्वारा कवर किए गए सभी features को कई अलग-अलग frameworks, plug-ins और tools का उपयोग करके हल करने की कोशिश की।
|
||||
|
||||
लेकिन किसी बिंदु पर, ऐसा कुछ बनाने के अलावा कोई विकल्प नहीं बचा था जो ये सभी features प्रदान करे, पिछले tools से सर्वोत्तम ideas ले और उन्हें सर्वोत्तम संभव तरीके से जोड़े, साथ ही ऐसी language features का उपयोग करे जो पहले उपलब्ध भी नहीं थीं (Python 3.6+ type hints)।
|
||||
|
||||
</blockquote>
|
||||
|
||||
## जाँच-पड़ताल { #investigation }
|
||||
|
||||
सभी पिछले विकल्पों का उपयोग करके मुझे उन सभी से सीखने, ideas लेने, और उन्हें अपने तथा जिन डेवलपर टीमों के साथ मैंने काम किया है उनके लिए सबसे अच्छे तरीके से मिलाने का अवसर मिला।
|
||||
|
||||
उदाहरण के लिए, यह स्पष्ट था कि आदर्श रूप से इसे standard Python type hints पर आधारित होना चाहिए।
|
||||
|
||||
साथ ही, सबसे अच्छा तरीका पहले से मौजूद standards का उपयोग करना था।
|
||||
|
||||
इसलिए, **FastAPI** की coding शुरू करने से पहले ही, मैंने OpenAPI, JSON Schema, OAuth2 आदि के specs का अध्ययन करने में कई महीने बिताए। उनके संबंध, overlap और differences को समझा।
|
||||
|
||||
## डिज़ाइन { #design }
|
||||
|
||||
फिर मैंने उस developer "API" को डिज़ाइन करने में कुछ समय लगाया जिसे मैं एक user के रूप में पाना चाहता था (FastAPI का उपयोग करने वाले developer के रूप में)।
|
||||
|
||||
मैंने सबसे लोकप्रिय Python editors में कई ideas का परीक्षण किया: PyCharm, VS Code, Jedi आधारित editors।
|
||||
|
||||
पिछले [Python Developer Survey](https://www.jetbrains.com/research/python-developers-survey-2018/#development-tools) के अनुसार, यह लगभग 80% users को कवर करता है।
|
||||
|
||||
इसका मतलब है कि **FastAPI** को विशेष रूप से उन editors के साथ test किया गया था जिनका उपयोग 80% Python developers करते हैं। और चूँकि अधिकांश अन्य editors भी समान तरीके से काम करते हैं, इसके सभी लाभ लगभग सभी editors के लिए काम करने चाहिए।
|
||||
|
||||
इस तरह मैं code duplication को जितना संभव हो उतना कम करने, हर जगह completion पाने, type और error checks आदि के सर्वोत्तम तरीके खोज सका।
|
||||
|
||||
सब कुछ इस तरह से किया गया कि सभी developers को सर्वोत्तम development experience मिल सके।
|
||||
|
||||
## आवश्यकताएँ { #requirements }
|
||||
|
||||
कई विकल्पों का परीक्षण करने के बाद, मैंने तय किया कि मैं इसके लाभों के लिए [**Pydantic**](https://docs.pydantic.dev/) का उपयोग करूँगा।
|
||||
|
||||
फिर मैंने इसमें योगदान दिया, ताकि इसे JSON Schema के साथ पूरी तरह compliant बनाया जा सके, constraint declarations को define करने के अलग-अलग तरीकों का समर्थन किया जा सके, और कई editors में tests के आधार पर editor support (type checks, autocompletion) को बेहतर बनाया जा सके।
|
||||
|
||||
Development के दौरान, मैंने [**Starlette**](https://www.starlette.dev/) में भी योगदान दिया, जो दूसरी मुख्य आवश्यकता थी।
|
||||
|
||||
## Development { #development }
|
||||
|
||||
जब तक मैंने **FastAPI** खुद बनाना शुरू किया, तब तक अधिकांश हिस्से पहले से तैयार थे, design तय हो चुका था, requirements और tools तैयार थे, और standards तथा specifications के बारे में ज्ञान स्पष्ट और ताज़ा था।
|
||||
|
||||
## भविष्य { #future }
|
||||
|
||||
इस बिंदु तक, यह पहले से ही स्पष्ट है कि **FastAPI** अपने ideas के साथ कई लोगों के लिए उपयोगी साबित हो रहा है।
|
||||
|
||||
इसे कई use cases के लिए बेहतर उपयुक्त होने के कारण पिछले विकल्पों पर चुना जा रहा है।
|
||||
|
||||
कई developers और teams अपने projects के लिए पहले से ही **FastAPI** पर निर्भर हैं (मेरे और मेरी team सहित)।
|
||||
|
||||
लेकिन फिर भी, अभी कई improvements और features आने बाकी हैं।
|
||||
|
||||
**FastAPI** का भविष्य बहुत उज्ज्वल है।
|
||||
|
||||
और [आपकी मदद](help-fastapi.md) की बहुत सराहना की जाती है।
|
||||
585
docs/hi/docs/index.md
Normal file
585
docs/hi/docs/index.md
Normal file
@@ -0,0 +1,585 @@
|
||||
---
|
||||
include_yaml:
|
||||
sponsors: data/sponsors.yml
|
||||
---
|
||||
|
||||
# FastAPI { #fastapi }
|
||||
|
||||
<style>
|
||||
.md-content .md-typeset h1 { display: none; }
|
||||
</style>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://fastapi.tiangolo.com/hi"><img src="https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" alt="FastAPI"></a>
|
||||
</p>
|
||||
<p align="center">
|
||||
<em>FastAPI फ़्रेमवर्क, उच्च प्रदर्शन, सीखने में आसान, कोड लिखने में तेज़, प्रोडक्शन के लिए तैयार</em>
|
||||
</p>
|
||||
<p align="center">
|
||||
<a href="https://github.com/fastapi/fastapi/actions?query=workflow%3ATest+event%3Apush+branch%3Amaster">
|
||||
<img src="https://github.com/fastapi/fastapi/actions/workflows/test.yml/badge.svg?event=push&branch=master" alt="टेस्ट">
|
||||
</a>
|
||||
<a href="https://coverage-badge.samuelcolvin.workers.dev/redirect/fastapi/fastapi">
|
||||
<img src="https://coverage-badge.samuelcolvin.workers.dev/fastapi/fastapi.svg" alt="कवरेज">
|
||||
</a>
|
||||
<a href="https://pypi.org/project/fastapi">
|
||||
<img src="https://img.shields.io/pypi/v/fastapi?color=%2334D058&label=pypi%20package" alt="पैकेज संस्करण">
|
||||
</a>
|
||||
<a href="https://pypi.org/project/fastapi">
|
||||
<img src="https://img.shields.io/pypi/pyversions/fastapi.svg?color=%2334D058" alt="समर्थित Python संस्करण">
|
||||
</a>
|
||||
</p>
|
||||
|
||||
---
|
||||
|
||||
**दस्तावेज़**: [https://fastapi.tiangolo.com](https://fastapi.tiangolo.com/hi)
|
||||
|
||||
**स्रोत कोड**: [https://github.com/fastapi/fastapi](https://github.com/fastapi/fastapi)
|
||||
|
||||
---
|
||||
|
||||
FastAPI एक आधुनिक, तेज़ (उच्च-प्रदर्शन) वेब फ़्रेमवर्क है जो मानक Python type hints के आधार पर Python से APIs बनाने के लिए है।
|
||||
|
||||
मुख्य विशेषताएँ:
|
||||
|
||||
* **तेज़**: बहुत उच्च प्रदर्शन, **NodeJS** और **Go** के समकक्ष (Starlette और Pydantic की बदौलत)। [उपलब्ध सबसे तेज़ Python फ़्रेमवर्क्स में से एक](#performance)।
|
||||
* **कोड लिखने में तेज़**: फ़ीचर्स विकसित करने की गति लगभग 200% से 300% तक बढ़ाएँ। *
|
||||
* **कम बग्स**: मानवीय (डेवलपर) त्रुटियों में लगभग 40% की कमी। *
|
||||
* **सहज**: बेहतरीन एडिटर सपोर्ट। हर जगह <dfn title="उर्फ़: ऑटो-कम्प्लीट, ऑटोकम्प्लीशन, IntelliSense">ऑटो-कम्प्लीट</dfn>। डिबगिंग में कम समय।
|
||||
* **आसान**: इस्तेमाल और सीखने में आसान। दस्तावेज़ पढ़ने में कम समय।
|
||||
* **संक्षिप्त**: कोड डुप्लीकेशन को न्यूनतम करें। प्रत्येक parameter declaration से कई फ़ीचर्स। कम बग्स।
|
||||
* **मजबूत**: प्रोडक्शन-रेडी कोड प्राप्त करें। स्वतः इंटरैक्टिव दस्तावेज़ीकरण के साथ।
|
||||
* **मानकों पर आधारित**: APIs के खुले मानकों पर आधारित (और पूर्णतः अनुकूल): [OpenAPI](https://github.com/OAI/OpenAPI-Specification) (जिसे पहले Swagger कहा जाता था) और [JSON Schema](https://json-schema.org/)।
|
||||
|
||||
<small>* आंतरिक डेवलपमेंट टीम द्वारा प्रोडक्शन ऐप्स बनाते समय किए गए परीक्षणों के आधार पर अनुमान।</small>
|
||||
|
||||
## प्रायोजक { #sponsors }
|
||||
|
||||
<!-- sponsors -->
|
||||
|
||||
### कीस्टोन प्रायोजक { #keystone-sponsor }
|
||||
|
||||
<div class="fastapi-sponsors fastapi-sponsors--keystone">
|
||||
{% for sponsor in sponsors.keystone -%}
|
||||
<a class="fastapi-sponsors__card fastapi-sponsors__card--keystone" href="{{ sponsor.url }}" title="{{ sponsor.title }}"><img class="fastapi-sponsors__banner" src="{{ sponsor.img }}" alt="{{ sponsor.title }}"></a>
|
||||
{% endfor -%}
|
||||
</div>
|
||||
|
||||
### गोल्ड प्रायोजक { #gold-sponsors }
|
||||
|
||||
<div class="fastapi-sponsors fastapi-sponsors--gold">
|
||||
{% for sponsor in sponsors.gold -%}
|
||||
<a class="fastapi-sponsors__card fastapi-sponsors__card--gold" href="{{ sponsor.url }}" title="{{ sponsor.title }}"><img class="fastapi-sponsors__banner" src="{{ sponsor.img }}" alt="{{ sponsor.title }}" loading="lazy"></a>
|
||||
{% endfor -%}
|
||||
</div>
|
||||
|
||||
### सिल्वर प्रायोजक { #silver-sponsors }
|
||||
|
||||
<div class="fastapi-sponsors fastapi-sponsors--silver">
|
||||
{% for sponsor in sponsors.silver -%}
|
||||
<a class="fastapi-sponsors__card fastapi-sponsors__card--silver" href="{{ sponsor.url }}" title="{{ sponsor.title }}"><img class="fastapi-sponsors__banner" src="{{ sponsor.img }}" alt="{{ sponsor.title }}" loading="lazy"></a>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
<!-- /sponsors -->
|
||||
|
||||
[अन्य प्रायोजक](https://fastapi.tiangolo.com/hi/fastapi-people/#sponsors)
|
||||
|
||||
## विचार { #opinions }
|
||||
|
||||
<!-- only-mkdocs -->
|
||||
<div class="fastapi-opinions" data-fastapi-opinions>
|
||||
<div class="fastapi-opinions__tabs" role="tablist" aria-label="Companies using FastAPI">
|
||||
<button class="fastapi-opinions__tab" role="tab" type="button" id="fo-tab-microsoft" aria-controls="fo-panel-microsoft" aria-selected="true" tabindex="0">
|
||||
<span class="fastapi-opinions__mark"><img src="/img/logos/microsoft.svg" alt="Microsoft" loading="lazy"></span>
|
||||
</button>
|
||||
<button class="fastapi-opinions__tab" role="tab" type="button" id="fo-tab-uber" aria-controls="fo-panel-uber" aria-selected="false" tabindex="-1">
|
||||
<span class="fastapi-opinions__mark"><img src="/img/logos/uber.svg" alt="Uber" loading="lazy"></span>
|
||||
</button>
|
||||
<button class="fastapi-opinions__tab" role="tab" type="button" id="fo-tab-netflix" aria-controls="fo-panel-netflix" aria-selected="false" tabindex="-1">
|
||||
<span class="fastapi-opinions__mark"><img src="/img/logos/netflix.svg" alt="Netflix" loading="lazy"></span>
|
||||
</button>
|
||||
<button class="fastapi-opinions__tab" role="tab" type="button" id="fo-tab-cisco" aria-controls="fo-panel-cisco" aria-selected="false" tabindex="-1">
|
||||
<span class="fastapi-opinions__mark"><img src="/img/logos/cisco.svg" alt="Cisco" loading="lazy"></span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="fastapi-opinions__panel" id="fo-panel-microsoft" role="tabpanel" aria-labelledby="fo-tab-microsoft" tabindex="0">
|
||||
<blockquote class="fastapi-opinions__quote">"मैं इन दिनों <strong>FastAPI</strong> का बहुत उपयोग कर रहा/रही हूँ। वास्तव में मैं अपनी टीम की <strong>Microsoft में ML सेवाओं</strong> के लिए इसे उपयोग करने की योजना बना रहा/रही हूँ। इनमें से कुछ को मुख्य <strong>Windows</strong> प्रोडक्ट और कुछ <strong>Office</strong> प्रोडक्ट्स में इंटीग्रेट किया जा रहा है।"</blockquote>
|
||||
<div class="fastapi-opinions__attr">— कबीर खान, <strong>Microsoft</strong> <a href="https://github.com/fastapi/fastapi/pull/26">(संदर्भ)</a></div>
|
||||
</div>
|
||||
<div class="fastapi-opinions__panel" id="fo-panel-uber" role="tabpanel" aria-labelledby="fo-tab-uber" tabindex="0" hidden>
|
||||
<blockquote class="fastapi-opinions__quote">"हमने <strong>FastAPI</strong> लाइब्रेरी अपनाई ताकि एक <strong>REST</strong> सर्वर स्पॉन किया जा सके जिसे <strong>अन्दाज़ों/अनुमानों</strong> को प्राप्त करने के लिए क्वेरी किया जा सके।" <em>[Ludwig के लिए]</em></blockquote>
|
||||
<div class="fastapi-opinions__attr">— पिएरो मोलिनो, यारोस्लाव डुडिन, साई सुमंत मिर्याला, <strong>Uber</strong> <a href="https://eng.uber.com/ludwig-v0-2/">(संदर्भ)</a></div>
|
||||
</div>
|
||||
<div class="fastapi-opinions__panel" id="fo-panel-netflix" role="tabpanel" aria-labelledby="fo-tab-netflix" tabindex="0" hidden>
|
||||
<blockquote class="fastapi-opinions__quote">"<strong>Netflix</strong> हमारे <strong>संकट प्रबंधन</strong> ऑर्केस्ट्रेशन फ़्रेमवर्क: <strong>Dispatch</strong> के ओपन-सोर्स रिलीज़ की घोषणा करते हुए प्रसन्न है!" <em>[FastAPI के साथ बनाया गया]</em></blockquote>
|
||||
<div class="fastapi-opinions__attr">— केविन ग्लिसन, मार्क विलानोवा, फॉरेस्ट मॉन्सेन, <strong>Netflix</strong> <a href="https://netflixtechblog.com/introducing-dispatch-da4b8a2a8072">(संदर्भ)</a></div>
|
||||
</div>
|
||||
<div class="fastapi-opinions__panel" id="fo-panel-cisco" role="tabpanel" aria-labelledby="fo-tab-cisco" tabindex="0" hidden>
|
||||
<blockquote class="fastapi-opinions__quote">"यदि कोई प्रोडक्शन Python API बनाना चाहता है, तो मैं <strong>FastAPI</strong> की अत्यधिक अनुशंसा करूंगा/करूंगी। यह <strong>सुंदरता से डिज़ाइन</strong> किया गया है, <strong>उपयोग में सरल</strong> है और <strong>बेहद स्केलेबल</strong> है — यह हमारी API-फर्स्ट डेवलपमेंट रणनीति का <strong>मुख्य घटक</strong> बन गया है।"</blockquote>
|
||||
<div class="fastapi-opinions__attr">— डीयोन पिल्सबरी, <strong>Cisco</strong> <a href="https://www.linkedin.com/posts/deonpillsbury_cisco-cx-python-activity-6963242628536487936-trAp/">(संदर्भ)</a></div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- /only-mkdocs -->
|
||||
|
||||
<div class="only-github" markdown="1">
|
||||
|
||||
"_[...] मैं इन दिनों **FastAPI** का बहुत उपयोग कर रहा/रही हूँ। [...] वास्तव में मैं अपनी टीम की **Microsoft में ML सेवाओं** के लिए इसे उपयोग करने की योजना बना रहा/रही हूँ। इनमें से कुछ को मुख्य **Windows** प्रोडक्ट और कुछ **Office** प्रोडक्ट्स में इंटीग्रेट किया जा रहा है._"
|
||||
|
||||
<div style="text-align: right; margin-right: 10%;">कबीर खान - <strong>Microsoft</strong> <a href="https://github.com/fastapi/fastapi/pull/26"><small>(संदर्भ)</small></a></div>
|
||||
|
||||
---
|
||||
|
||||
"_हमने **FastAPI** लाइब्रेरी अपनाई ताकि एक **REST** सर्वर स्पॉन किया जा सके जिसे **अनुमानों** को प्राप्त करने के लिए क्वेरी किया जा सके। [Ludwig के लिए]_"
|
||||
|
||||
<div style="text-align: right; margin-right: 10%;">पिएरो मोलिनो, यारोस्लाव डुडिन, और साई सुमंत मिर्याला - <strong>Uber</strong> <a href="https://eng.uber.com/ludwig-v0-2/"><small>(संदर्भ)</small></a></div>
|
||||
|
||||
---
|
||||
|
||||
"_**Netflix** हमारे **संकट प्रबंधन** ऑर्केस्ट्रेशन फ़्रेमवर्क: **Dispatch** के ओपन-सोर्स रिलीज़ की घोषणा करते हुए प्रसन्न है! [**FastAPI** के साथ बनाया गया]_"
|
||||
|
||||
<div style="text-align: right; margin-right: 10%;">केविन ग्लिसन, मार्क विलानोवा, फॉरेस्ट मॉन्सेन - <strong>Netflix</strong> <a href="https://netflixtechblog.com/introducing-dispatch-da4b8a2a8072"><small>(संदर्भ)</small></a></div>
|
||||
|
||||
---
|
||||
|
||||
"_यदि कोई प्रोडक्शन Python API बनाना चाहता है, तो मैं **FastAPI** की अत्यधिक अनुशंसा करूंगा/करूंगी। यह **सुंदरता से डिज़ाइन** किया गया है, **उपयोग में सरल** है और **बेहद स्केलेबल** है, यह हमारी API-फ़र्स्ट डेवलपमेंट रणनीति का **मुख्य घटक** बन गया है और हमारे Virtual TAC Engineer जैसे कई ऑटोमेशन्स और सेवाओं को चला रहा है._"
|
||||
|
||||
<div style="text-align: right; margin-right: 10%;">डीयोन पिल्सबरी - <strong>Cisco</strong> <a href="https://www.linkedin.com/posts/deonpillsbury_cisco-cx-python-activity-6963242628536487936-trAp/"><small>(संदर्भ)</small></a></div>
|
||||
|
||||
---
|
||||
|
||||
</div>
|
||||
|
||||
## FastAPI कॉन्फ़ { #fastapi-conf }
|
||||
|
||||
[**FastAPI Conf '26**](https://fastapiconf.com) **28 अक्टूबर, 2026** को **एम्स्टर्डम, नीदरलैंड्स** में हो रही है। सब कुछ FastAPI के बारे में, सीधे स्रोत से। 🎤
|
||||
|
||||
<a class="fastapi-feature-banner" href="https://fastapiconf.com"><img src="https://fastapi.tiangolo.com/img/fastapi-conf.jpeg" alt="FastAPI Conf '26 - 28 अक्टूबर, 2026 - एम्स्टर्डम, NL"></a>
|
||||
|
||||
## FastAPI मिनी डॉक्यूमेंट्री { #fastapi-mini-documentary }
|
||||
|
||||
साल 2025 के अंत में एक [FastAPI मिनी डॉक्यूमेंट्री](https://www.youtube.com/watch?v=mpR8ngthqiE) रिलीज़ हुई, आप इसे ऑनलाइन देख सकते हैं:
|
||||
|
||||
<a class="fastapi-feature-banner" href="https://www.youtube.com/watch?v=mpR8ngthqiE"><img src="https://fastapi.tiangolo.com/img/fastapi-documentary.jpg" alt="FastAPI मिनी डॉक्यूमेंट्री"></a>
|
||||
|
||||
## **Typer**, CLIs का FastAPI { #typer-the-fastapi-of-clis }
|
||||
|
||||
<a href="https://typer.tiangolo.com"><img src="https://typer.tiangolo.com/img/logo-margin/logo-margin-vector.svg" style="width: 20%;"></a>
|
||||
|
||||
यदि आप वेब API के बजाय टर्मिनल में उपयोग होने वाला <abbr title="Command Line Interface - आदेश पंक्ति इंटरफ़ेस">CLI</abbr> ऐप बना रहे हैं, तो [**Typer**](https://typer.tiangolo.com/) देखें।
|
||||
|
||||
**Typer**, FastAPI का छोटा भाई/बहन है। और इसका उद्देश्य **CLIs का FastAPI** होना है। ⌨️ 🚀
|
||||
|
||||
## आवश्यकताएँ { #requirements }
|
||||
|
||||
FastAPI दिग्गजों के कंधों पर खड़ा है:
|
||||
|
||||
* वेब हिस्सों के लिए [Starlette](https://www.starlette.dev/)।
|
||||
* डेटा हिस्सों के लिए [Pydantic](https://docs.pydantic.dev/)।
|
||||
|
||||
## स्थापना { #installation }
|
||||
|
||||
एक [वर्चुअल एन्वायरनमेंट](https://fastapi.tiangolo.com/hi/virtual-environments/) बनाएँ और सक्रिय करें, और फिर FastAPI स्थापित करें:
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ pip install "fastapi[standard]"
|
||||
|
||||
---> 100%
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
**नोट**: सुनिश्चित करें कि आप सभी टर्मिनलों में काम करने के लिए `"fastapi[standard]"` को उद्धरण-चिह्नों में रखें।
|
||||
|
||||
## उदाहरण { #example }
|
||||
|
||||
### इसे बनाएँ { #create-it }
|
||||
|
||||
`main.py` फ़ाइल बनाएँ और इसमें लिखें:
|
||||
|
||||
```Python
|
||||
from fastapi import FastAPI
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"Hello": "World"}
|
||||
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
def read_item(item_id: int, q: str | None = None):
|
||||
return {"item_id": item_id, "q": q}
|
||||
```
|
||||
|
||||
<details markdown="1">
|
||||
<summary>या <code>async def</code> का उपयोग करें...</summary>
|
||||
|
||||
यदि आपका कोड `async` / `await` का उपयोग करता है, तो `async def` का उपयोग करें:
|
||||
|
||||
```Python hl_lines="7 12"
|
||||
from fastapi import FastAPI
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def read_root():
|
||||
return {"Hello": "World"}
|
||||
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_item(item_id: int, q: str | None = None):
|
||||
return {"item_id": item_id, "q": q}
|
||||
```
|
||||
|
||||
**नोट**:
|
||||
|
||||
यदि आप नहीं जानते, तो _"जल्दी में?"_ सेक्शन देखें: दस्तावेज़ में [`async` और `await`](https://fastapi.tiangolo.com/hi/async/#in-a-hurry) के बारे में।
|
||||
|
||||
</details>
|
||||
|
||||
### इसे चलाएँ { #run-it }
|
||||
|
||||
सर्वर को इस कमांड से चलाएँ:
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ fastapi dev
|
||||
|
||||
╭────────── FastAPI CLI - Development mode ───────────╮
|
||||
│ │
|
||||
│ Serving at: http://127.0.0.1:8000 │
|
||||
│ │
|
||||
│ API docs: http://127.0.0.1:8000/docs │
|
||||
│ │
|
||||
│ Running in development mode, for production use: │
|
||||
│ │
|
||||
│ fastapi run │
|
||||
│ │
|
||||
╰─────────────────────────────────────────────────────╯
|
||||
|
||||
INFO: Will watch for changes in these directories: ['/home/user/code/awesomeapp']
|
||||
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
||||
INFO: Started reloader process [2248755] using WatchFiles
|
||||
INFO: Started server process [2248757]
|
||||
INFO: Waiting for application startup.
|
||||
INFO: Application startup complete.
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
<details markdown="1">
|
||||
<summary><code>fastapi dev</code> कमांड के बारे में...</summary>
|
||||
|
||||
`fastapi dev` कमांड आपका `main.py` फ़ाइल स्वतः पढ़ता है, उसमें **FastAPI** ऐप का पता लगाता है, और [Uvicorn](https://www.uvicorn.dev) का उपयोग करके सर्वर शुरू करता है।
|
||||
|
||||
डिफ़ॉल्ट रूप से, `fastapi dev` लोकल डेवलपमेंट के लिए auto-reload सक्षम करके शुरू होगा।
|
||||
|
||||
आप इसके बारे में और पढ़ सकते हैं: [FastAPI CLI दस्तावेज़](https://fastapi.tiangolo.com/hi/fastapi-cli/) में।
|
||||
|
||||
</details>
|
||||
|
||||
### इसे जाँचें { #check-it }
|
||||
|
||||
अपने ब्राउज़र में [http://127.0.0.1:8000/items/5?q=somequery](http://127.0.0.1:8000/items/5?q=somequery) खोलें।
|
||||
|
||||
आपको JSON प्रतिक्रिया इस प्रकार दिखेगी:
|
||||
|
||||
```JSON
|
||||
{"item_id": 5, "q": "somequery"}
|
||||
```
|
||||
|
||||
आपने पहले ही एक API बना ली है जो:
|
||||
|
||||
* _paths_ `/` और `/items/{item_id}` पर HTTP अनुरोध स्वीकार करती है।
|
||||
* दोनों _paths_ `GET` <em>operations</em> लेती हैं (जिन्हें HTTP _methods_ भी कहा जाता है)।
|
||||
* _path_ `/items/{item_id}` में एक _path parameter_ `item_id` है जो `int` होना चाहिए।
|
||||
* _path_ `/items/{item_id}` में एक वैकल्पिक `str` _query parameter_ `q` है।
|
||||
|
||||
### इंटरैक्टिव API दस्तावेज़ { #interactive-api-docs }
|
||||
|
||||
अब [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) पर जाएँ।
|
||||
|
||||
आपको स्वचालित इंटरैक्टिव API दस्तावेज़ीकरण दिखेगा (जो [Swagger UI](https://github.com/swagger-api/swagger-ui) द्वारा प्रदान किया जाता है):
|
||||
|
||||

|
||||
|
||||
### वैकल्पिक API दस्तावेज़ { #alternative-api-docs }
|
||||
|
||||
और अब, [http://127.0.0.1:8000/redoc](http://127.0.0.1:8000/redoc) पर जाएँ।
|
||||
|
||||
आपको वैकल्पिक स्वचालित दस्तावेज़ीकरण दिखेगा (जो [ReDoc](https://github.com/Rebilly/ReDoc) द्वारा प्रदान किया जाता है):
|
||||
|
||||

|
||||
|
||||
## उदाहरण उन्नयन { #example-upgrade }
|
||||
|
||||
अब `PUT` अनुरोध से body प्राप्त करने के लिए `main.py` फ़ाइल संशोधित करें।
|
||||
|
||||
Pydantic की बदौलत, body को मानक Python प्रकारों से घोषित करें।
|
||||
|
||||
```Python hl_lines="2 7-10 23-25"
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
price: float
|
||||
is_offer: bool | None = None
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"Hello": "World"}
|
||||
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
def read_item(item_id: int, q: str | None = None):
|
||||
return {"item_id": item_id, "q": q}
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
def update_item(item_id: int, item: Item):
|
||||
return {"item_name": item.name, "item_id": item_id}
|
||||
```
|
||||
|
||||
`fastapi dev` सर्वर स्वतः रीलोड होना चाहिए।
|
||||
|
||||
### इंटरैक्टिव API दस्तावेज़ उन्नयन { #interactive-api-docs-upgrade }
|
||||
|
||||
अब [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) पर जाएँ।
|
||||
|
||||
* इंटरैक्टिव API दस्तावेज़ स्वतः अपडेट हो जाएगा, नए body सहित:
|
||||
|
||||

|
||||
|
||||
* "Try it out" बटन पर क्लिक करें, यह आपको parameters भरने और सीधे API के साथ इंटरेक्ट करने की अनुमति देता है:
|
||||
|
||||

|
||||
|
||||
* फिर "Execute" बटन पर क्लिक करें, यूज़र इंटरफ़ेस आपकी API से संवाद करेगा, parameters भेजेगा, परिणाम प्राप्त करेगा और उन्हें स्क्रीन पर दिखाएगा:
|
||||
|
||||

|
||||
|
||||
### वैकल्पिक API दस्तावेज़ उन्नयन { #alternative-api-docs-upgrade }
|
||||
|
||||
और अब, [http://127.0.0.1:8000/redoc](http://127.0.0.1:8000/redoc) पर जाएँ।
|
||||
|
||||
* वैकल्पिक दस्तावेज़ भी नए query parameter और body को दर्शाएगा:
|
||||
|
||||

|
||||
|
||||
### पुनरावलोकन { #recap }
|
||||
|
||||
संक्षेप में, आप parameters, body, आदि के प्रकार फ़ंक्शन parameters के रूप में **एक बार** घोषित करते हैं।
|
||||
|
||||
आप यह मानक आधुनिक Python प्रकारों से करते हैं।
|
||||
|
||||
आपको किसी नई सिंटैक्स, किसी विशेष लाइब्रेरी के methods या classes, आदि सीखने की आवश्यकता नहीं है।
|
||||
|
||||
बस मानक **Python**।
|
||||
|
||||
उदाहरण के लिए, एक `int` के लिए:
|
||||
|
||||
```Python
|
||||
item_id: int
|
||||
```
|
||||
|
||||
या एक अधिक जटिल `Item` मॉडल के लिए:
|
||||
|
||||
```Python
|
||||
item: Item
|
||||
```
|
||||
|
||||
...और केवल उसी एक घोषणा के साथ आपको मिलता है:
|
||||
|
||||
* एडिटर सपोर्ट, जिसमें शामिल है:
|
||||
* कम्प्लीशन।
|
||||
* प्रकार जाँच।
|
||||
* डेटा का वैधीकरण:
|
||||
* जब डेटा अमान्य हो तो स्वतः और स्पष्ट त्रुटियाँ।
|
||||
* गहराई से nested JSON objects के लिए भी वैधीकरण।
|
||||
* इनपुट डेटा का <dfn title="उर्फ़: सीरियलाइज़ेशन, पार्सिंग, मार्शलिंग">रूपांतरण</dfn>: नेटवर्क से Python डेटा और प्रकारों में। इनमें से पढ़ना:
|
||||
* JSON।
|
||||
* Path parameters।
|
||||
* Query parameters।
|
||||
* Cookies।
|
||||
* Headers।
|
||||
* Forms।
|
||||
* Files।
|
||||
* आउटपुट डेटा का <dfn title="उर्फ़: सीरियलाइज़ेशन, पार्सिंग, मार्शलिंग">रूपांतरण</dfn>: Python डेटा और प्रकारों से नेटवर्क डेटा (JSON के रूप में) में:
|
||||
* Python प्रकारों का रूपांतरण (`str`, `int`, `float`, `bool`, `list`, आदि)।
|
||||
* `datetime` ऑब्जेक्ट्स।
|
||||
* `UUID` ऑब्जेक्ट्स।
|
||||
* डेटाबेस मॉडल्स।
|
||||
* ...और बहुत कुछ।
|
||||
* स्वचालित इंटरैक्टिव API दस्तावेज़ीकरण, जिनमें 2 वैकल्पिक यूज़र इंटरफ़ेस शामिल हैं:
|
||||
* Swagger UI।
|
||||
* ReDoc।
|
||||
|
||||
---
|
||||
|
||||
पिछले कोड उदाहरण पर लौटते हुए, **FastAPI** यह करेगा:
|
||||
|
||||
* `GET` और `PUT` अनुरोधों के लिए path में `item_id` है, यह सत्यापित करेगा।
|
||||
* `GET` और `PUT` अनुरोधों के लिए `item_id` का प्रकार `int` है, यह सत्यापित करेगा।
|
||||
* यदि नहीं है, तो क्लाइंट को एक उपयोगी, स्पष्ट त्रुटि दिखाई देगी।
|
||||
* `GET` अनुरोधों के लिए यह जाँच करेगा कि `q` नाम का एक वैकल्पिक query parameter है (जैसे `http://127.0.0.1:8000/items/foo?q=somequery`)।
|
||||
* क्योंकि `q` parameter `= None` के साथ घोषित है, यह वैकल्पिक है।
|
||||
* `None` के बिना यह आवश्यक होता (जैसे `PUT` के मामले में body आवश्यक है)।
|
||||
* `/items/{item_id}` पर `PUT` अनुरोधों के लिए, body को JSON के रूप में पढ़ेगा:
|
||||
* यह जाँचेगा कि एक आवश्यक attribute `name` है जो `str` होना चाहिए।
|
||||
* यह जाँचेगा कि एक आवश्यक attribute `price` है जो `float` होना चाहिए।
|
||||
* यह जाँचेगा कि एक वैकल्पिक attribute `is_offer` है, जो यदि मौजूद है तो `bool` होना चाहिए।
|
||||
* यह सब गहराई से nested JSON objects के लिए भी काम करेगा।
|
||||
* JSON से और JSON में स्वतः रूपांतरण।
|
||||
* हर चीज़ को OpenAPI के साथ दस्तावेज़ित करेगा, जिसे निम्न द्वारा उपयोग किया जा सकता है:
|
||||
* इंटरैक्टिव दस्तावेज़ीकरण प्रणालियाँ।
|
||||
* कई भाषाओं के लिए स्वचालित क्लाइंट कोड जनरेशन प्रणालियाँ।
|
||||
* सीधे 2 इंटरैक्टिव दस्तावेज़ीकरण वेब इंटरफेसेज़ प्रदान करेगा।
|
||||
|
||||
---
|
||||
|
||||
हमने केवल सतह को छुआ है, लेकिन आपको पहले ही समझ आ गया होगा कि यह सब कैसे काम करता है।
|
||||
|
||||
इस पंक्ति को बदलकर देखें:
|
||||
|
||||
```Python
|
||||
return {"item_name": item.name, "item_id": item_id}
|
||||
```
|
||||
|
||||
...यहाँ से:
|
||||
|
||||
```Python
|
||||
... "item_name": item.name ...
|
||||
```
|
||||
|
||||
...यहाँ तक:
|
||||
|
||||
```Python
|
||||
... "item_price": item.price ...
|
||||
```
|
||||
|
||||
...और देखें कि आपका एडिटर attributes को कैसे auto-complete करेगा और उनके प्रकार जानेगा:
|
||||
|
||||

|
||||
|
||||
अधिक फ़ीचर्स सहित एक अधिक सम्पूर्ण उदाहरण के लिए, <a href="https://fastapi.tiangolo.com/hi/tutorial/">ट्यूटोरियल - यूज़र गाइड</a> देखें।
|
||||
|
||||
**स्पॉइलर अलर्ट**: ट्यूटोरियल - यूज़र गाइड में शामिल है:
|
||||
|
||||
* विभिन्न स्थानों से **parameters** की घोषणा: **headers**, **cookies**, **form fields** और **files**।
|
||||
* `maximum_length` या `regex` जैसी **validation constraints** कैसे सेट करें।
|
||||
* एक बहुत शक्तिशाली और उपयोग में आसान **<dfn title="उर्फ़: कॉम्पोनेंट्स, रिसोर्सेज़, प्रोवाइडर्स, सर्विसेज़, इंजेक्टेबल्स">डिपेंडेंसी इंजेक्शन</dfn>** सिस्टम।
|
||||
* सुरक्षा और प्रमाणीकरण, जिसमें **OAuth2** के साथ **JWT tokens** और **HTTP Basic** auth का समर्थन शामिल है।
|
||||
* **गहराई से nested JSON मॉडल्स** घोषित करने की अधिक उन्नत (पर समान रूप से आसान) तकनीकें (Pydantic की बदौलत)।
|
||||
* [Strawberry](https://strawberry.rocks) और अन्य लाइब्रेरीज़ के साथ **GraphQL** एकीकरण।
|
||||
* कई अतिरिक्त फ़ीचर्स (Starlette की बदौलत) जैसे:
|
||||
* **WebSockets**
|
||||
* HTTPX और `pytest` पर आधारित अत्यंत आसान टेस्ट्स
|
||||
* **CORS**
|
||||
* **Cookie Sessions**
|
||||
* ...आदि।
|
||||
|
||||
### अपनी ऐप परिनियोजित करें (वैकल्पिक) { #deploy-your-app-optional }
|
||||
|
||||
आप वैकल्पिक रूप से अपनी FastAPI ऐप को [FastAPI Cloud](https://fastapicloud.com) पर एक ही कमांड से डिप्लॉय कर सकते हैं। 🚀
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ fastapi deploy
|
||||
|
||||
Deploying to FastAPI Cloud...
|
||||
|
||||
✅ Deployment successful!
|
||||
|
||||
🐔 Ready the chicken! Your app is ready at https://myapp.fastapicloud.dev
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
CLI आपकी FastAPI एप्लिकेशन को स्वतः पहचान लेगा और उसे क्लाउड पर डिप्लॉय करेगा। यदि आप logged in नहीं हैं, तो प्रमाणीकरण प्रक्रिया पूरी करने के लिए आपका ब्राउज़र खुलेगा।
|
||||
|
||||
बस इतना ही! अब आप उस URL पर अपनी ऐप एक्सेस कर सकते हैं। ✨
|
||||
|
||||
#### FastAPI Cloud के बारे में { #about-fastapi-cloud }
|
||||
|
||||
**[FastAPI Cloud](https://fastapicloud.com)** को **FastAPI** के ही लेखक और टीम ने बनाया है।
|
||||
|
||||
यह न्यूनतम प्रयास में किसी API को **बनाने**, **डिप्लॉय** करने और **एक्सेस** करने की प्रक्रिया को सरल बनाता है।
|
||||
|
||||
यह FastAPI के साथ ऐप्स बनाने के उसी **डेवलपर अनुभव** को उन्हें क्लाउड में **डिप्लॉय** करने तक लाता है। 🎉
|
||||
|
||||
FastAPI Cloud, *FastAPI and friends* ओपन सोर्स प्रोजेक्ट्स के लिए मुख्य प्रायोजक और फंडिंग प्रदाता है। ✨
|
||||
|
||||
#### अन्य क्लाउड प्रदाताओं पर डिप्लॉय करें { #deploy-to-other-cloud-providers }
|
||||
|
||||
FastAPI ओपन सोर्स है और मानकों पर आधारित है। आप FastAPI ऐप्स को किसी भी क्लाउड प्रदाता पर डिप्लॉय कर सकते हैं।
|
||||
|
||||
अपने क्लाउड प्रदाता के गाइड्स का पालन करें और उनके साथ FastAPI ऐप्स डिप्लॉय करें। 🤓
|
||||
|
||||
## प्रदर्शन { #performance }
|
||||
|
||||
स्वतंत्र TechEmpower बेंचमार्क दिखाते हैं कि Uvicorn के तहत चलने वाले **FastAPI** एप्लीकेशन्स [उपलब्ध सबसे तेज़ Python फ़्रेमवर्क्स में से एक](https://www.techempower.com/benchmarks/#section=test&runid=7464e520-0dc2-473d-bd34-dbdfd7e85911&hw=ph&test=query&l=zijzen-7) हैं, केवल Starlette और Uvicorn (जो FastAPI द्वारा आंतरिक रूप से उपयोग किए जाते हैं) से नीचे। (*)
|
||||
|
||||
इसके बारे में अधिक समझने के लिए, [बेंचमार्क्स](https://fastapi.tiangolo.com/hi/benchmarks/) सेक्शन देखें।
|
||||
|
||||
## निर्भरताएँ { #dependencies }
|
||||
|
||||
FastAPI, Pydantic और Starlette पर निर्भर करता है।
|
||||
|
||||
### `standard` निर्भरताएँ { #standard-dependencies }
|
||||
|
||||
जब आप `pip install "fastapi[standard]"` के साथ FastAPI स्थापित करते हैं, तो यह `standard` समूह की वैकल्पिक निर्भरताओं के साथ आता है:
|
||||
|
||||
Pydantic द्वारा उपयोग किया गया:
|
||||
|
||||
* [`email-validator`](https://github.com/JoshData/python-email-validator) - ईमेल वैधीकरण के लिए।
|
||||
|
||||
Starlette द्वारा उपयोग किया गया:
|
||||
|
||||
* [`httpx`](https://www.python-httpx.org) - यदि आप `TestClient` का उपयोग करना चाहते हैं तो आवश्यक।
|
||||
* [`jinja2`](https://jinja.palletsprojects.com) - यदि आप डिफ़ॉल्ट टेम्पलेट कॉन्फ़िगरेशन का उपयोग करना चाहते हैं तो आवश्यक।
|
||||
* [`python-multipart`](https://github.com/Kludex/python-multipart) - यदि आप फॉर्म <dfn title="HTTP अनुरोध से आने वाली स्ट्रिंग को Python डेटा में बदलना">"पार्सिंग"</dfn> का समर्थन करना चाहते हैं, `request.form()` के साथ, तो आवश्यक।
|
||||
|
||||
FastAPI द्वारा उपयोग किया गया:
|
||||
|
||||
* [`uvicorn`](https://www.uvicorn.dev) - वह सर्वर जो आपकी एप्लिकेशन को लोड और सर्व करता है। इसमें `uvicorn[standard]` शामिल है, जिसमें उच्च-प्रदर्शन सर्विंग के लिए कुछ निर्भरताएँ (जैसे `uvloop`) शामिल हैं।
|
||||
* `fastapi-cli[standard]` - `fastapi` कमांड प्रदान करने के लिए।
|
||||
* इसमें `fastapi-cloud-cli` शामिल है, जो आपको अपनी FastAPI एप्लिकेशन को [FastAPI Cloud](https://fastapicloud.com) पर डिप्लॉय करने की अनुमति देता है।
|
||||
|
||||
### `standard` निर्भरताओं के बिना { #without-standard-dependencies }
|
||||
|
||||
यदि आप `standard` वैकल्पिक निर्भरताओं को शामिल नहीं करना चाहते, तो आप `pip install fastapi` के साथ स्थापित कर सकते हैं, `pip install "fastapi[standard]"` के बजाय।
|
||||
|
||||
### `fastapi-cloud-cli` के बिना { #without-fastapi-cloud-cli }
|
||||
|
||||
यदि आप standard निर्भरताओं के साथ लेकिन `fastapi-cloud-cli` के बिना FastAPI स्थापित करना चाहते हैं, तो `pip install "fastapi[standard-no-fastapi-cloud-cli]"` के साथ स्थापित कर सकते हैं।
|
||||
|
||||
### अतिरिक्त वैकल्पिक निर्भरताएँ { #additional-optional-dependencies }
|
||||
|
||||
कुछ अतिरिक्त निर्भरताएँ हैं जिन्हें आप स्थापित करना चाहेंगे।
|
||||
|
||||
अतिरिक्त वैकल्पिक Pydantic निर्भरताएँ:
|
||||
|
||||
* [`pydantic-settings`](https://docs.pydantic.dev/latest/usage/pydantic_settings/) - सेटिंग्स प्रबंधन के लिए।
|
||||
* [`pydantic-extra-types`](https://docs.pydantic.dev/latest/usage/types/extra_types/extra_types/) - Pydantic के साथ उपयोग करने के लिए अतिरिक्त प्रकारों हेतु।
|
||||
|
||||
अतिरिक्त वैकल्पिक FastAPI निर्भरताएँ:
|
||||
|
||||
* [`orjson`](https://github.com/ijl/orjson) - यदि आप `ORJSONResponse` उपयोग करना चाहते हैं तो आवश्यक।
|
||||
* [`ujson`](https://github.com/esnme/ultrajson) - यदि आप `UJSONResponse` उपयोग करना चाहते हैं तो आवश्यक।
|
||||
|
||||
## लाइसेंस { #license }
|
||||
|
||||
यह प्रोजेक्ट MIT लाइसेंस की शर्तों के अंतर्गत लाइसेंस प्राप्त है।
|
||||
28
docs/hi/docs/project-generation.md
Normal file
28
docs/hi/docs/project-generation.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# Full Stack FastAPI Template { #full-stack-fastapi-template }
|
||||
|
||||
Templates आम तौर पर एक विशिष्ट setup के साथ आते हैं, लेकिन उन्हें flexible और customizable होने के लिए डिज़ाइन किया जाता है। इससे आप उन्हें अपने project की आवश्यकताओं के अनुसार modify और adapt कर सकते हैं, जिससे वे एक बेहतरीन starting point बन जाते हैं। 🏁
|
||||
|
||||
आप शुरू करने के लिए इस template का उपयोग कर सकते हैं, क्योंकि इसमें आपके लिए बहुत सा initial setup, security, database और कुछ API endpoints पहले से तैयार हैं।
|
||||
|
||||
GitHub Repository: [Full Stack FastAPI Template](https://github.com/tiangolo/full-stack-fastapi-template)
|
||||
|
||||
## Full Stack FastAPI Template - Technology Stack और Features { #full-stack-fastapi-template-technology-stack-and-features }
|
||||
|
||||
- ⚡ Python backend API के लिए [**FastAPI**](https://fastapi.tiangolo.com/hi)।
|
||||
- 🧰 Python SQL database interactions (ORM) के लिए [SQLModel](https://sqlmodel.tiangolo.com)।
|
||||
- 🔍 data validation और settings management के लिए [Pydantic](https://docs.pydantic.dev), जिसका उपयोग FastAPI करता है।
|
||||
- 💾 SQL database के रूप में [PostgreSQL](https://www.postgresql.org)।
|
||||
- 🚀 frontend के लिए [React](https://react.dev)।
|
||||
- 💃 TypeScript, hooks, Vite, और modern frontend stack के अन्य parts का उपयोग।
|
||||
- 🎨 frontend components के लिए [Tailwind CSS](https://tailwindcss.com) और [shadcn/ui](https://ui.shadcn.com)।
|
||||
- 🤖 एक automatically generated frontend client।
|
||||
- 🧪 End-to-End testing के लिए [Playwright](https://playwright.dev)।
|
||||
- 🦇 Dark mode support।
|
||||
- 🐋 development और production के लिए [Docker Compose](https://www.docker.com)।
|
||||
- 🔒 default रूप से secure password hashing।
|
||||
- 🔑 JWT (JSON Web Token) authentication।
|
||||
- 📫 Email आधारित password recovery।
|
||||
- ✅ [Pytest](https://pytest.org) के साथ tests।
|
||||
- 📞 reverse proxy / load balancer के रूप में [Traefik](https://traefik.io)।
|
||||
- 🚢 Docker Compose का उपयोग करके deployment instructions, जिसमें automatic HTTPS certificates handle करने के लिए frontend Traefik proxy setup करना शामिल है।
|
||||
- 🏭 GitHub Actions पर आधारित CI (continuous integration) और CD (continuous deployment)।
|
||||
348
docs/hi/docs/python-types.md
Normal file
348
docs/hi/docs/python-types.md
Normal file
@@ -0,0 +1,348 @@
|
||||
# Python Types परिचय { #python-types-intro }
|
||||
|
||||
Python में वैकल्पिक "type hints" (जिन्हें "type annotations" भी कहा जाता है) का समर्थन है।
|
||||
|
||||
ये **"type hints"** या annotations एक विशेष syntax हैं, जो किसी variable का <dfn title="उदाहरण के लिए: str, int, float, bool">type</dfn> घोषित करने की अनुमति देते हैं।
|
||||
|
||||
अपने variables के लिए types घोषित करके, editors और tools आपको बेहतर support दे सकते हैं।
|
||||
|
||||
यह Python type hints के बारे में बस एक **त्वरित tutorial / refresher** है। इसमें केवल उतना ही शामिल है जितना उन्हें **FastAPI** के साथ उपयोग करने के लिए न्यूनतम रूप से आवश्यक है... जो वास्तव में बहुत कम है।
|
||||
|
||||
**FastAPI** पूरी तरह से इन्हीं type hints पर आधारित है, ये इसे कई फायदे और लाभ देते हैं।
|
||||
|
||||
लेकिन अगर आप कभी **FastAPI** का उपयोग नहीं भी करते, तब भी इनके बारे में थोड़ा सीखने से आपको लाभ होगा।
|
||||
|
||||
/// note | नोट
|
||||
|
||||
अगर आप Python expert हैं, और type hints के बारे में पहले से सब कुछ जानते हैं, तो अगले chapter पर जाएँ।
|
||||
|
||||
///
|
||||
|
||||
## प्रेरणा { #motivation }
|
||||
|
||||
आइए एक सरल उदाहरण से शुरू करें:
|
||||
|
||||
{* ../../docs_src/python_types/tutorial001_py310.py *}
|
||||
|
||||
इस program को call करने पर output आता है:
|
||||
|
||||
```
|
||||
John Doe
|
||||
```
|
||||
|
||||
Function निम्नलिखित करता है:
|
||||
|
||||
* एक `first_name` और `last_name` लेता है।
|
||||
* प्रत्येक के पहले अक्षर को `title()` के साथ upper case में बदलता है।
|
||||
* उन्हें बीच में एक space के साथ <dfn title="उन्हें एक साथ रखता है, एक के रूप में। एक की सामग्री के बाद दूसरे की सामग्री के साथ।">Concatenate</dfn> करता है।
|
||||
|
||||
{* ../../docs_src/python_types/tutorial001_py310.py hl[2] *}
|
||||
|
||||
### इसे edit करें { #edit-it }
|
||||
|
||||
यह एक बहुत सरल program है।
|
||||
|
||||
लेकिन अब कल्पना करें कि आप इसे scratch से लिख रहे थे।
|
||||
|
||||
किसी point पर आप function define करना शुरू करते हैं, और आपके parameters तैयार हैं...
|
||||
|
||||
लेकिन फिर आपको "वह method जो पहले अक्षर को upper case में बदलता है" call करना है।
|
||||
|
||||
क्या वह `upper` था? क्या वह `uppercase` था? `first_uppercase`? `capitalize`?
|
||||
|
||||
फिर, आप programmer के पुराने दोस्त, editor autocompletion के साथ कोशिश करते हैं।
|
||||
|
||||
आप function का पहला parameter, `first_name`, फिर एक dot (`.`) type करते हैं और फिर completion trigger करने के लिए `Ctrl+Space` दबाते हैं।
|
||||
|
||||
लेकिन, दुख की बात है, आपको कुछ भी उपयोगी नहीं मिलता:
|
||||
|
||||
<img src="/img/python-types/image01.png">
|
||||
|
||||
### Types जोड़ें { #add-types }
|
||||
|
||||
आइए पिछले version की एक single line बदलते हैं।
|
||||
|
||||
हम ठीक इस fragment को, function के parameters को, इससे बदलेंगे:
|
||||
|
||||
```Python
|
||||
first_name, last_name
|
||||
```
|
||||
|
||||
इसमें:
|
||||
|
||||
```Python
|
||||
first_name: str, last_name: str
|
||||
```
|
||||
|
||||
बस इतना ही।
|
||||
|
||||
यही "type hints" हैं:
|
||||
|
||||
{* ../../docs_src/python_types/tutorial002_py310.py hl[1] *}
|
||||
|
||||
यह default values declare करने जैसा नहीं है, जैसा कि इसमें होता:
|
||||
|
||||
```Python
|
||||
first_name="john", last_name="doe"
|
||||
```
|
||||
|
||||
यह एक अलग चीज़ है।
|
||||
|
||||
हम colons (`:`) का उपयोग कर रहे हैं, equals (`=`) का नहीं।
|
||||
|
||||
और type hints जोड़ने से सामान्यतः यह नहीं बदलता कि बिना उनके जो होता, वह कैसे होता।
|
||||
|
||||
लेकिन अब, कल्पना करें कि आप फिर से उस function को बनाने के बीच में हैं, लेकिन type hints के साथ।
|
||||
|
||||
उसी point पर, आप `Ctrl+Space` के साथ autocomplete trigger करने की कोशिश करते हैं और आप देखते हैं:
|
||||
|
||||
<img src="/img/python-types/image02.png">
|
||||
|
||||
इसके साथ, आप options देखते हुए scroll कर सकते हैं, जब तक आपको वह न मिल जाए जो "पहचाना हुआ लगे":
|
||||
|
||||
<img src="/img/python-types/image03.png">
|
||||
|
||||
## और प्रेरणा { #more-motivation }
|
||||
|
||||
इस function को देखें, इसमें पहले से type hints हैं:
|
||||
|
||||
{* ../../docs_src/python_types/tutorial003_py310.py hl[1] *}
|
||||
|
||||
क्योंकि editor variables के types जानता है, आपको केवल completion ही नहीं मिलता, आपको error checks भी मिलते हैं:
|
||||
|
||||
<img src="/img/python-types/image04.png">
|
||||
|
||||
अब आप जानते हैं कि आपको इसे ठीक करना है, `age` को `str(age)` के साथ string में convert करना है:
|
||||
|
||||
{* ../../docs_src/python_types/tutorial004_py310.py hl[2] *}
|
||||
|
||||
## Types declare करना { #declaring-types }
|
||||
|
||||
आपने अभी type hints declare करने की मुख्य जगह देखी। Function parameters के रूप में।
|
||||
|
||||
यही वह मुख्य जगह भी है जहाँ आप उन्हें **FastAPI** के साथ उपयोग करेंगे।
|
||||
|
||||
### Simple types { #simple-types }
|
||||
|
||||
आप सभी standard Python types declare कर सकते हैं, केवल `str` ही नहीं।
|
||||
|
||||
आप उदाहरण के लिए उपयोग कर सकते हैं:
|
||||
|
||||
* `int`
|
||||
* `float`
|
||||
* `bool`
|
||||
* `bytes`
|
||||
|
||||
{* ../../docs_src/python_types/tutorial005_py310.py hl[1] *}
|
||||
|
||||
### `typing` module { #typing-module }
|
||||
|
||||
कुछ अतिरिक्त use cases के लिए, आपको standard library के `typing` module से कुछ चीज़ें import करने की आवश्यकता हो सकती है, उदाहरण के लिए जब आप declare करना चाहते हैं कि किसी चीज़ का "कोई भी type" है, तो आप `typing` से `Any` का उपयोग कर सकते हैं:
|
||||
|
||||
```python
|
||||
from typing import Any
|
||||
|
||||
|
||||
def some_function(data: Any):
|
||||
print(data)
|
||||
```
|
||||
|
||||
### Generic types { #generic-types }
|
||||
|
||||
कुछ types square brackets में "type parameters" ले सकते हैं, ताकि उनके internal types define किए जा सकें, उदाहरण के लिए "strings की list" को `list[str]` declare किया जाएगा।
|
||||
|
||||
जो types type parameters ले सकते हैं उन्हें **Generic types** या **Generics** कहा जाता है।
|
||||
|
||||
आप उन्हीं builtin types को generics के रूप में उपयोग कर सकते हैं (square brackets और अंदर types के साथ):
|
||||
|
||||
* `list`
|
||||
* `tuple`
|
||||
* `set`
|
||||
* `dict`
|
||||
|
||||
#### List { #list }
|
||||
|
||||
उदाहरण के लिए, आइए एक variable को `str` की `list` के रूप में define करते हैं।
|
||||
|
||||
Variable को उसी colon (`:`) syntax के साथ declare करें।
|
||||
|
||||
Type के रूप में, `list` रखें।
|
||||
|
||||
क्योंकि list एक ऐसा type है जिसमें कुछ internal types होते हैं, आप उन्हें square brackets में रखते हैं:
|
||||
|
||||
{* ../../docs_src/python_types/tutorial006_py310.py hl[1] *}
|
||||
|
||||
/// note | नोट
|
||||
|
||||
Square brackets में मौजूद उन internal types को "type parameters" कहा जाता है।
|
||||
|
||||
इस case में, `str` वह type parameter है जो `list` को pass किया गया है।
|
||||
|
||||
///
|
||||
|
||||
इसका मतलब है: "variable `items` एक `list` है, और इस list का प्रत्येक item एक `str` है"।
|
||||
|
||||
ऐसा करने से, आपका editor list से items process करते समय भी support दे सकता है:
|
||||
|
||||
<img src="/img/python-types/image05.png">
|
||||
|
||||
Types के बिना, इसे हासिल करना लगभग असंभव है।
|
||||
|
||||
ध्यान दें कि variable `item`, list `items` के elements में से एक है।
|
||||
|
||||
और फिर भी, editor जानता है कि यह एक `str` है, और उसके लिए support देता है।
|
||||
|
||||
#### Tuple और Set { #tuple-and-set }
|
||||
|
||||
आप `tuple`s और `set`s declare करने के लिए भी ऐसा ही करेंगे:
|
||||
|
||||
{* ../../docs_src/python_types/tutorial007_py310.py hl[1] *}
|
||||
|
||||
इसका मतलब है:
|
||||
|
||||
* Variable `items_t` 3 items वाला एक `tuple` है, एक `int`, दूसरा `int`, और एक `str`।
|
||||
* Variable `items_s` एक `set` है, और इसके प्रत्येक item का type `bytes` है।
|
||||
|
||||
#### Dict { #dict }
|
||||
|
||||
`dict` define करने के लिए, आप 2 type parameters pass करते हैं, commas से separated।
|
||||
|
||||
पहला type parameter `dict` की keys के लिए होता है।
|
||||
|
||||
दूसरा type parameter `dict` की values के लिए होता है:
|
||||
|
||||
{* ../../docs_src/python_types/tutorial008_py310.py hl[1] *}
|
||||
|
||||
इसका मतलब है:
|
||||
|
||||
* Variable `prices` एक `dict` है:
|
||||
* इस `dict` की keys `str` type की हैं (मान लें, प्रत्येक item का नाम)।
|
||||
* इस `dict` की values `float` type की हैं (मान लें, प्रत्येक item की price)।
|
||||
|
||||
#### Union { #union }
|
||||
|
||||
आप declare कर सकते हैं कि कोई variable **कई types** में से कोई भी हो सकता है, उदाहरण के लिए, एक `int` या एक `str`।
|
||||
|
||||
इसे define करने के लिए आप दोनों types को separate करने के लिए <dfn title='इसे "bitwise or operator" भी कहा जाता है, लेकिन वह अर्थ यहाँ relevant नहीं है'>vertical bar (`|`)</dfn> का उपयोग करते हैं।
|
||||
|
||||
इसे "union" कहा जाता है, क्योंकि variable उन दो type sets के union में कुछ भी हो सकता है।
|
||||
|
||||
```Python hl_lines="1"
|
||||
{!> ../../docs_src/python_types/tutorial008b_py310.py!}
|
||||
```
|
||||
|
||||
इसका मतलब है कि `item` एक `int` या एक `str` हो सकता है।
|
||||
|
||||
#### संभवतः `None` { #possibly-none }
|
||||
|
||||
आप declare कर सकते हैं कि किसी value का type, जैसे `str`, हो सकता है, लेकिन वह `None` भी हो सकती है।
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="1"
|
||||
{!> ../../docs_src/python_types/tutorial009_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
सिर्फ `str` के बजाय `str | None` का उपयोग करने से editor आपको उन errors को detect करने में मदद करेगा जहाँ आप यह मान रहे हो सकते हैं कि कोई value हमेशा `str` है, जबकि वास्तव में वह `None` भी हो सकती है।
|
||||
|
||||
### Classes को types के रूप में { #classes-as-types }
|
||||
|
||||
आप किसी class को भी variable के type के रूप में declare कर सकते हैं।
|
||||
|
||||
मान लें आपके पास एक class `Person` है, जिसमें एक name है:
|
||||
|
||||
{* ../../docs_src/python_types/tutorial010_py310.py hl[1:3] *}
|
||||
|
||||
फिर आप किसी variable को `Person` type का declare कर सकते हैं:
|
||||
|
||||
{* ../../docs_src/python_types/tutorial010_py310.py hl[6] *}
|
||||
|
||||
और फिर, फिर से, आपको पूरा editor support मिलता है:
|
||||
|
||||
<img src="/img/python-types/image06.png">
|
||||
|
||||
ध्यान दें कि इसका मतलब है "`one_person`, class `Person` का एक **instance** है"।
|
||||
|
||||
इसका मतलब यह नहीं है कि "`one_person`, `Person` नाम की **class** है"।
|
||||
|
||||
## Pydantic models { #pydantic-models }
|
||||
|
||||
[Pydantic](https://docs.pydantic.dev/) data validation करने के लिए एक Python library है।
|
||||
|
||||
आप data की "shape" को attributes वाली classes के रूप में declare करते हैं।
|
||||
|
||||
और प्रत्येक attribute का एक type होता है।
|
||||
|
||||
फिर आप कुछ values के साथ उस class का एक instance create करते हैं और यह values को validate करेगा, उन्हें appropriate type में convert करेगा (अगर ऐसा case है) और आपको पूरे data वाला एक object देगा।
|
||||
|
||||
और उस resulting object के साथ आपको पूरा editor support मिलता है।
|
||||
|
||||
Official Pydantic docs से एक उदाहरण:
|
||||
|
||||
{* ../../docs_src/python_types/tutorial011_py310.py *}
|
||||
|
||||
/// note | नोट
|
||||
|
||||
[Pydantic के बारे में अधिक जानने के लिए, इसके docs देखें](https://docs.pydantic.dev/)।
|
||||
|
||||
///
|
||||
|
||||
**FastAPI** पूरी तरह से Pydantic पर आधारित है।
|
||||
|
||||
आप यह सब practice में [Tutorial - User Guide](tutorial/index.md) में बहुत अधिक देखेंगे।
|
||||
|
||||
## Metadata Annotations के साथ Type Hints { #type-hints-with-metadata-annotations }
|
||||
|
||||
Python में एक feature भी है जो `Annotated` का उपयोग करके इन type hints में **अतिरिक्त <dfn title="Data के बारे में data, इस case में, type के बारे में जानकारी, जैसे description।">metadata</dfn>** डालने की अनुमति देता है।
|
||||
|
||||
आप `typing` से `Annotated` import कर सकते हैं।
|
||||
|
||||
{* ../../docs_src/python_types/tutorial013_py310.py hl[1,4] *}
|
||||
|
||||
Python खुद इस `Annotated` के साथ कुछ नहीं करता। और editors और अन्य tools के लिए, type अभी भी `str` है।
|
||||
|
||||
लेकिन आप `Annotated` में इस जगह का उपयोग **FastAPI** को अतिरिक्त metadata देने के लिए कर सकते हैं कि आप अपनी application को कैसे behave कराना चाहते हैं।
|
||||
|
||||
याद रखने वाली महत्वपूर्ण बात यह है कि `Annotated` को pass किया गया **पहला *type parameter*** ही **actual type** होता है। बाकी सब, अन्य tools के लिए केवल metadata है।
|
||||
|
||||
अभी के लिए, आपको बस यह जानना है कि `Annotated` मौजूद है, और यह standard Python है। 😎
|
||||
|
||||
बाद में आप देखेंगे कि यह कितना **powerful** हो सकता है।
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
यह तथ्य कि यह **standard Python** है, इसका मतलब है कि आपको अपने editor में, अपने code को analyze और refactor करने वाले tools के साथ, आदि, अभी भी **सबसे अच्छा possible developer experience** मिलेगा। ✨
|
||||
|
||||
और यह भी कि आपका code कई अन्य Python tools और libraries के साथ बहुत compatible होगा। 🚀
|
||||
|
||||
///
|
||||
|
||||
## **FastAPI** में Type hints { #type-hints-in-fastapi }
|
||||
|
||||
**FastAPI** इन type hints का लाभ उठाकर कई चीज़ें करता है।
|
||||
|
||||
**FastAPI** के साथ आप type hints के साथ parameters declare करते हैं और आपको मिलता है:
|
||||
|
||||
* **Editor support**।
|
||||
* **Type checks**।
|
||||
|
||||
...और **FastAPI** उन्हीं declarations का उपयोग करता है:
|
||||
|
||||
* **Requirements define** करने के लिए: request path parameters, query parameters, headers, bodies, dependencies, आदि से।
|
||||
* **Data convert** करने के लिए: request से required type में।
|
||||
* **Data validate** करने के लिए: प्रत्येक request से आने वाले data को:
|
||||
* Data invalid होने पर client को लौटाए जाने वाले **automatic errors** generate करना।
|
||||
* OpenAPI का उपयोग करके API को **document** करने के लिए:
|
||||
* जिसका उपयोग फिर automatic interactive documentation user interfaces द्वारा किया जाता है।
|
||||
|
||||
यह सब abstract लग सकता है। चिंता न करें। आप यह सब action में [Tutorial - User Guide](tutorial/index.md) में देखेंगे।
|
||||
|
||||
महत्वपूर्ण बात यह है कि standard Python types का उपयोग करके, एक ही जगह पर (अधिक classes, decorators, आदि जोड़ने के बजाय), **FastAPI** आपके लिए बहुत सारा काम कर देगा।
|
||||
|
||||
/// note | नोट
|
||||
|
||||
अगर आप पहले ही पूरे tutorial से गुजर चुके हैं और types के बारे में और देखने के लिए वापस आए हैं, तो एक अच्छा resource [`mypy` की "cheat sheet"](https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html) है।
|
||||
|
||||
///
|
||||
11
docs/hi/docs/translation-banner.md
Normal file
11
docs/hi/docs/translation-banner.md
Normal file
@@ -0,0 +1,11 @@
|
||||
/// details | 🌐 एआई और मनुष्यों द्वारा किया गया अनुवाद
|
||||
|
||||
यह अनुवाद मनुष्यों के मार्गदर्शन में एआई द्वारा किया गया है। 🤝
|
||||
|
||||
इसमें मूल अर्थ को गलत समझने या अप्राकृतिक लगने आदि जैसी गलतियाँ हो सकती हैं। 🤖
|
||||
|
||||
आप [हमें एआई LLM को बेहतर मार्गदर्शन करने में मदद करके](https://fastapi.tiangolo.com/hi/contributing/#translations) इस अनुवाद को बेहतर बना सकते हैं।
|
||||
|
||||
[अंग्रेज़ी संस्करण](ENGLISH_VERSION_URL)
|
||||
|
||||
///
|
||||
862
docs/hi/docs/virtual-environments.md
Normal file
862
docs/hi/docs/virtual-environments.md
Normal file
@@ -0,0 +1,862 @@
|
||||
# वर्चुअल एनवायरनमेंट { #virtual-environments }
|
||||
|
||||
जब आप Python प्रोजेक्ट्स पर काम करते हैं, तो संभवतः आपको हर प्रोजेक्ट के लिए इंस्टॉल किए जाने वाले पैकेजों को अलग रखने के लिए एक **वर्चुअल एनवायरनमेंट** (या कोई समान तरीका) इस्तेमाल करना चाहिए।
|
||||
|
||||
/// note | नोट
|
||||
|
||||
अगर आप पहले से वर्चुअल एनवायरनमेंट्स के बारे में जानते हैं, उन्हें कैसे बनाना और इस्तेमाल करना है जानते हैं, तो आप इस सेक्शन को छोड़ना चाह सकते हैं। 🤓
|
||||
|
||||
///
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
एक **वर्चुअल एनवायरनमेंट**, एक **एनवायरनमेंट वेरिएबल** से अलग होता है।
|
||||
|
||||
एक **एनवायरनमेंट वेरिएबल** सिस्टम में एक वेरिएबल होता है जिसे प्रोग्राम इस्तेमाल कर सकते हैं।
|
||||
|
||||
एक **वर्चुअल एनवायरनमेंट** एक डायरेक्टरी होती है जिसमें कुछ फाइलें होती हैं।
|
||||
|
||||
///
|
||||
|
||||
/// note | नोट
|
||||
|
||||
यह पेज आपको **वर्चुअल एनवायरनमेंट्स** का उपयोग करना और वे कैसे काम करते हैं, सिखाएगा।
|
||||
|
||||
अगर आप अपने लिए **सब कुछ मैनेज करने वाला टूल** अपनाने के लिए तैयार हैं (जिसमें Python इंस्टॉल करना भी शामिल है), तो [uv](https://github.com/astral-sh/uv) आज़माएँ।
|
||||
|
||||
///
|
||||
|
||||
## एक प्रोजेक्ट बनाएँ { #create-a-project }
|
||||
|
||||
सबसे पहले, अपने प्रोजेक्ट के लिए एक डायरेक्टरी बनाएँ।
|
||||
|
||||
मैं सामान्यतः अपनी home/user डायरेक्टरी के अंदर `code` नाम की एक डायरेक्टरी बनाता हूँ।
|
||||
|
||||
और उसके अंदर हर प्रोजेक्ट के लिए एक डायरेक्टरी बनाता हूँ।
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
// होम डायरेक्टरी में जाएँ
|
||||
$ cd
|
||||
// अपने सभी कोड प्रोजेक्ट्स के लिए एक डायरेक्टरी बनाएँ
|
||||
$ mkdir code
|
||||
// उस कोड डायरेक्टरी में जाएँ
|
||||
$ cd code
|
||||
// इस प्रोजेक्ट के लिए एक डायरेक्टरी बनाएँ
|
||||
$ mkdir awesome-project
|
||||
// उस प्रोजेक्ट डायरेक्टरी में जाएँ
|
||||
$ cd awesome-project
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
## वर्चुअल एनवायरनमेंट बनाएँ { #create-a-virtual-environment }
|
||||
|
||||
जब आप किसी Python प्रोजेक्ट पर **पहली बार** काम शुरू करते हैं, तो एक वर्चुअल एनवायरनमेंट **<dfn title="अन्य विकल्प भी हैं, यह एक सरल दिशानिर्देश है">अपने प्रोजेक्ट के अंदर</dfn>** बनाएँ।
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
आपको यह **हर प्रोजेक्ट के लिए केवल एक बार** करना होता है, हर बार काम करते समय नहीं।
|
||||
|
||||
///
|
||||
|
||||
//// tab | `venv`
|
||||
|
||||
वर्चुअल एनवायरनमेंट बनाने के लिए, आप Python के साथ आने वाले `venv` module का उपयोग कर सकते हैं।
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ python -m venv .venv
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
/// details | उस कमांड का क्या अर्थ है
|
||||
|
||||
* `python`: `python` नाम के प्रोग्राम का उपयोग करें
|
||||
* `-m`: किसी module को script की तरह call करें, अगला हम उसे बताएँगे कि कौन-सा module
|
||||
* `venv`: `venv` नाम के module का उपयोग करें जो सामान्यतः Python के साथ इंस्टॉल आता है
|
||||
* `.venv`: नई डायरेक्टरी `.venv` में वर्चुअल एनवायरनमेंट बनाएँ
|
||||
|
||||
///
|
||||
|
||||
////
|
||||
|
||||
//// tab | `uv`
|
||||
|
||||
अगर आपके पास [`uv`](https://github.com/astral-sh/uv) इंस्टॉल है, तो आप इसका उपयोग वर्चुअल एनवायरनमेंट बनाने के लिए कर सकते हैं।
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ uv venv
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
डिफ़ॉल्ट रूप से, `uv` `.venv` नाम की डायरेक्टरी में वर्चुअल एनवायरनमेंट बनाएगा।
|
||||
|
||||
लेकिन आप डायरेक्टरी नाम के साथ एक अतिरिक्त argument देकर इसे customize कर सकते हैं।
|
||||
|
||||
///
|
||||
|
||||
////
|
||||
|
||||
वह कमांड `.venv` नाम की डायरेक्टरी में एक नया वर्चुअल एनवायरनमेंट बनाता है।
|
||||
|
||||
/// details | `.venv` या कोई दूसरा नाम
|
||||
|
||||
आप वर्चुअल एनवायरनमेंट को किसी दूसरी डायरेक्टरी में बना सकते हैं, लेकिन इसे `.venv` कहने की एक convention है।
|
||||
|
||||
///
|
||||
|
||||
## वर्चुअल एनवायरनमेंट सक्रिय करें { #activate-the-virtual-environment }
|
||||
|
||||
नए वर्चुअल एनवायरनमेंट को activate करें ताकि आप जो भी Python command चलाएँ या जो package install करें, वह इसका उपयोग करे।
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
प्रोजेक्ट पर काम करने के लिए **हर बार** जब आप एक **नया terminal session** शुरू करें, तो यह करें।
|
||||
|
||||
///
|
||||
|
||||
//// tab | Linux, macOS
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ source .venv/bin/activate
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
////
|
||||
|
||||
//// tab | Windows PowerShell
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ .venv\Scripts\Activate.ps1
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
////
|
||||
|
||||
//// tab | Windows Bash
|
||||
|
||||
या अगर आप Windows के लिए Bash का उपयोग करते हैं (जैसे [Git Bash](https://gitforwindows.org/)):
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ source .venv/Scripts/activate
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
////
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
हर बार जब आप उस environment में कोई **नया package** install करें, तो environment को फिर से **activate** करें।
|
||||
|
||||
यह सुनिश्चित करता है कि अगर आप उस package द्वारा install किया गया कोई **terminal (<abbr title="command line interface - कमांड लाइन इंटरफ़ेस">CLI</abbr>) program** इस्तेमाल करते हैं, तो आप अपने वर्चुअल एनवायरनमेंट वाला ही उपयोग करें, कोई और नहीं जो global रूप से install हो सकता है, शायद आपकी ज़रूरत से अलग version के साथ।
|
||||
|
||||
///
|
||||
|
||||
## जाँचें कि वर्चुअल एनवायरनमेंट सक्रिय है { #check-the-virtual-environment-is-active }
|
||||
|
||||
जाँचें कि वर्चुअल एनवायरनमेंट active है (पिछली command ने काम किया)।
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
यह **वैकल्पिक** है, लेकिन यह **जाँचने** का एक अच्छा तरीका है कि सब कुछ अपेक्षा के अनुसार काम कर रहा है और आप वही वर्चुअल एनवायरनमेंट इस्तेमाल कर रहे हैं जिसका आपने इरादा किया था।
|
||||
|
||||
///
|
||||
|
||||
//// tab | Linux, macOS, Windows Bash
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ which python
|
||||
|
||||
/home/user/code/awesome-project/.venv/bin/python
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
अगर यह `.venv/bin/python` पर `python` binary दिखाता है, आपके प्रोजेक्ट के अंदर (इस मामले में `awesome-project`), तो यह काम कर गया। 🎉
|
||||
|
||||
////
|
||||
|
||||
//// tab | Windows PowerShell
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ Get-Command python
|
||||
|
||||
C:\Users\user\code\awesome-project\.venv\Scripts\python
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
अगर यह `.venv\Scripts\python` पर `python` binary दिखाता है, आपके प्रोजेक्ट के अंदर (इस मामले में `awesome-project`), तो यह काम कर गया। 🎉
|
||||
|
||||
////
|
||||
|
||||
## `pip` अपग्रेड करें { #upgrade-pip }
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
अगर आप [`uv`](https://github.com/astral-sh/uv) का उपयोग करते हैं, तो आप चीजें install करने के लिए `pip` की बजाय उसी का उपयोग करेंगे, इसलिए आपको `pip` upgrade करने की ज़रूरत नहीं है। 😎
|
||||
|
||||
///
|
||||
|
||||
अगर आप packages install करने के लिए `pip` का उपयोग कर रहे हैं (यह Python के साथ default रूप से आता है), तो आपको इसे latest version में **upgrade** करना चाहिए।
|
||||
|
||||
किसी package को install करते समय कई अजीब errors केवल पहले `pip` upgrade करने से हल हो जाते हैं।
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
आप सामान्यतः यह **एक बार** करेंगे, वर्चुअल एनवायरनमेंट बनाने के ठीक बाद।
|
||||
|
||||
///
|
||||
|
||||
सुनिश्चित करें कि वर्चुअल एनवायरनमेंट active है (ऊपर वाली command से) और फिर चलाएँ:
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ python -m pip install --upgrade pip
|
||||
|
||||
---> 100%
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
कभी-कभी, pip upgrade करने की कोशिश करते समय आपको **`No module named pip`** error मिल सकता है।
|
||||
|
||||
अगर ऐसा होता है, तो नीचे दी गई command का उपयोग करके pip install और upgrade करें:
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ python -m ensurepip --upgrade
|
||||
|
||||
---> 100%
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
यह command pip को install करेगी अगर वह पहले से install नहीं है और यह भी सुनिश्चित करेगी कि install किया गया pip का version कम से कम `ensurepip` में उपलब्ध version जितना नया हो।
|
||||
|
||||
///
|
||||
|
||||
## `.gitignore` जोड़ें { #add-gitignore }
|
||||
|
||||
अगर आप **Git** का उपयोग कर रहे हैं (आपको करना चाहिए), तो अपनी `.venv` की हर चीज़ को Git से exclude करने के लिए एक `.gitignore` file जोड़ें।
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
अगर आपने वर्चुअल एनवायरनमेंट बनाने के लिए [`uv`](https://github.com/astral-sh/uv) का उपयोग किया है, तो यह आपके लिए पहले ही कर चुका है, आप यह step छोड़ सकते हैं। 😎
|
||||
|
||||
///
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
यह **एक बार** करें, वर्चुअल एनवायरनमेंट बनाने के ठीक बाद।
|
||||
|
||||
///
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ echo "*" > .venv/.gitignore
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
/// details | उस कमांड का क्या अर्थ है
|
||||
|
||||
* `echo "*"`: terminal में text `*` को "print" करेगा (अगला हिस्सा इसे थोड़ा बदल देता है)
|
||||
* `>`: `>` के बाईं ओर वाली command द्वारा terminal में print की गई कोई भी चीज़ print नहीं होनी चाहिए, बल्कि `>` के दाईं ओर वाली file में लिखी जानी चाहिए
|
||||
* `.gitignore`: उस file का नाम जहाँ text लिखा जाना चाहिए
|
||||
|
||||
और Git के लिए `*` का मतलब "सब कुछ" होता है। इसलिए, यह `.venv` directory में सब कुछ ignore करेगा।
|
||||
|
||||
वह command `.gitignore` file बनाएगी, इस content के साथ:
|
||||
|
||||
```gitignore
|
||||
*
|
||||
```
|
||||
|
||||
///
|
||||
|
||||
## Packages इंस्टॉल करें { #install-packages }
|
||||
|
||||
Environment activate करने के बाद, आप उसमें packages install कर सकते हैं।
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
जब आप अपने प्रोजेक्ट के लिए आवश्यक packages install या upgrade कर रहे हों, तो यह **एक बार** करें।
|
||||
|
||||
अगर आपको किसी version को upgrade करना हो या कोई नया package जोड़ना हो, तो आप **यह फिर से करेंगे**।
|
||||
|
||||
///
|
||||
|
||||
### सीधे Packages इंस्टॉल करें { #install-packages-directly }
|
||||
|
||||
अगर आप जल्दी में हैं और अपने प्रोजेक्ट की package requirements declare करने के लिए कोई file इस्तेमाल नहीं करना चाहते, तो आप उन्हें सीधे install कर सकते हैं।
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
आपके program को जिन packages और versions की ज़रूरत है, उन्हें एक file में रखना (बहुत) अच्छा विचार है (उदाहरण के लिए `requirements.txt` या `pyproject.toml`)।
|
||||
|
||||
///
|
||||
|
||||
//// tab | `pip`
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ pip install "fastapi[standard]"
|
||||
|
||||
---> 100%
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
////
|
||||
|
||||
//// tab | `uv`
|
||||
|
||||
अगर आपके पास [`uv`](https://github.com/astral-sh/uv) है:
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ uv pip install "fastapi[standard]"
|
||||
---> 100%
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
////
|
||||
|
||||
### `requirements.txt` से इंस्टॉल करें { #install-from-requirements-txt }
|
||||
|
||||
अगर आपके पास `requirements.txt` है, तो अब आप इसके packages install करने के लिए इसका उपयोग कर सकते हैं।
|
||||
|
||||
//// tab | `pip`
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ pip install -r requirements.txt
|
||||
---> 100%
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
////
|
||||
|
||||
//// tab | `uv`
|
||||
|
||||
अगर आपके पास [`uv`](https://github.com/astral-sh/uv) है:
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ uv pip install -r requirements.txt
|
||||
---> 100%
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
////
|
||||
|
||||
/// details | `requirements.txt`
|
||||
|
||||
कुछ packages वाला `requirements.txt` ऐसा दिख सकता है:
|
||||
|
||||
```requirements.txt
|
||||
fastapi[standard]==0.113.0
|
||||
pydantic==2.8.0
|
||||
```
|
||||
|
||||
///
|
||||
|
||||
## अपना Program चलाएँ { #run-your-program }
|
||||
|
||||
वर्चुअल एनवायरनमेंट activate करने के बाद, आप अपना program चला सकते हैं, और यह आपके वर्चुअल एनवायरनमेंट के अंदर मौजूद Python का उपयोग करेगा, उन packages के साथ जिन्हें आपने वहाँ install किया है।
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ python main.py
|
||||
|
||||
Hello World
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
## अपना Editor कॉन्फ़िगर करें { #configure-your-editor }
|
||||
|
||||
आप शायद एक editor का उपयोग करेंगे, सुनिश्चित करें कि आप इसे उसी वर्चुअल एनवायरनमेंट का उपयोग करने के लिए configure करें जिसे आपने बनाया है (यह शायद इसे autodetect कर लेगा), ताकि आपको autocompletion और inline errors मिल सकें।
|
||||
|
||||
उदाहरण के लिए:
|
||||
|
||||
* [VS Code](https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment)
|
||||
* [PyCharm](https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html)
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
आपको सामान्यतः यह केवल **एक बार** करना होता है, जब आप वर्चुअल एनवायरनमेंट बनाते हैं।
|
||||
|
||||
///
|
||||
|
||||
## वर्चुअल एनवायरनमेंट निष्क्रिय करें { #deactivate-the-virtual-environment }
|
||||
|
||||
जब आप अपने प्रोजेक्ट पर काम कर लें, तो आप वर्चुअल एनवायरनमेंट को **deactivate** कर सकते हैं।
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ deactivate
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
इस तरह, जब आप `python` चलाएँगे, तो यह वहाँ install packages वाले उस वर्चुअल एनवायरनमेंट से इसे चलाने की कोशिश नहीं करेगा।
|
||||
|
||||
## काम करने के लिए तैयार { #ready-to-work }
|
||||
|
||||
अब आप अपने प्रोजेक्ट पर काम शुरू करने के लिए तैयार हैं।
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
क्या आप समझना चाहते हैं कि ऊपर की सारी चीज़ें क्या हैं?
|
||||
|
||||
आगे पढ़ते रहें। 👇🤓
|
||||
|
||||
///
|
||||
|
||||
## वर्चुअल एनवायरनमेंट क्यों { #why-virtual-environments }
|
||||
|
||||
FastAPI के साथ काम करने के लिए आपको [Python](https://www.python.org/) install करना होगा।
|
||||
|
||||
उसके बाद, आपको FastAPI और कोई भी अन्य **packages** जिन्हें आप इस्तेमाल करना चाहते हैं, **install** करने होंगे।
|
||||
|
||||
Packages install करने के लिए आप सामान्यतः Python के साथ आने वाली `pip` command (या समान alternatives) का उपयोग करेंगे।
|
||||
|
||||
फिर भी, अगर आप सीधे `pip` का उपयोग करते हैं, तो packages आपके **global Python environment** (Python की global installation) में install हो जाएँगे।
|
||||
|
||||
### समस्या { #the-problem }
|
||||
|
||||
तो, global Python environment में packages install करने में समस्या क्या है?
|
||||
|
||||
किसी समय, आप शायद कई अलग-अलग programs लिखेंगे जो **अलग-अलग packages** पर निर्भर करते हैं। और जिन projects पर आप काम करेंगे उनमें से कुछ उसी package के **अलग-अलग versions** पर निर्भर होंगे। 😱
|
||||
|
||||
उदाहरण के लिए, आप `philosophers-stone` नाम का एक project बना सकते हैं, यह program **`harry`, version `1`** नाम के किसी दूसरे package पर निर्भर करता है। इसलिए, आपको `harry` install करना होगा।
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
stone(philosophers-stone) -->|requires| harry-1[harry v1]
|
||||
```
|
||||
|
||||
फिर, कुछ समय बाद, आप `prisoner-of-azkaban` नाम का दूसरा project बनाते हैं, और यह project भी `harry` पर निर्भर करता है, लेकिन इस project को **`harry` version `3`** चाहिए।
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
azkaban(prisoner-of-azkaban) --> |requires| harry-3[harry v3]
|
||||
```
|
||||
|
||||
लेकिन अब समस्या यह है कि अगर आप packages को local **वर्चुअल एनवायरनमेंट** में install करने के बजाय globally (global environment में) install करते हैं, तो आपको चुनना होगा कि `harry` का कौन-सा version install करना है।
|
||||
|
||||
अगर आप `philosophers-stone` चलाना चाहते हैं, तो आपको पहले `harry` version `1` install करना होगा, उदाहरण के लिए:
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ pip install "harry==1"
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
और फिर आपके global Python environment में `harry` version `1` install हो जाएगा।
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
subgraph global[global env]
|
||||
harry-1[harry v1]
|
||||
end
|
||||
subgraph stone-project[philosophers-stone project]
|
||||
stone(philosophers-stone) -->|requires| harry-1
|
||||
end
|
||||
```
|
||||
|
||||
लेकिन फिर अगर आप `prisoner-of-azkaban` चलाना चाहते हैं, तो आपको `harry` version `1` uninstall करके `harry` version `3` install करना होगा (या सिर्फ version `3` install करने से version `1` automatically uninstall हो जाएगा)।
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ pip install "harry==3"
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
और फिर आपके global Python environment में `harry` version `3` install हो जाएगा।
|
||||
|
||||
और अगर आप `philosophers-stone` फिर से चलाने की कोशिश करते हैं, तो संभावना है कि यह **काम न करे** क्योंकि इसे `harry` version `1` चाहिए।
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
subgraph global[global env]
|
||||
harry-1[<strike>harry v1</strike>]
|
||||
style harry-1 fill:#ccc,stroke-dasharray: 5 5
|
||||
harry-3[harry v3]
|
||||
end
|
||||
subgraph stone-project[philosophers-stone project]
|
||||
stone(philosophers-stone) -.-x|⛔️| harry-1
|
||||
end
|
||||
subgraph azkaban-project[prisoner-of-azkaban project]
|
||||
azkaban(prisoner-of-azkaban) --> |requires| harry-3
|
||||
end
|
||||
```
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
Python packages में **नए versions** में **breaking changes से बचने** की पूरी कोशिश करना बहुत आम है, लेकिन सुरक्षित रहना बेहतर है, और नए versions को जानबूझकर तथा तब install करना बेहतर है जब आप tests चलाकर जाँच सकें कि सब कुछ सही तरीके से काम कर रहा है।
|
||||
|
||||
///
|
||||
|
||||
अब, यही चीज़ उन **कई** अन्य **packages** के साथ कल्पना करें जिन पर आपके सभी **projects निर्भर करते हैं**। इसे manage करना बहुत कठिन है। और संभवतः आप कुछ projects को packages के कुछ **incompatible versions** के साथ चला देंगे, और यह नहीं जान पाएँगे कि कुछ काम क्यों नहीं कर रहा।
|
||||
|
||||
साथ ही, आपके operating system (जैसे Linux, Windows, macOS) के आधार पर, उसमें Python पहले से install आया हो सकता है। और उस मामले में संभवतः कुछ packages कुछ specific versions के साथ pre-installed होंगे जो **आपके system के लिए आवश्यक** हैं। अगर आप global Python environment में packages install करते हैं, तो आप अपने operating system के साथ आए कुछ programs को **break** कर सकते हैं।
|
||||
|
||||
## Packages कहाँ इंस्टॉल होते हैं { #where-are-packages-installed }
|
||||
|
||||
जब आप Python install करते हैं, तो यह आपके computer पर कुछ files वाली कुछ directories बनाता है।
|
||||
|
||||
इनमें से कुछ directories वे होती हैं जो आपके द्वारा install किए गए सभी packages को रखने की जिम्मेदार होती हैं।
|
||||
|
||||
जब आप चलाते हैं:
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
// इसे अभी न चलाएँ, यह केवल एक उदाहरण है 🤓
|
||||
$ pip install "fastapi[standard]"
|
||||
---> 100%
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
तो यह FastAPI code वाली एक compressed file download करेगा, सामान्यतः [PyPI](https://pypi.org/project/fastapi/) से।
|
||||
|
||||
यह उन अन्य packages की files भी **download** करेगा जिन पर FastAPI निर्भर करता है।
|
||||
|
||||
फिर यह उन सभी files को **extract** करेगा और उन्हें आपके computer की एक directory में रखेगा।
|
||||
|
||||
Default रूप से, यह उन downloaded और extracted files को उस directory में रखेगा जो आपकी Python installation के साथ आती है, वही **global environment** है।
|
||||
|
||||
## वर्चुअल एनवायरनमेंट्स क्या हैं { #what-are-virtual-environments }
|
||||
|
||||
सभी packages को global environment में रखने की समस्याओं का समाधान है कि आप जिस भी project पर काम करते हैं उसके लिए **एक वर्चुअल एनवायरनमेंट** उपयोग करें।
|
||||
|
||||
एक वर्चुअल एनवायरनमेंट एक **directory** है, global वाली के बहुत समान, जहाँ आप किसी project के लिए packages install कर सकते हैं।
|
||||
|
||||
इस तरह, हर project का अपना वर्चुअल एनवायरनमेंट (`.venv` directory) होगा, अपने packages के साथ।
|
||||
|
||||
```mermaid
|
||||
flowchart TB
|
||||
subgraph stone-project[philosophers-stone project]
|
||||
stone(philosophers-stone) --->|requires| harry-1
|
||||
subgraph venv1[.venv]
|
||||
harry-1[harry v1]
|
||||
end
|
||||
end
|
||||
subgraph azkaban-project[prisoner-of-azkaban project]
|
||||
azkaban(prisoner-of-azkaban) --->|requires| harry-3
|
||||
subgraph venv2[.venv]
|
||||
harry-3[harry v3]
|
||||
end
|
||||
end
|
||||
stone-project ~~~ azkaban-project
|
||||
```
|
||||
|
||||
## वर्चुअल एनवायरनमेंट activate करने का क्या मतलब है { #what-does-activating-a-virtual-environment-mean }
|
||||
|
||||
जब आप किसी वर्चुअल एनवायरनमेंट को activate करते हैं, उदाहरण के लिए:
|
||||
|
||||
//// tab | Linux, macOS
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ source .venv/bin/activate
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
////
|
||||
|
||||
//// tab | Windows PowerShell
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ .venv\Scripts\Activate.ps1
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
////
|
||||
|
||||
//// tab | Windows Bash
|
||||
|
||||
या अगर आप Windows के लिए Bash का उपयोग करते हैं (जैसे [Git Bash](https://gitforwindows.org/)):
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ source .venv/Scripts/activate
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
////
|
||||
|
||||
वह command कुछ [environment variables](environment-variables.md) बनाएगी या modify करेगी, जो अगली commands के लिए उपलब्ध होंगे।
|
||||
|
||||
उन variables में से एक `PATH` variable है।
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
आप [Environment Variables](environment-variables.md#path-environment-variable) section में `PATH` environment variable के बारे में और जान सकते हैं।
|
||||
|
||||
///
|
||||
|
||||
वर्चुअल एनवायरनमेंट activate करने से उसका path `.venv/bin` (Linux और macOS पर) या `.venv\Scripts` (Windows पर) `PATH` environment variable में जुड़ जाता है।
|
||||
|
||||
मान लीजिए कि environment activate करने से पहले, `PATH` variable ऐसा दिखता था:
|
||||
|
||||
//// tab | Linux, macOS
|
||||
|
||||
```plaintext
|
||||
/usr/bin:/bin:/usr/sbin:/sbin
|
||||
```
|
||||
|
||||
इसका मतलब है कि system programs को इनमें खोजता:
|
||||
|
||||
* `/usr/bin`
|
||||
* `/bin`
|
||||
* `/usr/sbin`
|
||||
* `/sbin`
|
||||
|
||||
////
|
||||
|
||||
//// tab | Windows
|
||||
|
||||
```plaintext
|
||||
C:\Windows\System32
|
||||
```
|
||||
|
||||
इसका मतलब है कि system programs को इसमें खोजता:
|
||||
|
||||
* `C:\Windows\System32`
|
||||
|
||||
////
|
||||
|
||||
वर्चुअल एनवायरनमेंट activate करने के बाद, `PATH` variable कुछ ऐसा दिखेगा:
|
||||
|
||||
//// tab | Linux, macOS
|
||||
|
||||
```plaintext
|
||||
/home/user/code/awesome-project/.venv/bin:/usr/bin:/bin:/usr/sbin:/sbin
|
||||
```
|
||||
|
||||
इसका मतलब है कि system अब सबसे पहले programs को यहाँ खोजना शुरू करेगा:
|
||||
|
||||
```plaintext
|
||||
/home/user/code/awesome-project/.venv/bin
|
||||
```
|
||||
|
||||
बाकी directories में देखने से पहले।
|
||||
|
||||
तो, जब आप terminal में `python` type करते हैं, तो system Python program को यहाँ पाएगा
|
||||
|
||||
```plaintext
|
||||
/home/user/code/awesome-project/.venv/bin/python
|
||||
```
|
||||
|
||||
और उसी का उपयोग करेगा।
|
||||
|
||||
////
|
||||
|
||||
//// tab | Windows
|
||||
|
||||
```plaintext
|
||||
C:\Users\user\code\awesome-project\.venv\Scripts;C:\Windows\System32
|
||||
```
|
||||
|
||||
इसका मतलब है कि system अब सबसे पहले programs को यहाँ खोजना शुरू करेगा:
|
||||
|
||||
```plaintext
|
||||
C:\Users\user\code\awesome-project\.venv\Scripts
|
||||
```
|
||||
|
||||
बाकी directories में देखने से पहले।
|
||||
|
||||
तो, जब आप terminal में `python` type करते हैं, तो system Python program को यहाँ पाएगा
|
||||
|
||||
```plaintext
|
||||
C:\Users\user\code\awesome-project\.venv\Scripts\python
|
||||
```
|
||||
|
||||
और उसी का उपयोग करेगा।
|
||||
|
||||
////
|
||||
|
||||
एक महत्वपूर्ण detail यह है कि यह वर्चुअल एनवायरनमेंट path को `PATH` variable की **शुरुआत** में रखेगा। System इसे किसी भी अन्य उपलब्ध Python से **पहले** पाएगा। इस तरह, जब आप `python` चलाते हैं, तो यह किसी अन्य `python` (उदाहरण के लिए, global environment वाला `python`) के बजाय **वर्चुअल एनवायरनमेंट से** Python का उपयोग करेगा।
|
||||
|
||||
वर्चुअल एनवायरनमेंट activate करने से कुछ और चीजें भी बदलती हैं, लेकिन यह उसके द्वारा की जाने वाली सबसे महत्वपूर्ण चीज़ों में से एक है।
|
||||
|
||||
## वर्चुअल एनवायरनमेंट की जाँच करना { #checking-a-virtual-environment }
|
||||
|
||||
जब आप जाँचते हैं कि वर्चुअल एनवायरनमेंट active है या नहीं, उदाहरण के लिए:
|
||||
|
||||
//// tab | Linux, macOS, Windows Bash
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ which python
|
||||
|
||||
/home/user/code/awesome-project/.venv/bin/python
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
////
|
||||
|
||||
//// tab | Windows PowerShell
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ Get-Command python
|
||||
|
||||
C:\Users\user\code\awesome-project\.venv\Scripts\python
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
////
|
||||
|
||||
इसका मतलब है कि जो `python` program उपयोग किया जाएगा, वह **वर्चुअल एनवायरनमेंट में** मौजूद है।
|
||||
|
||||
आप Linux और macOS में `which` और Windows PowerShell में `Get-Command` का उपयोग करते हैं।
|
||||
|
||||
वह command जिस तरह काम करती है, वह यह है कि यह `PATH` environment variable में जाकर **हर path को क्रम से** check करेगी, `python` नाम के program को खोजते हुए। एक बार जब यह उसे ढूँढ लेती है, तो यह आपको उस program का **path दिखाएगी**।
|
||||
|
||||
सबसे महत्वपूर्ण हिस्सा यह है कि जब आप `python` call करते हैं, तो वही exact "`python`" execute होगा।
|
||||
|
||||
तो, आप confirm कर सकते हैं कि आप सही वर्चुअल एनवायरनमेंट में हैं या नहीं।
|
||||
|
||||
/// tip | सुझाव
|
||||
|
||||
एक वर्चुअल एनवायरनमेंट activate करना, एक Python पाना, और फिर **दूसरे project में चले जाना** आसान है।
|
||||
|
||||
और दूसरा project **काम नहीं करेगा** क्योंकि आप **गलत Python** का उपयोग कर रहे हैं, जो किसी दूसरे project के वर्चुअल एनवायरनमेंट से है।
|
||||
|
||||
यह check कर पाना उपयोगी है कि कौन-सा `python` उपयोग हो रहा है। 🤓
|
||||
|
||||
///
|
||||
|
||||
## वर्चुअल एनवायरनमेंट deactivate क्यों करें { #why-deactivate-a-virtual-environment }
|
||||
|
||||
उदाहरण के लिए, आप `philosophers-stone` project पर काम कर रहे हो सकते हैं, **उस वर्चुअल एनवायरनमेंट को activate** करके, packages install करके और उस environment के साथ काम करके।
|
||||
|
||||
और फिर आप **किसी दूसरे project** `prisoner-of-azkaban` पर काम करना चाहते हैं।
|
||||
|
||||
आप उस project में जाते हैं:
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ cd ~/code/prisoner-of-azkaban
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
अगर आप `philosophers-stone` के लिए वर्चुअल एनवायरनमेंट को deactivate नहीं करते, तो जब आप terminal में `python` चलाएँगे, यह `philosophers-stone` से Python का उपयोग करने की कोशिश करेगा।
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ cd ~/code/prisoner-of-azkaban
|
||||
|
||||
$ python main.py
|
||||
|
||||
// sirius import करने में error, यह install नहीं है 😱
|
||||
Traceback (most recent call last):
|
||||
File "main.py", line 1, in <module>
|
||||
import sirius
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
लेकिन अगर आप वर्चुअल एनवायरनमेंट deactivate करके `prisoner-of-azkaban` के लिए नया वाला activate करते हैं, तो जब आप `python` चलाएँगे, यह `prisoner-of-azkaban` में मौजूद वर्चुअल एनवायरनमेंट से Python का उपयोग करेगा।
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ cd ~/code/prisoner-of-azkaban
|
||||
|
||||
// deactivate करने के लिए आपको पुरानी directory में होने की ज़रूरत नहीं है, आप जहाँ भी हों वहाँ से कर सकते हैं, दूसरे project में जाने के बाद भी 😎
|
||||
$ deactivate
|
||||
|
||||
// prisoner-of-azkaban/.venv में वर्चुअल एनवायरनमेंट activate करें 🚀
|
||||
$ source .venv/bin/activate
|
||||
|
||||
// अब जब आप python चलाएँगे, तो यह इस वर्चुअल एनवायरनमेंट में install package sirius को पाएगा ✨
|
||||
$ python main.py
|
||||
|
||||
I solemnly swear 🐺
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
## Alternatives { #alternatives }
|
||||
|
||||
यह आपको शुरू करने और यह सिखाने के लिए एक सरल guide है कि सब कुछ **अंदर से** कैसे काम करता है।
|
||||
|
||||
Virtual environments, package dependencies (requirements), projects को manage करने के कई **alternatives** हैं।
|
||||
|
||||
जब आप तैयार हों और **पूरे project को manage** करने के लिए कोई tool उपयोग करना चाहें, package dependencies, virtual environments आदि सहित, तो मैं सुझाव दूँगा कि आप [uv](https://github.com/astral-sh/uv) आज़माएँ।
|
||||
|
||||
`uv` बहुत सारी चीज़ें कर सकता है, यह कर सकता है:
|
||||
|
||||
* आपके लिए **Python install** करना, अलग-अलग versions सहित
|
||||
* आपके projects के लिए **वर्चुअल एनवायरनमेंट** manage करना
|
||||
* **Packages** install करना
|
||||
* आपके project के लिए package **dependencies और versions** manage करना
|
||||
* सुनिश्चित करना कि आपके पास install करने के लिए packages और versions का **exact** set हो, उनकी dependencies सहित, ताकि आप सुनिश्चित हो सकें कि आप अपने project को production में ठीक उसी तरह चला सकते हैं जैसे development के दौरान अपने computer पर चलाते हैं, इसे **locking** कहा जाता है
|
||||
* और कई अन्य चीज़ें
|
||||
|
||||
## निष्कर्ष { #conclusion }
|
||||
|
||||
अगर आपने यह सब पढ़ा और समझा है, तो अब **आप वर्चुअल एनवायरनमेंट्स के बारे में** वहाँ मौजूद कई developers से कहीं ज़्यादा जानते हैं। 🤓
|
||||
|
||||
इन details को जानना भविष्य में उस समय बहुत संभवतः उपयोगी होगा जब आप किसी ऐसी चीज़ को debug कर रहे होंगे जो complex लगती है, लेकिन आपको पता होगा कि **यह सब अंदर से कैसे काम करता है**। 😎
|
||||
5
docs/hi/llm-prompt.md
Normal file
5
docs/hi/llm-prompt.md
Normal file
@@ -0,0 +1,5 @@
|
||||
### Target language
|
||||
|
||||
Translate to Hindi (हिन्दी).
|
||||
|
||||
Language code: hi.
|
||||
1
docs/hi/mkdocs.yml
Normal file
1
docs/hi/mkdocs.yml
Normal file
@@ -0,0 +1 @@
|
||||
INHERIT: ../en/mkdocs.yml
|
||||
@@ -298,7 +298,7 @@ Apply shared dependencies at the router level via `dependencies=[Depends(...)]`.
|
||||
|
||||
## Async vs Sync *path operations*
|
||||
|
||||
Use `async` *path operations* only when fully certain that the logic called inside is compatible with async and await (it's called with `await`) or that doesn't block.
|
||||
Use `async` *path operations* only when fully certain that the logic called inside is compatible with async and await (it's called with `await`) or that it doesn't block.
|
||||
|
||||
```python
|
||||
from fastapi import FastAPI
|
||||
|
||||
@@ -5,7 +5,7 @@ Use dependencies when:
|
||||
* They can't be declared in Pydantic validation and require additional logic
|
||||
* The logic depends on external resources or could block in any other way
|
||||
* Other dependencies need their results (it's a sub-dependency)
|
||||
* The logic can be shared by multiple endpoints to do things like error early, authentication, etc.
|
||||
* The logic can be shared by multiple endpoints to do things like error early, handle authentication, etc.
|
||||
* They need to handle cleanup (e.g., DB sessions, file handles), using dependencies with `yield`
|
||||
* Their logic needs input data from the request, like headers, query parameters, etc.
|
||||
|
||||
@@ -53,7 +53,7 @@ def get_username():
|
||||
try:
|
||||
yield "Rick"
|
||||
finally:
|
||||
print("Cleanup up before response is sent")
|
||||
print("Clean up before response is sent")
|
||||
|
||||
UserNameDep = Annotated[str, Depends(get_username, scope="function")]
|
||||
|
||||
|
||||
@@ -71,6 +71,6 @@ Prefer it over SQLAlchemy.
|
||||
|
||||
## HTTPX
|
||||
|
||||
Use HTTPX for handling HTTP communication (e.g. with other APIs). It support sync and async usage.
|
||||
Use HTTPX for handling HTTP communication (e.g. with other APIs). It supports sync and async usage.
|
||||
|
||||
Prefer it over Requests.
|
||||
|
||||
@@ -331,6 +331,7 @@ extend-exclude = [
|
||||
"docs/uk/",
|
||||
"docs/zh/",
|
||||
"docs/zh-hant/",
|
||||
"docs/hi/",
|
||||
"htmlcov/",
|
||||
"scripts/general-llm-prompt.md",
|
||||
"scripts/tests/test_translation_fixer/test_complex_doc/",
|
||||
|
||||
@@ -31,6 +31,7 @@ SUPPORTED_LANGS = {
|
||||
"uk",
|
||||
"zh",
|
||||
"zh-hant",
|
||||
"hi",
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -132,7 +132,7 @@ def translate_page(
|
||||
print(f"Found existing translation: {out_path}")
|
||||
old_translation = out_path.read_text(encoding="utf-8")
|
||||
print(f"Translating {en_path} to {language} ({language_name})")
|
||||
agent = Agent("openai:gpt-5")
|
||||
agent = Agent("openai-chat:gpt-5.5")
|
||||
|
||||
MAX_ATTEMPTS = 3
|
||||
additional_instructions = ""
|
||||
|
||||
Reference in New Issue
Block a user