Vaishnav14220 commited on
Commit
f1e54b3
·
1 Parent(s): 03acb00

Update 3D molecule viewer to use 3Dmol.js instead of py3Dmol

Browse files
Files changed (2) hide show
  1. app.py +25 -12
  2. requirements.txt +1 -3
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  from rdkit import Chem
3
  from rdkit.Chem import Descriptors, Draw, AllChem
4
  import cirpy
5
- import py3Dmol
6
 
7
 
8
  # RDKit API with multiple endpoints
@@ -75,17 +74,31 @@ def name_to_3d_molecule(name: str) -> str:
75
  # Optimize geometry
76
  AllChem.MMFFOptimizeMolecule(mol)
77
 
78
- # Convert to PDB format for py3Dmol
79
- pdb_block = Chem.MolToPDBBlock(mol)
80
-
81
- # Create 3D viewer
82
- viewer = py3Dmol.view(width=400, height=400)
83
- viewer.addModel(pdb_block, 'pdb')
84
- viewer.setStyle({'stick': {}})
85
- viewer.zoomTo()
86
-
87
- # Return HTML representation
88
- return viewer._repr_html_()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  except Exception as e:
91
  raise gr.Error(f"Error creating 3D molecule: {str(e)}")
 
2
  from rdkit import Chem
3
  from rdkit.Chem import Descriptors, Draw, AllChem
4
  import cirpy
 
5
 
6
 
7
  # RDKit API with multiple endpoints
 
74
  # Optimize geometry
75
  AllChem.MMFFOptimizeMolecule(mol)
76
 
77
+ # Convert to SDF format (contains 3D coordinates)
78
+ sdf_string = Chem.SDWriter.GetText(mol)
79
+
80
+ # Create HTML with embedded 3D viewer using 3Dmol.js
81
+ html_content = f"""
82
+ <!DOCTYPE html>
83
+ <html>
84
+ <head>
85
+ <script src="https://3dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
86
+ </head>
87
+ <body>
88
+ <div id="container" style="width: 400px; height: 400px; position: relative;"></div>
89
+ <script>
90
+ let viewer = $3Dmol.createViewer($("#container"));
91
+ let sdf = `{sdf_string}`;
92
+ viewer.addModel(sdf, "sdf");
93
+ viewer.setStyle({{'stick': {{}}}});
94
+ viewer.zoomTo();
95
+ viewer.render();
96
+ </script>
97
+ </body>
98
+ </html>
99
+ """
100
+
101
+ return html_content
102
 
103
  except Exception as e:
104
  raise gr.Error(f"Error creating 3D molecule: {str(e)}")
requirements.txt CHANGED
@@ -2,6 +2,4 @@
2
  rdkit
3
  gradio==4.44.1
4
  huggingface_hub==0.19.4
5
- cirpy
6
- py3Dmol
7
- # Updated for 3D molecule viewer
 
2
  rdkit
3
  gradio==4.44.1
4
  huggingface_hub==0.19.4
5
+ cirpy