import flask
from cref.app.web import app
from cref.app.web.tasks import predict_structure


def success(result):
    return flask.jsonify({
        'status': 'success',
        'retval': result
    })


def failure(reason='Unknown'):
    return flask.jsonify({
        'status': 'failure',
        'reason': reason
    })


@app.route('/predict/', methods=['POST'])
def predict():
    sequence = flask.request.get_json(force=True)['sequence']
    resp = predict_structure.delay(sequence)
    return success({'task_id': resp.id})


@app.route('/predict/<sequence>', methods=['GET'])
def predict_test(sequence):
    resp = predict_structure.delay(sequence)
    return success({'task_id': resp.id})


@app.route('/status/<task_id>')
def status(task_id):
    result = predict_structure.AsyncResult(task_id)
    return success({'state': result.state})


@app.route('/result/<task_id>')
def result(task_id):
    result = predict_structure.AsyncResult(task_id)
    if result.ready():
        return success({'pdb_file': result.get()})
    else:
        return failure('Task is pending')

import flask
from cref.app.web import app
from cref.app.web.tasks import predict_structure


def success(result):
    return flask.jsonify({
        'status': 'success',
        'retval': result
    })


def failure(reason='Unknown'):
    return flask.jsonify({
        'status': 'failure',
        'reason': reason
    })


@app.route('/predict/', methods=['POST'])
def predict():
    params = flask.request.get_json(force=True)
    resp = predict_structure.delay(params['sequence'])
    return success({'task_id': resp.id})


@app.route('/predict/<sequence>', methods=['GET'])
def predict_test(sequence):
    resp = predict_structure.delay(sequence)
    return success({'task_id': resp.id})


@app.route('/status/<task_id>')
def status(task_id):
    result = predict_structure.AsyncResult(task_id)
    return success({'state': result.state})


@app.route('/result/<task_id>')
def result(task_id):
    result = predict_structure.AsyncResult(task_id)
    if result.ready():
        return success({'pdb_file': result.get()})
    else:
        return failure('Task is pending')


@app.route('/predictions/<task_id>/<path:filename>')
def download_file(filename):
    return flask.send_from_directory(
        '/home/mchelem/dev/cref2/predictions/',
        filename, as_attachment=True
    )

Add method to serve prediction result files
Add method to serve prediction result files

  import flask
  from cref.app.web import app
  from cref.app.web.tasks import predict_structure
  
  
  def success(result):
      return flask.jsonify({
          'status': 'success',
          'retval': result
      })
  
  
  def failure(reason='Unknown'):
      return flask.jsonify({
          'status': 'failure',
          'reason': reason
      })
  
  
  @app.route('/predict/', methods=['POST'])
  def predict():
-     sequence = flask.request.get_json(force=True)['sequence']
+     params = flask.request.get_json(force=True)
-     resp = predict_structure.delay(sequence)
+     resp = predict_structure.delay(params['sequence'])
      return success({'task_id': resp.id})
  
  
  @app.route('/predict/<sequence>', methods=['GET'])
  def predict_test(sequence):
      resp = predict_structure.delay(sequence)
      return success({'task_id': resp.id})
  
  
  @app.route('/status/<task_id>')
  def status(task_id):
      result = predict_structure.AsyncResult(task_id)
      return success({'state': result.state})
  
  
  @app.route('/result/<task_id>')
  def result(task_id):
      result = predict_structure.AsyncResult(task_id)
      if result.ready():
          return success({'pdb_file': result.get()})
      else:
          return failure('Task is pending')
  
+ 
+ @app.route('/predictions/<task_id>/<path:filename>')
+ def download_file(filename):
+     return flask.send_from_directory(
+         '/home/mchelem/dev/cref2/predictions/',
+         filename, as_attachment=True
+     )
+ 
Add method to serve prediction result files
## Code Before:
import flask
from cref.app.web import app
from cref.app.web.tasks import predict_structure


def success(result):
    return flask.jsonify({
        'status': 'success',
        'retval': result
    })


def failure(reason='Unknown'):
    return flask.jsonify({
        'status': 'failure',
        'reason': reason
    })


@app.route('/predict/', methods=['POST'])
def predict():
    sequence = flask.request.get_json(force=True)['sequence']
    resp = predict_structure.delay(sequence)
    return success({'task_id': resp.id})


@app.route('/predict/<sequence>', methods=['GET'])
def predict_test(sequence):
    resp = predict_structure.delay(sequence)
    return success({'task_id': resp.id})


@app.route('/status/<task_id>')
def status(task_id):
    result = predict_structure.AsyncResult(task_id)
    return success({'state': result.state})


@app.route('/result/<task_id>')
def result(task_id):
    result = predict_structure.AsyncResult(task_id)
    if result.ready():
        return success({'pdb_file': result.get()})
    else:
        return failure('Task is pending')

## Instruction:
Add method to serve prediction result files
## Code After:
import flask
from cref.app.web import app
from cref.app.web.tasks import predict_structure


def success(result):
    return flask.jsonify({
        'status': 'success',
        'retval': result
    })


def failure(reason='Unknown'):
    return flask.jsonify({
        'status': 'failure',
        'reason': reason
    })


@app.route('/predict/', methods=['POST'])
def predict():
    params = flask.request.get_json(force=True)
    resp = predict_structure.delay(params['sequence'])
    return success({'task_id': resp.id})


@app.route('/predict/<sequence>', methods=['GET'])
def predict_test(sequence):
    resp = predict_structure.delay(sequence)
    return success({'task_id': resp.id})


@app.route('/status/<task_id>')
def status(task_id):
    result = predict_structure.AsyncResult(task_id)
    return success({'state': result.state})


@app.route('/result/<task_id>')
def result(task_id):
    result = predict_structure.AsyncResult(task_id)
    if result.ready():
        return success({'pdb_file': result.get()})
    else:
        return failure('Task is pending')


@app.route('/predictions/<task_id>/<path:filename>')
def download_file(filename):
    return flask.send_from_directory(
        '/home/mchelem/dev/cref2/predictions/',
        filename, as_attachment=True
    )
