Skip to content

Commit

Permalink
add hello and bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettgu10 committed Mar 29, 2024
0 parents commit 061f3c5
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/.wrangler
**/node_modules
17 changes: 17 additions & 0 deletions 01-hello/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Hello World Example

Warning: Python support in Workers is experimental and things will break. This
example is meant for reference only right now; you should be prepared to update
your code between now and official release time as APIs may change.

## How to Run
First ensure that your Wrangler version is up to date (3.30.0 and above).
```
$ wrangler -v
⛅️ wrangler 3.30.0
```

Now, if you run `wrangler dev` within this directory, it should use the config
in `wrangler.toml` to run the example.

You can also run `wrangler deploy` to deploy the example.
4 changes: 4 additions & 0 deletions 01-hello/src/entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from js import Response

async def on_fetch(request, env):
return Response.new("Hello world!")
4 changes: 4 additions & 0 deletions 01-hello/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = "hello-python-worker"
main = "src/entry.py"
compatibility_flags = ["python_workers"]
compatibility_date = "2024-03-29"
19 changes: 19 additions & 0 deletions 02-binding/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Bindings Example

Warning: Python support in Workers is experimental and things will break. This
example is meant for reference only right now; you should be prepared to update
your code between now and official release time as APIs may change.

## How to Run
First ensure that your Wrangler version is up to date (3.30.0 and above).
```
$ wrangler -v
⛅️ wrangler 3.30.0
```

Now, you must set up a [KV namespace](https://developers.cloudflare.com/kv/reference/kv-namespaces/). Edit `wrangler.toml` to reflect the KV namespace you created.

Now, if you run `wrangler dev` within this directory, it should use the config
in `wrangler.toml` to run the example.

You can also run `wrangler deploy` to deploy the example.
6 changes: 6 additions & 0 deletions 02-binding/src/entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from js import Response

async def on_fetch(request, env):
await env.FOO.put("bar", "baz")
bar = await env.FOO.get("bar")
return Response.new(bar) # returns "baz"
8 changes: 8 additions & 0 deletions 02-binding/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name = "hello-python-bindings"
main = "src/entry.py"
compatibility_flags = ["python_workers"]
compatibility_date = "2024-03-29"

kv_namespaces = [
{ binding = "FOO", id = "<YOUR_KV_NAMESPACE_ID>" }
]

0 comments on commit 061f3c5

Please sign in to comment.