Skip to content

Commit 8be742f

Browse files
authored
Add Python example to d1 tutorial (#24789)
Added Python code to the tab switcher for the d1 tutorial.
1 parent dfbbc9d commit 8be742f

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/content/docs/d1/get-started.mdx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ After you have set up your database, run an SQL query from within your Worker.
324324
2. Clear the content of `index.ts`.
325325
3. Paste the following code snippet into your `index.ts` file:
326326

327-
<TypeScriptExample filename="index.ts">
327+
<Tabs syncKey="workersExamples">
328+
<TypeScriptExample filename="index.ts" omitTabs={true}>
328329
```typescript
329330
export interface Env {
330331
// If you set another name in the Wrangler config file for the value for 'binding',
@@ -353,6 +354,29 @@ After you have set up your database, run an SQL query from within your Worker.
353354
} satisfies ExportedHandler<Env>;
354355
```
355356
</TypeScriptExample>
357+
<TabItem label="Python" icon="seti:python">
358+
```python title="entry.py"
359+
from workers import Response, WorkerEntrypoint
360+
from urllib.parse import urlparse
361+
362+
class Default(WorkerEntrypoint):
363+
async def fetch(self, request):
364+
pathname = urlparse(request.url).path
365+
if pathname == "/api/beverages":
366+
query = (
367+
await self.env.DB.prepare(
368+
"SELECT * FROM Customers WHERE CompanyName = ?",
369+
)
370+
.bind("Bs Beverages")
371+
.run()
372+
)
373+
return Response.json(query.results)
374+
return Response(
375+
"Call /api/beverages to see everyone who works at Bs Beverages"
376+
)
377+
```
378+
</TabItem>
379+
</Tabs>
356380

357381
In the code above, you:
358382

0 commit comments

Comments
 (0)