-
Notifications
You must be signed in to change notification settings - Fork 10k
Python documentation additions and cleanup #26267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| --- | ||
| pcx_content_type: concept | ||
| title: The Basics | ||
| sidebar: | ||
| order: 4 | ||
| head: | ||
| - tag: title | ||
| content: Learn the basics of Python Workers | ||
| description: Learn the basics of Python Workers | ||
| --- | ||
|
|
||
| import { WranglerConfig } from "~/components"; | ||
|
|
||
| ## Fetch Handler | ||
|
|
||
| As mentioned in the [introduction to Python Workers](/workers/languages/python/), a Python Worker can be as simple as four lines of code: | ||
|
|
||
| ```python | ||
| from workers import WorkerEntrypoint, Response | ||
|
|
||
| class Default(WorkerEntrypoint): | ||
| async def fetch(self, request): | ||
| return Response("Hello World!") | ||
| ``` | ||
|
|
||
| Similar to other Workers, the main entry point for a Python worker is the [`fetch` handler](/workers/runtime-apis/handlers/fetch) which handles incoming requests | ||
| sent to the Worker. | ||
|
|
||
| In a Python Worker, this handler is placed in a `Default` class that extends the `WorkerEntrypoint` class (which you can import from the `workers` SDK module). | ||
|
|
||
| ## The `Request` Interface | ||
|
|
||
| The `request` parameter passed to your `fetch` handler is a JavaScript Request object, exposed via the [foreign function interface (FFI)](/workers/languages/python/ffi), | ||
| allowing you to access it directly from your Python code. | ||
|
|
||
| Let's try editing the worker to accept a POST request. We know from the | ||
| [documentation for `Request`](/workers/runtime-apis/request) that we can call | ||
| `await request.json()` within an `async` function to parse the request body as | ||
| JSON. | ||
|
|
||
| In a Python Worker, you would write: | ||
|
|
||
| ```python | ||
| from workers import WorkerEntrypoint, Response | ||
| from hello import hello | ||
|
|
||
| class Default(WorkerEntrypoint): | ||
| async def fetch(self, request): | ||
| name = (await request.json()).name | ||
| return Response(hello(name)) | ||
| ``` | ||
|
|
||
| Many other JavaScript APIs are available in Python Workers via the FFI, so you can | ||
| call other methods in a similar way. | ||
|
|
||
| Once you edit the `src/entry.py`, Wrangler will automatically restart the local | ||
| development server. | ||
|
|
||
| Now, if you send a POST request with the appropriate body, | ||
| your Worker will respond with a personalized message. | ||
|
|
||
| ```bash | ||
| curl --header "Content-Type: application/json" \ | ||
| --request POST \ | ||
| --data '{"name": "Python"}' http://localhost:8787 | ||
| ``` | ||
|
|
||
| ```bash output | ||
| Hello, Python! | ||
| ``` | ||
|
|
||
| ## The `env` Attribute | ||
|
|
||
| The `env` attribute on the `WorkerEntrypoint` can be used to access | ||
| [environment variables](/workers/configuration/environment-variables/), | ||
| [secrets](/workers/configuration/secrets/),and | ||
| [bindings](/workers/runtime-apis/bindings/). | ||
|
|
||
| For example, let us try setting and using an environment variable in a Python Worker. First, add the environment variable to your Worker's [Wrangler configuration file](/workers/wrangler/configuration/): | ||
|
|
||
| <WranglerConfig> | ||
|
|
||
| ```toml | ||
| name = "hello-python-worker" | ||
| main = "src/entry.py" | ||
| compatibility_flags = ["python_workers"] | ||
| compatibility_date = "2025-11-02" | ||
|
|
||
| [vars] | ||
| API_HOST = "example.com" | ||
| ``` | ||
|
|
||
| </WranglerConfig> | ||
|
|
||
| Then, you can access the `API_HOST` environment variable via the `env` parameter: | ||
|
|
||
| ```python | ||
| from workers import WorkerEntrypoint, Response | ||
|
|
||
| class Default(WorkerEntrypoint): | ||
| async def fetch(self, request): | ||
| return Response(self.env.API_HOST) | ||
| ``` | ||
|
|
||
| ## Modules | ||
|
|
||
| Python workers can be split across multiple files. | ||
|
|
||
| Let's create a new Python file, called `src/hello.py`: | ||
|
|
||
| ```python | ||
| def hello(name): | ||
| return "Hello, " + name + "!" | ||
| ``` | ||
|
|
||
| Now, we can modify `src/entry.py` to make use of the new module. | ||
|
|
||
| ```python | ||
| from hello import hello | ||
| from workers import WorkerEntrypoint, Response | ||
|
|
||
| class Default(WorkerEntrypoint): | ||
| async def fetch(self, request): | ||
| return Response(hello("World")) | ||
| ``` | ||
|
|
||
| Once you edit `src/entry.py`, Wrangler will automatically detect the change and | ||
| reload your Worker. | ||
|
|
||
| ## Types and Autocompletion | ||
|
|
||
| When developing Python Workers, you can take advantage of type hints and autocompletion | ||
| in your IDE. | ||
|
|
||
| To enable them, install the `workers-runtime-sdk` package in your `pyproject.toml` file. | ||
|
|
||
| ```toml | ||
| [dependency-groups] | ||
| dev = [ | ||
| "workers-py", | ||
| "workers-runtime-sdk" | ||
| ] | ||
| ``` | ||
|
|
||
| Additionally, you can generate types based on your Worker configuration using `uv run pywrangler types` | ||
|
|
||
| This includes Env types based on your bindings, module rules, and runtime types based on the compatibility_date | ||
| and compatibility_flags in your config file. | ||
|
|
||
| ## Upgrading `pywrangler` | ||
|
|
||
| To upgrade to the latest version of `pywrangler`, run the following command: | ||
|
|
||
| ```bash | ||
| uv tool upgrade workers-py | ||
| ``` | ||
mikenomitch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Next Up | ||
|
|
||
| - Learn details about local development, deployment, and [how to Python Workers work](/workers/languages/python/how-python-workers-work). | ||
mikenomitch marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - Explore the [package](/workers/languages/python/packages) docs for instructions on how to use packages with Python Workers. | ||
| - Understand which parts of the [Python Standard Library](/workers/languages/python/stdlib) are supported in Python Workers. | ||
| - Learn about Python Workers' [foreign function interface (FFI)](/workers/languages/python/ffi), and how to use it to work with [bindings](/workers/runtime-apis/bindings) and [Runtime APIs](/workers/runtime-apis/). | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.