Created
January 30, 2016 20:47
-
-
Save cthoyt/6680e12299703f44a6ad to your computer and use it in GitHub Desktop.
Troubleshooting SMILES Server with Flask and RDKit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
or more generally, I had the route as
@app.route('/<smiles>')
and smiles as the argument todraw_smiles
rather than getting it as an argument fromrequest