Skip to content

Commit 2224a1c

Browse files
Added Chemical Visualizer WebApp
1 parent acbe0c8 commit 2224a1c

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

Chemical Visualizer.png

43.8 KB
Loading

ChemicalVisualization.css

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
text-align: center;
4+
background-color: #f4f4f4;
5+
margin: 0;
6+
padding: 0;
7+
}
8+
9+
.container {
10+
max-width: 600px;
11+
margin: 50px auto;
12+
background: white;
13+
padding: 20px;
14+
border-radius: 8px;
15+
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
16+
}
17+
18+
h1 {
19+
color: #333;
20+
}
21+
22+
input {
23+
width: 80%;
24+
padding: 10px;
25+
margin-top: 10px;
26+
border: 1px solid #ccc;
27+
border-radius: 5px;
28+
}
29+
30+
button {
31+
padding: 10px 15px;
32+
margin-top: 10px;
33+
background: #28a745;
34+
color: white;
35+
border: none;
36+
border-radius: 5px;
37+
cursor: pointer;
38+
}
39+
40+
button:hover {
41+
background: #218838;
42+
}
43+
44+
.canvas-container {
45+
margin-top: 20px;
46+
}
47+
48+
/* Centering description text */
49+
.description {
50+
margin-top: 30px;
51+
padding: 10px;
52+
background-color: #f1f1f1;
53+
border-radius: 8px;
54+
font-size: 1rem;
55+
color: #555;
56+
max-width: 600px;
57+
margin-left: auto;
58+
margin-right: auto;
59+
text-align: center;
60+
}
61+

ChemicalVisualization.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>Chemical Visualizer</title>
7+
<link rel="stylesheet" href="ChemicalVisualization.css"> <!-- Updated CSS file name -->
8+
<!-- Include SmilesDrawer from a CDN -->
9+
<script src="https://unpkg.com/[email protected]/dist/smiles-drawer.min.js"></script>
10+
</head>
11+
<body>
12+
<!-- Navbar dynamically loaded here -->
13+
<div id="navbar-container"></div>
14+
<script>
15+
fetch('/navbar.html')
16+
.then(response => response.text())
17+
.then(data => document.getElementById('navbar-container').innerHTML = data);
18+
</script>
19+
20+
<div class="container">
21+
<h1>Chemical Visualizer</h1>
22+
<input type="text" id="chemicalFormula" placeholder="Enter chemical formula or IUPAC name (e.g., C6H6)">
23+
<button onclick="renderMolecule()">Visualize</button>
24+
<div class="canvas-container">
25+
<canvas id="moleculeCanvas" width="400" height="400"></canvas>
26+
</div>
27+
</div>
28+
29+
<!-- Description section at the bottom, centered -->
30+
<div class="description">
31+
<p>
32+
This web app allows users to input chemical names or formulas. It then uses PubChem's API to retrieve the corresponding SMILES notation for the molecule. The SMILES string is then rendered as a 2D visual representation using the SmilesDrawer JavaScript module.
33+
</p>
34+
</div>
35+
36+
<script src="ChemicalVisualization.js"></script> <!-- Updated JS file name -->
37+
</body>
38+
</html>

ChemicalVisualization.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Function to fetch SMILES from PubChem and render the molecule using SmilesDrawer
2+
function renderMolecule() {
3+
const formula = document.getElementById("chemicalFormula").value.trim();
4+
if (!formula) {
5+
alert("Please enter a valid chemical formula.");
6+
return;
7+
}
8+
9+
// Use PubChem API to fetch the Isomeric SMILES string for the given compound
10+
fetch(`https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/${formula}/property/IsomericSMILES/JSON`)
11+
.then(response => response.json())
12+
.then(data => {
13+
if (data.PropertyTable && data.PropertyTable.Properties.length > 0) {
14+
const smiles = data.PropertyTable.Properties[0].IsomericSMILES;
15+
// Use SmilesDrawer to parse and render the SMILES string
16+
SmilesDrawer.parse(smiles, function(tree) {
17+
const drawer = new SmilesDrawer.Drawer({width: 400, height: 400});
18+
drawer.draw(tree, 'moleculeCanvas', 'light', false);
19+
}, function(err) {
20+
console.error("Error parsing SMILES:", err);
21+
alert("Error parsing molecule data.");
22+
});
23+
} else {
24+
alert("Molecule not found. Try a different formula.");
25+
}
26+
})
27+
.catch(error => {
28+
console.error("Error fetching molecule data:", error);
29+
alert("Failed to retrieve molecule data.");
30+
});
31+
}
32+
33+
// Event listener to trigger renderMolecule function when "Enter" is pressed
34+
document.getElementById("chemicalFormula").addEventListener("keypress", function(event) {
35+
if (event.key === "Enter") {
36+
event.preventDefault(); // Prevent form submission (if wrapped in form)
37+
renderMolecule(); // Trigger the molecule rendering
38+
}
39+
});

0 commit comments

Comments
 (0)