简体   繁体   中英

Is there a way to print Flask data back to React Front-end?

I have a React web-app running that sends data to a Flask back-end via a POST request. The back-end processes the data and needs to return the results back to the front-end. How do I print my results on the React front-end? Here is my Flask code:

from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import sys
import escode

app = Flask(__name__)
CORS(app)

@app.route('/result', methods = ['GET','POST'])

def result():
    SearchData = request.get_json()

    results = escode.retrieve_results( SearchData['MaskList'][0]['SpecChar'] )
    data = [doc for doc in results['hits']['hits']]
    for doc in data:
        print("(%s) %s"% (doc['_id'], doc['_source']['Name']), file=sys.stderr)

    return jsonify({ 'msg' : 'hi hello' }), 200

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

You can capture the data using useState of react by declaring const[getMessage,setMessage] = useState(''). Then pass the result to setMessage(response) in your api call which was returned by the flask and finally you can use getMessage in you html tags something like this:

{getMessage}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM