Skip to content

Commit c2b0400

Browse files
committed
Add Python code to Durable Object get-started.mdx.
1 parent 8c29672 commit c2b0400

File tree

2 files changed

+99
-7
lines changed

2 files changed

+99
-7
lines changed

src/components/TypeScriptExample.astro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ if (!raw) {
4141
raw = raw.replace(/\u007f/g, "\n");
4242
4343
const js = await format(tsBlankSpace(raw), { parser: "babel", useTabs: true });
44+
45+
const includeTabsDefinition = copy.attributes["omit-tabs"] != "true";
4446
---
4547

46-
<Tabs syncKey="workersExamples">
48+
{ includeTabsDefinition ? (<Tabs syncKey="workersExamples">) }
4749
<TabItem label="JavaScript" icon="seti:javascript">
4850
<Code
4951
{...code}
@@ -56,4 +58,4 @@ const js = await format(tsBlankSpace(raw), { parser: "babel", useTabs: true });
5658
<TabItem label="TypeScript" icon="seti:typescript">
5759
<Code {...code} lang="ts" code={raw} title={filename} />
5860
</TabItem>
59-
</Tabs>
61+
{ includeTabsDefinition ? (</Tabs>) }

src/content/docs/durable-objects/get-started.mdx

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ If you do not use JavaScript or TypeScript, you will need a [shim](https://devel
7272

7373
Your `MyDurableObject` class will have a constructor with two parameters. The first parameter, `ctx`, passed to the class constructor contains state specific to the Durable Object, including methods for accessing storage. The second parameter, `env`, contains any bindings you have associated with the Worker when you uploaded it.
7474

75-
<TypeScriptExample>
75+
<Tabs syncKey="workersExamples">
76+
77+
<TypeScriptExample omit-tabs="true">
7678
```ts
7779
export class MyDurableObject extends DurableObject<Env> {
7880
constructor(ctx: DurableObjectState, env: Env) {
@@ -83,19 +85,35 @@ export class MyDurableObject extends DurableObject<Env> {
8385
```
8486
</TypeScriptExample>
8587

88+
<TabItem label="Python" icon="seti:python">
89+
90+
```python
91+
from workers import DurableObject
92+
93+
class MyDurableObject(DurableObject):
94+
def __init__(ctx, env):
95+
super().__init__(ctx, env)
96+
```
97+
98+
</TabItem>
99+
100+
</Tabs>
101+
86102
Workers communicate with a Durable Object using [remote-procedure call](/workers/runtime-apis/rpc/#_top). Public methods on a Durable Object class are exposed as [RPC methods](/durable-objects/best-practices/create-durable-object-stubs-and-send-requests/) to be called by another Worker.
87103

88104
Your file should now look like:
89105

90-
<TypeScriptExample>
106+
<Tabs syncKey="workersExamples">
107+
108+
<TypeScriptExample omit-tabs="true">
91109
```ts
92110
export class MyDurableObject extends DurableObject<Env> {
93111
constructor(ctx: DurableObjectState, env: Env) {
94112
// Required, as we're extending the base class.
95113
super(ctx, env)
96114
}
97115

98-
async sayHello():Promise<string> {
116+
async sayHello(): Promise<string> {
99117
let result = this.ctx.storage.sql
100118
.exec("SELECT 'Hello, World!' as greeting")
101119
.one();
@@ -105,6 +123,26 @@ export class MyDurableObject extends DurableObject<Env> {
105123
```
106124
</TypeScriptExample>
107125

126+
<TabItem label="Python" icon="seti:python">
127+
128+
```python
129+
from workers import DurableObject
130+
131+
class MyDurableObject(DurableObject):
132+
def __init__(ctx, env):
133+
super().__init__(ctx, env)
134+
135+
async def say_hello(self):
136+
result = self.ctx.storage.sql
137+
.exec("SELECT 'Hello, World!' as greeting")
138+
.one()
139+
return result.greeting
140+
```
141+
142+
</TabItem>
143+
144+
</Tabs>
145+
108146
In the code above, you have:
109147

110148
1. Defined a RPC method, `sayHello()`, that can be called by a Worker to communicate with a Durable Object.
@@ -124,7 +162,10 @@ A Worker is used to [access Durable Objects](/durable-objects/best-practices/cre
124162

125163
To communicate with a Durable Object, the Worker's fetch handler should look like the following:
126164

127-
<TypeScriptExample>
165+
166+
<Tabs syncKey="workersExamples">
167+
168+
<TypeScriptExample omit-tabs="true">
128169
```ts
129170
export default {
130171
async fetch(request, env, ctx): Promise<Response> {
@@ -140,6 +181,24 @@ export default {
140181
```
141182
</TypeScriptExample>
142183

184+
<TabItem label="Python" icon="seti:python">
185+
186+
```python
187+
from workers import handler, Response
188+
189+
@handler
190+
async def on_fetch(request, env, ctx):
191+
url = urlparse(request.url)
192+
id = env.MY_DURABLE_OBJECT.idFromName(url.path)
193+
stub = env.MY_DURABLE_OBJECT.get(id)
194+
greeting = await stub.say_hello()
195+
return Response(greeting)
196+
```
197+
198+
</TabItem>
199+
200+
</Tabs>
201+
143202
In the code above, you have:
144203

145204
1. Exported your Worker's main event handlers, such as the `fetch()` handler for receiving HTTP requests.
@@ -217,7 +276,10 @@ Preview your Durable Object Worker at `<YOUR_WORKER>.<YOUR_SUBDOMAIN>.workers.de
217276

218277
Your final code should look like this:
219278

220-
<TypeScriptExample>
279+
280+
<Tabs syncKey="workersExamples">
281+
282+
<TypeScriptExample omit-tabs="true">
221283
```ts title="index.ts"
222284
import { DurableObject } from "cloudflare:workers";
223285
export class MyDurableObject extends DurableObject<Env> {
@@ -247,6 +309,34 @@ export default {
247309
```
248310
</TypeScriptExample>
249311

312+
<TabItem label="Python" icon="seti:python">
313+
314+
```python
315+
from workers import DurableObject, handler, Response
316+
317+
class MyDurableObject(DurableObject):
318+
def __init__(ctx, env):
319+
super().__init__(ctx, env)
320+
321+
async def say_hello(self):
322+
result = self.ctx.storage.sql
323+
.exec("SELECT 'Hello, World!' as greeting")
324+
.one()
325+
return result.greeting
326+
327+
@handler
328+
async def on_fetch(request, env, ctx):
329+
url = urlparse(request.url)
330+
id = env.MY_DURABLE_OBJECT.idFromName(url.path)
331+
stub = env.MY_DURABLE_OBJECT.get(id)
332+
greeting = await stub.say_hello()
333+
return Response(greeting)
334+
```
335+
336+
</TabItem>
337+
338+
</Tabs>
339+
250340
By finishing this tutorial, you have:
251341

252342
- Successfully created a Durable Object

0 commit comments

Comments
 (0)