Skip to content

Commit d79cebe

Browse files
committed
Adds ffi properly
1 parent 7bc3653 commit d79cebe

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

07-durable-objects/src/entry.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
from workers import WorkerEntrypoint, Response, DurableObject
2-
2+
from pyodide.ffi import to_js
33
from urllib.parse import urlparse
4-
import json
54

65
class List(DurableObject):
76
async def get_messages(self):
87
messages = await self.ctx.storage.get("messages")
9-
return json.loads(messages) if messages else []
8+
return messages if messages else []
109

1110
async def add_message(self, message):
1211
messages = await self.get_messages()
1312
messages.append(message)
14-
await self.ctx.storage.put("messages", json.dumps(messages))
13+
await self.ctx.storage.put("messages", to_js(messages))
1514
return
1615

1716

@@ -21,7 +20,7 @@ async def fetch(self, request):
2120

2221
list_id = url.path.split("/")[1]
2322
if list_id == "":
24-
return Response("List ID not specified", status=400)
23+
return Response("Go to /<list_id>/show to see messages\nGo to /<list_id>/add/<message> to add a message", status=400)
2524

2625
do_id = self.env.LISTS.idFromName(list_id)
2726
stub = self.env.LISTS.get(do_id)

07-durable-objects/uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ Need to deploy your Worker to Cloudflare? Python Workers are in open beta and ha
2121
- [**`04-query-d1/`**](04-query-d1) - shows how to query D1 with Python Workers
2222
- [**`05-langchain/`**](05-langchain) — demonstrates how to use the [LangChain](https://pypi.org/project/langchain/) package with Python Workers. Currently broken.
2323
- [**`06-assets/`**](06-assets) — An example with an assets binding.
24+
- [**`07-durable-objects/`**](06-durable-objects) — An example with storing state in a [Durable Object](https://developers.cloudflare.com/durable-objects/).
25+
- [**`08-cron/`**](08-cron) — shows a simple [cron job](https://developers.cloudflare.com/workers/configuration/cron-triggers/).
26+
- [**`09-workers-ai/`**](09-workers-ai) makes a call [Workers AI](https://developers.cloudflare.com/workers-ai/) to run inference on Cloudflare's Global Network.
27+
- [**`10-workflows/`**](09-workflows) — shows a durable [Workflows](https://developers.cloudflare.com/workflows/) example.
2428

2529

2630

2731
## Open Beta and Limits
2832

29-
- Python Workers are in open beta. You can use packages in your Workers by vendoring them using the [pywrangler](https://github.com/cloudflare/workers-py?tab=readme-ov-file#pywrangler) tool.
33+
- Python Workers are in open beta. You can use packages in your Workers by using the [pywrangler](https://github.com/cloudflare/workers-py?tab=readme-ov-file#pywrangler) tool.
3034
- You must add the `python_workers` compatibility flag to your Worker while Python Workers are in open beta.
3135

3236
We’d love your feedback. Join the `#python-workers channel` in the [Cloudflare Developers Discord](https://discord.cloudflare.com/) and let us know what you’d like to see next.

0 commit comments

Comments
 (0)