Vaishnav14220 commited on
Commit
d64616a
·
1 Parent(s): 994cda0

Add auto-render SVG on selection and common reaction presets

Browse files
Files changed (1) hide show
  1. app.py +42 -2
app.py CHANGED
@@ -581,24 +581,64 @@ def build_interface() -> gr.Blocks:
581
  outputs=[detail_markdown, dataset_table, reaction_plot, reaction_svg],
582
  )
583
 
 
 
 
 
 
 
 
584
  with gr.Tab("Reaction SVG"):
585
  gr.Markdown(
586
  "Render an RDKit reaction sketch from reaction SMILES/SMARTS. "
587
  "Example: `CCO.O=C=O>>CC(=O)O` or `[CH3:1].[Cl:2][C@@H](F)[Br]>>[CH3:1][C@@H](F)[Cl]`."
588
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
589
  reaction_input = gr.Textbox(
590
  label="Reaction SMILES/SMARTS",
591
  placeholder="Reactant1.Reactant2>>Product1.Product2",
592
  lines=2,
593
  )
 
 
 
 
 
 
 
 
 
 
 
 
594
  render_button = gr.Button("Render Reaction", variant="secondary")
595
- reaction_svg = gr.HTML()
596
  render_status = gr.Markdown()
597
 
598
  render_button.click(
599
  fn=render_reaction_svg,
600
  inputs=reaction_input,
601
- outputs=[reaction_svg, render_status],
602
  )
603
 
604
  return demo
 
581
  outputs=[detail_markdown, dataset_table, reaction_plot, reaction_svg],
582
  )
583
 
584
+ # Auto-render SVG when selection changes
585
+ selection.change(
586
+ fn=fetch_detail,
587
+ inputs=[selection, manual_url],
588
+ outputs=[detail_markdown, dataset_table, reaction_plot, reaction_svg],
589
+ )
590
+
591
  with gr.Tab("Reaction SVG"):
592
  gr.Markdown(
593
  "Render an RDKit reaction sketch from reaction SMILES/SMARTS. "
594
  "Example: `CCO.O=C=O>>CC(=O)O` or `[CH3:1].[Cl:2][C@@H](F)[Br]>>[CH3:1][C@@H](F)[Cl]`."
595
  )
596
+
597
+ # Common reaction examples
598
+ common_reactions = [
599
+ ("Ethanol esterification", "CCO.CC(=O)O>>CC(=O)OCC.O"),
600
+ ("Methane combustion", "C.O>>CO2"),
601
+ ("Ethylene hydration", "C=C.O>>CCO"),
602
+ ("Acetylene + HBr", "C#C.Br>>C=CBr"),
603
+ ("Benzene nitration", "c1ccccc1.O=N(=O)O>>c1ccc(cc1)[N+](=O)[O-].O"),
604
+ ("Methyl radical + Ethane", "[CH3].CC>>[CH4].C"),
605
+ ("Chlorine + Hydrogen", "Cl.C>>CCl"),
606
+ ("Propane oxidation", "CCC.O>>CC(C)=O"),
607
+ ]
608
+
609
+ with gr.Row():
610
+ reaction_preset = gr.Dropdown(
611
+ label="Common Reactions",
612
+ choices=[label for label, _ in common_reactions],
613
+ interactive=True
614
+ )
615
+ preset_dict = {label: smiles for label, smiles in common_reactions}
616
+
617
  reaction_input = gr.Textbox(
618
  label="Reaction SMILES/SMARTS",
619
  placeholder="Reactant1.Reactant2>>Product1.Product2",
620
  lines=2,
621
  )
622
+
623
+ def populate_from_preset(preset_name):
624
+ if preset_name and preset_name in preset_dict:
625
+ return preset_dict[preset_name]
626
+ return ""
627
+
628
+ reaction_preset.change(
629
+ fn=populate_from_preset,
630
+ inputs=reaction_preset,
631
+ outputs=reaction_input,
632
+ )
633
+
634
  render_button = gr.Button("Render Reaction", variant="secondary")
635
+ reaction_svg_output = gr.HTML()
636
  render_status = gr.Markdown()
637
 
638
  render_button.click(
639
  fn=render_reaction_svg,
640
  inputs=reaction_input,
641
+ outputs=[reaction_svg_output, render_status],
642
  )
643
 
644
  return demo