Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +82 -1
src/streamlit_app.py
CHANGED
|
@@ -87,6 +87,86 @@ SAMPLE_STRUCTURES = {
|
|
| 87 |
"Ibuprofen": "ibuprofen.xyz"
|
| 88 |
}
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
|
| 92 |
# Custom logger that updates the table
|
|
@@ -302,7 +382,8 @@ if atoms is not None:
|
|
| 302 |
return view
|
| 303 |
|
| 304 |
# Display the 3D structure
|
| 305 |
-
view =
|
|
|
|
| 306 |
html_str = view._make_html()
|
| 307 |
st.components.v1.html(html_str, width=400, height=400)
|
| 308 |
|
|
|
|
| 87 |
"Ibuprofen": "ibuprofen.xyz"
|
| 88 |
}
|
| 89 |
|
| 90 |
+
def get_structure_viz2(atoms_obj, style='stick', show_unit_cell=True, width=400, height=400):
|
| 91 |
+
"""
|
| 92 |
+
Generate visualization of atomic structure with optional unit cell display
|
| 93 |
+
|
| 94 |
+
Parameters:
|
| 95 |
+
-----------
|
| 96 |
+
atoms_obj : ase.Atoms
|
| 97 |
+
ASE Atoms object containing the structure
|
| 98 |
+
style : str
|
| 99 |
+
Visualization style: 'ball_stick', 'stick', or 'ball'
|
| 100 |
+
show_unit_cell : bool
|
| 101 |
+
Whether to display unit cell for periodic systems
|
| 102 |
+
width, height : int
|
| 103 |
+
Dimensions of the visualization window
|
| 104 |
+
|
| 105 |
+
Returns:
|
| 106 |
+
--------
|
| 107 |
+
py3Dmol.view object
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
# Convert atoms to XYZ format
|
| 111 |
+
xyz_str = ""
|
| 112 |
+
xyz_str += f"{len(atoms_obj)}\n"
|
| 113 |
+
xyz_str += "Structure\n"
|
| 114 |
+
for atom in atoms_obj:
|
| 115 |
+
xyz_str += f"{atom.symbol} {atom.position[0]:.6f} {atom.position[1]:.6f} {atom.position[2]:.6f}\n"
|
| 116 |
+
|
| 117 |
+
# Create a py3Dmol visualization
|
| 118 |
+
view = py3Dmol.view(width=width, height=height)
|
| 119 |
+
view.addModel(xyz_str, "xyz")
|
| 120 |
+
|
| 121 |
+
# Set molecular style based on input
|
| 122 |
+
if style.lower() == 'ball_stick':
|
| 123 |
+
view.setStyle({'stick': {'radius': 0.1}, 'sphere': {'scale': 0.3}})
|
| 124 |
+
elif style.lower() == 'stick':
|
| 125 |
+
view.setStyle({'stick': {'radius': 0.15}})
|
| 126 |
+
elif style.lower() == 'ball':
|
| 127 |
+
view.setStyle({'sphere': {'scale': 0.4}})
|
| 128 |
+
else:
|
| 129 |
+
# Default to stick if unknown style
|
| 130 |
+
view.setStyle({'stick': {'radius': 0.15}})
|
| 131 |
+
|
| 132 |
+
# Add unit cell visualization for periodic systems
|
| 133 |
+
if show_unit_cell and any(atoms_obj.pbc):
|
| 134 |
+
cell = atoms_obj.get_cell()
|
| 135 |
+
|
| 136 |
+
# Define unit cell edges
|
| 137 |
+
origin = np.array([0.0, 0.0, 0.0])
|
| 138 |
+
edges = [
|
| 139 |
+
# Bottom face
|
| 140 |
+
(origin, cell[0]), # a
|
| 141 |
+
(origin, cell[1]), # b
|
| 142 |
+
(cell[0], cell[0] + cell[1]), # a+b from a
|
| 143 |
+
(cell[1], cell[0] + cell[1]), # a+b from b
|
| 144 |
+
# Top face
|
| 145 |
+
(cell[2], cell[2] + cell[0]), # a from c
|
| 146 |
+
(cell[2], cell[2] + cell[1]), # b from c
|
| 147 |
+
(cell[2] + cell[0], cell[2] + cell[0] + cell[1]), # a+b from c+a
|
| 148 |
+
(cell[2] + cell[1], cell[2] + cell[0] + cell[1]), # a+b from c+b
|
| 149 |
+
# Vertical edges
|
| 150 |
+
(origin, cell[2]), # c
|
| 151 |
+
(cell[0], cell[0] + cell[2]), # c from a
|
| 152 |
+
(cell[1], cell[1] + cell[2]), # c from b
|
| 153 |
+
(cell[0] + cell[1], cell[0] + cell[1] + cell[2]) # c from a+b
|
| 154 |
+
]
|
| 155 |
+
|
| 156 |
+
# Add unit cell lines
|
| 157 |
+
for start, end in edges:
|
| 158 |
+
view.addCylinder({
|
| 159 |
+
'start': {'x': start[0], 'y': start[1], 'z': start[2]},
|
| 160 |
+
'end': {'x': end[0], 'y': end[1], 'z': end[2]},
|
| 161 |
+
'radius': 0.05,
|
| 162 |
+
'color': 'black',
|
| 163 |
+
'alpha': 0.7
|
| 164 |
+
})
|
| 165 |
+
|
| 166 |
+
view.zoomTo()
|
| 167 |
+
view.setBackgroundColor('white')
|
| 168 |
+
|
| 169 |
+
return view
|
| 170 |
|
| 171 |
|
| 172 |
# Custom logger that updates the table
|
|
|
|
| 382 |
return view
|
| 383 |
|
| 384 |
# Display the 3D structure
|
| 385 |
+
view = get_structure_viz2(atoms, style='stick', show_unit_cell=True, width=400, height=400)
|
| 386 |
+
# view = get_structure_viz(atoms)
|
| 387 |
html_str = view._make_html()
|
| 388 |
st.components.v1.html(html_str, width=400, height=400)
|
| 389 |
|