Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

python - fetching data from Flask REST service from laravel

I am trying to pass the path of an xml file to a flask/Python based REST-microservice. When passing the url manually to the service, it works as intended. However, I have problems calling the service from my laravel project.

Call to python rest service from laravel:

function gpxToJson(){
    url = "http://127.0.0.1:5000/convert?path=C:/xampp/htdocs/greenlaneAdventures/public/storage/gpx/OOCe4r5Mas8w94uGgEb8qYlI0T3ClZDoclcfrR7s.xml" ;
    fetch(url).then(function (response){
        var track = JSON.parse(response.json());
        return track;
    });
}

Python script:

import gpxpy
import pandas as pd
from flask_cors import CORS
from flask import Flask, request, make_response, render_template
from flask_restful import Resource, Api

# create empty web-application
app = Flask(__name__)
api = Api(app)
CORS(app)



# create necessary classes
class Converter(Resource):
    def get(self):
        # path passed as argument
        file = request.args.get('path', '')

        # parse gpx file
        gpx = gpxpy.parse(open(file))

        # define data
        track = gpx.tracks[0]
        segment = track.segments[0]

        # load wanted data into a panda dataframe
        data = []
        segment_length = segment.length_3d()
        for point_idx, point in enumerate(segment.points):
            data.append([point.longitude, point.latitude, point.elevation,
                         point.time, segment.get_speed(point_idx)])
        columns = ['Longitude', 'Latitude', 'Altitude', 'Time', 'Speed']
        gpx_df = pd.DataFrame(data, columns=columns)

        # convert dataframe to json
        gpx_json = gpx_df.to_json(orient='index')
        print(gpx_json)

        #return the converted json (http response code 200 = OK. The request has suceeded)
        headers = {"Content-Type": "application/json"}
        return make_response(gpx_json, 200, headers)

api.add_resource(Converter, "/convert")

#error handling
@app.errorhandler(404)
def not_found():
    """Page not found."""
    return make_response(
        render_template("404.html"),
        404
     )

@app.errorhandler(400)
def bad_request():
    """Bad request."""
    return make_response(
        render_template("400.html"),
        400
    )

@app.errorhandler(500)
def server_error():
    """Internal server error."""
    return make_response(
        render_template("500.html"),
        500
    )


#main function call
if __name__ == "__main__":
    app.run(debug=True)

When calling the function gpxToJson() by submitting a form, I get the following error message in firefox.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...