Skip to content

Instantly share code, notes, and snippets.

@cthoyt
Created January 30, 2016 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cthoyt/6680e12299703f44a6ad to your computer and use it in GitHub Desktop.
Save cthoyt/6680e12299703f44a6ad to your computer and use it in GitHub Desktop.
Troubleshooting SMILES Server with Flask and RDKit
#!/usr/bin/env python3
from flask import Flask, request, make_response
from rdkit import Chem, rdBase
from rdkit.Chem import rdDepictor
from rdkit.Chem.Draw import rdMolDraw2D
app = Flask(__name__)
# Flask example: https://github.com/rduplain/flask-svg-example/blob/master/app.py
# svg generation: http://rdkit.blogspot.de/2015/02/new-drawing-code.html
@app.route('/pic.svg')
def draw_smiles():
#print(request.args)
smiles = request.args.get("smiles", "C=CC=O")
width = request.args.get("width", 400)
height = request.args.get("height", 200)
try:
smiles = smiles.strip()
mol = Chem.MolFromSmiles(smiles)
rdDepictor.Compute2DCoords(mol)
drawer = rdMolDraw2D.MolDraw2DSVG(width, height)
drawer.DrawMolecule(mol)
drawer.FinishDrawing()
svg = drawer.GetDrawingText().replace('svg:','')
# maybe cut off the first line with xml tag?
#svg = "\n".join(svg.split("\n")[1:])
response = make_response(svg)
response.content_type = 'image/svg+xml'
return response
except:
return "invalid input: " + smiles
if __name__ == "__main__":
app.run()
@cthoyt
Copy link
Author

cthoyt commented Jan 30, 2016

or more generally, I had the route as @app.route('/<smiles>') and smiles as the argument to draw_smiles rather than getting it as an argument from request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment