2828class UnsupportedButtonError (Exception ):
2929 pass
3030
31+ """
32+ This file contains examples of how to handle a user clicking a button on a canvas
33+ For examples of initialization, check out canvas_initialize.py
34+ """
3135
3236def route_interaction_webhook (app : App , canvas_interaction : CanvasInteractionWebhookV2 ) -> None :
3337 canvas_id = canvas_interaction .canvas_id
38+
39+ # Access the different actions by checking which button was clicked
3440 if canvas_interaction .button_id == SEARCH_BUTTON_ID :
3541 with app .create_session_context ("Search Chemicals" , timeout_seconds = 20 ) as session :
3642 session .attach_canvas (canvas_id )
43+
44+ # Use a CanvasBuilder to update an existing canvas
3745 canvas_builder = _canvas_builder_from_canvas_id (app , canvas_id )
3846 canvas_inputs = canvas_builder .inputs_to_dict_single_value ()
47+
48+ # Validate and sanitize the user's search input
3949 sanitized_inputs = _validate_and_sanitize_inputs (canvas_inputs )
50+
51+ # Search for the chemical
4052 results = search (sanitized_inputs [SEARCH_TEXT_ID ])
53+
54+ # Render the preview canvas
4155 render_preview_canvas (results , canvas_id , canvas_builder , session )
4256 elif canvas_interaction .button_id == CANCEL_BUTTON_ID :
4357 # Set session_id = None to detach and prior state or messages (essentially, reset)
@@ -46,12 +60,17 @@ def route_interaction_webhook(app: App, canvas_interaction: CanvasInteractionWeb
4660 .with_session_id (None )\
4761 .with_blocks (input_blocks ())\
4862 .to_update ()
63+
64+ # Update the canvas
4965 app .benchling .apps .update_canvas (canvas_id , canvas_update )
5066 elif canvas_interaction .button_id == CREATE_BUTTON_ID :
5167 with app .create_session_context ("Create Molecules" , timeout_seconds = 20 ) as session :
5268 session .attach_canvas (canvas_id )
69+
70+ # Use a CanvasBuilder to create a new molecule
5371 canvas_builder = _canvas_builder_from_canvas_id (app , canvas_id )
5472 molecule = _create_molecule_from_canvas (app , canvas_builder )
73+
5574 render_completed_canvas (molecule , canvas_id , canvas_builder , session )
5675 else :
5776 # Re-enable the Canvas, or it will stay disabled and the user will be stuck
0 commit comments