Skip to content

Commit 1389b7e

Browse files
Added docs for canvas code
1 parent 8ba8759 commit 1389b7e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

examples/chem-sync-local-flask/local_app/benchling_app/handler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,25 @@ class UnsupportedWebhookError(Exception):
2525

2626
def handle_webhook(webhook_dict: dict[str, Any]) -> None:
2727
logger.debug("Handling webhook with payload: %s", webhook_dict)
28+
29+
# Receive the webhook and initialize the app
2830
webhook = WebhookEnvelopeV0.from_dict(webhook_dict)
2931
app = init_app_from_webhook(webhook)
32+
33+
# Route the webhook to the appropriate handler
3034
# Could also choose to route on webhook.message.type
35+
3136
# Note: if your manifest specifies more than one item in `features`,
3237
# then `webhook.message.feature_id` may also need to be part of your routing logic
3338
try:
3439
if isinstance(webhook.message, CanvasInitializeWebhookV2):
40+
# This webhook is triggered when a canvas is initialized as a part of a run
3541
render_search_canvas(app, webhook.message)
3642
elif isinstance(webhook.message, CanvasInteractionWebhookV2):
43+
# This webhook is triggered when a user clicks a button on the canvas
3744
route_interaction_webhook(app, webhook.message)
3845
elif isinstance(webhook.message, CanvasCreatedWebhookV2Beta):
46+
# This webhook is triggered when a canvas is created in an entry
3947
render_search_canvas_for_created_canvas(app, webhook.message)
4048
else:
4149
# Should only happen if the app's manifest requests webhooks that aren't handled in its code paths

examples/chem-sync-local-flask/local_app/benchling_app/views/canvas_initialize.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616

1717
from local_app.benchling_app.views.constants import SEARCH_BUTTON_ID, SEARCH_TEXT_ID
1818

19+
"""
20+
This file contains examples of how to handle different Canvas Webhooks
21+
22+
Use a CanvasBuilder to create or update a canvas associated with your app
23+
This sdk tool can be used for easy creation and updates
24+
25+
Check out https://benchling.com/sdk-docs/1.22.0/benchling_sdk.apps.canvas.framework.html
26+
for more details on the CanvasBuilder class
27+
"""
28+
1929

2030
def render_search_canvas(app: App, canvas_initialized: CanvasInitializeWebhookV2) -> None:
2131
with app.create_session_context("Show Sync Search", timeout_seconds=20):
@@ -24,7 +34,11 @@ def render_search_canvas(app: App, canvas_initialized: CanvasInitializeWebhookV2
2434
feature_id=canvas_initialized.feature_id,
2535
resource_id=canvas_initialized.resource_id,
2636
)
37+
38+
# Add the input blocks to the canvas
2739
canvas_builder.blocks.append(input_blocks())
40+
41+
# Create the canvas
2842
app.benchling.apps.create_canvas(canvas_builder.to_create())
2943

3044

@@ -36,6 +50,13 @@ def render_search_canvas_for_created_canvas(app: App, canvas_created: CanvasCrea
3650

3751

3852
def input_blocks() -> list[UiBlock]:
53+
"""
54+
Create 3 blocks to populate the canvas:
55+
56+
Markdown block to display text
57+
TextInput block to capture user input
58+
Button block to trigger the search
59+
"""
3960
return [
4061
MarkdownUiBlock(
4162
id="top_instructions",

0 commit comments

Comments
 (0)