from rdkit import Chem from rdkit.Chem import Draw
# Define the molecule precursor_smiles = "CC1=CN2C=CC=C2C(=N1)C" precursor_mol = Chem.MolFromSmiles(precursor_smiles)
# Generate SVG as a string drawer = Draw.MolDraw2DSVG(600, 600) # width, height in pixels drawer.DrawMolecule(precursor_mol) drawer.FinishDrawing() svg = drawer.GetDrawingText()
# Save SVG to file withopen("C9H10N2_structure_pyrazine.svg", "w") as f: f.write(svg)
3. SmileDrawer
SmileDrawer is a lightweight JS library that renders chemical structures from SMILES strings in the browser. It supports canvas and SVG, with optional export and styling.
I receommend this method with the nice looking and the interactive feature.
<scriptsrc="https://unpkg.com/smiles-drawer@1.0.10/dist/smiles-drawer.min.js"></script> <script> let input = document.getElementById("example-input"); let options = {}; // Initialize the drawer to draw to canvas let smilesDrawer = newSmilesDrawer.Drawer(options); // Alternatively, initialize the SVG drawer: // let svgDrawer = new SmilesDrawer.SvgDrawer(options); input.addEventListener("input", function() { // Clean the input (remove unrecognized characters, such as spaces and tabs) and parse it SmilesDrawer.parse(input.value, function(tree) { // Draw to the canvas smilesDrawer.draw(tree, "example-canvas", "light", false); // Alternatively, draw to SVG: // svgDrawer.draw(tree, 'output-svg', 'dark', false); }); }); </script> </body>
Comments