from flask import Flask, jsonify, request, Response

import openbabel_api as openbabel

app = Flask(__name__)


@app.route('/convert/<output_format>', methods=['POST'])
def convert(output_format):
    """Convert molecule data from one format to another via OpenBabel

    The output format is specified in the path. The input format and
    the data are specified in the body (in json format) as the keys
    "format" and "data", respectively.

    Some options may also be specified in the json body. The following
    options will be used in all cases other than the special ones:

        gen3d (bool): should we generate 3d coordinates?
        addHydrogens (bool): should we add hydrogens?
        outOptions (dict): what extra output options are there?

    Special cases are:
        output_format:
            svg: returns the SVG
            smi: returns canonical smiles
            inchi: returns json containing "inchi" and "inchikey"

    Curl example:
    curl -X POST 'http://localhost:5000/convert/inchi' \
      -H "Content-Type: application/json" \
      -d '{"format": "smiles", "data": "CCO"}'
    """
    json_data = request.get_json()
    input_format = json_data['format']
    data = json_data['data']

    # Treat special cases with special functions
    out_lower = output_format.lower()
    if out_lower == 'svg':
        data, mime = openbabel.to_svg(data, input_format)
    elif out_lower in ['smiles', 'smi']:
        data, mime = openbabel.to_smiles(data, input_format)
    elif out_lower == 'inchi':
        inchi, inchikey = openbabel.to_inchi(data, input_format)
        d = {
            'inchi': inchi,
            'inchikey': inchikey
        }
        return jsonify(d)
    else:
        # Check for a few specific arguments
        gen3d = json_data.get('gen3d', False)
        add_hydrogens = json_data.get('addHydrogens', False)
        out_options = json_data.get('outOptions', {})

        data, mime = openbabel.convert_str(data, input_format, output_format,
                                           gen3d=gen3d,
                                           add_hydrogens=add_hydrogens,
                                           out_options=out_options)
    return Response(data, mimetype=mime)


if __name__ == '__main__':
    app.run(host='0.0.0.0')

@@ -2233,16 +2233,618 @@
 =mime)%0A%0A
+@app.route('/atom_count', methods=%5B'POST'%5D)%0Adef atom_count():%0A    %22%22%22Get the number of atoms in a molecule%0A%0A    The input format and the data are specified in the body (in json%0A    format) as the keys %22format%22 and %22data%22, respectively.%0A%0A    Curl example:%0A    curl -X POST 'http://localhost:5000/atom_count' %5C%0A      -H %22Content-Type: application/json%22 %5C%0A      -d '%7B%22format%22: %22smiles%22, %22data%22: %22CCO%22%7D'%0A    %22%22%22%0A    json_data = request.get_json()%0A    input_format = json_data%5B'format'%5D%0A    data = json_data%5B'data'%5D%0A%0A    # int output is not accepted%0A    return str(openbabel.atom_count(data, input_format))%0A
 %0Aif __na

Add endpoint to get atom count