Skip to content

Commit 435a5eb

Browse files
committed
Use Python SDK in all examples.
1 parent 660337a commit 435a5eb

File tree

7 files changed

+25
-14
lines changed

7 files changed

+25
-14
lines changed

01-hello/src/entry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from js import Response
1+
from workers import Response
22

33
async def on_fetch(request, env):
4-
return Response.new("Hello world!")
4+
return Response("Hello world!")

01-hello/wrangler.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
name = "hello-python"
2-
main = "src/entry.py"
2+
main = "src/scratch.py"
33
compatibility_flags = ["python_workers"]
44
compatibility_date = "2024-03-29"

02-binding/src/entry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from js import Response
1+
from workers import Response
22

33
async def on_fetch(request, env):
44
await env.FOO.put("bar", "baz")
55
bar = await env.FOO.get("bar")
6-
return Response.new(bar) # returns "baz"
6+
return Response(bar) # returns "baz"

04-langchain/src/worker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from js import Response
1+
from workers import Response
22
from langchain_core.prompts import PromptTemplate
33
from langchain_openai import OpenAI
44

@@ -8,4 +8,4 @@ async def on_fetch(request, env):
88
chain = prompt | llm
99

1010
res = await chain.ainvoke({"profession": "electrician"})
11-
return Response.new(res.split(".")[0].strip())
11+
return Response(res.split(".")[0].strip())

05-query-d1/README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@ Warning: Python support in Workers is experimental and things will break. This d
55
Currently, Python Workers using packages cannot be deployed and will only work in local development for the time being.
66

77
### How to Run
8+
89
Download this quotes file from [Hugging Face in SQL form](https://huggingface.co/datasets/lizziepika/quotes_sql/blob/main/data.sql)
910

1011
Create a new D1 database by running on the command line `npx wrangler d1 create {D1-NAME}`. Copy and paste the output into your `wrangler.toml` to bind your D1 database to your Python Worker.
1112

13+
Ingest the SQL file you've downloaded by running:
14+
15+
```
16+
npx wrangler d1 execute {D1-NAME} --local --file=./data.sql
17+
```
18+
1219
Ensure that your Wrangler version is up to date (3.30.0 and above).
1320

1421
```
1522
$ wrangler -v
1623
⛅️ wrangler 3.30.0
17-
```
18-
Now, if you run `wrangler dev` within this directory, it should use the config in`wrangler.toml` to run the demo.
24+
```
25+
26+
Now, if you run `wrangler dev` within this directory, it should use the config in `wrangler.toml` to run the demo.
1927

20-
You can also run `wrangler deploy` to deploy the demo.
28+
You can also run `wrangler deploy` to deploy the demo (though you'll need to repeat the ingestion step above for the remote DB).
2129

22-
Finally, get a random quote from the database by visiting your deployed worker in the browser!<img width="1421" alt="deployed app" src="https://github.com/user-attachments/assets/131a2836-2305-4b73-a54a-50dac039108f">
30+
Finally, get a random quote from the database by visiting your deployed worker in the browser!<img width="1421" alt="deployed app" src="https://github.com/user-attachments/assets/131a2836-2305-4b73-a54a-50dac039108f">

05-query-d1/src/entry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from js import Response
1+
from workers import Response
22

33
async def on_fetch(request, env):
44
query = """
@@ -11,4 +11,4 @@ async def on_fetch(request, env):
1111
data = results.results[0]
1212

1313
# Return a JSON response
14-
return Response.json(data)
14+
return Response.json(data)

05-query-d1/wrangler.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#:schema node_modules/wrangler/config-schema.json
22
compatibility_date = "2024-08-06"
3+
main = "src/entry.py"
4+
compatibility_flags = ["python_workers"]
5+
36

47
[ai]
58
binding = "AI"
69

710
[[d1_databases]]
811
binding = "DB" # i.e. available in your Worker on env.DB
912
database_name = "quotes" # REPLACE WITH YOUR DB NAME
10-
database_id = "408cddb7-e3f7-40d7-a4f7-33136a7fd3fa" # REPLACE WITH YOUR DB ID
13+
database_id = "408cddb7-e3f7-40d7-a4f7-33136a7fd3fa" # REPLACE WITH YOUR DB ID

0 commit comments

Comments
 (0)