PYTHON   82

aapi

Guest on 1st May 2022 12:02:31 AM

  1. from scripts import test2
  2. from flask import Flask
  3. from flask_restful import Resource, Api, reqparse
  4. import pandas as pd
  5. import ast
  6. from flask_cors import CORS
  7.  
  8.  
  9. # PERMANENT_PATH= "C:/xampp/htdocs/ocr/Backend/API/"
  10. PERMANENT_PATH = ""
  11. URL = "/"
  12.  
  13. app = Flask(__name__)
  14. api = Api(app)
  15. CORS(app, supports_credentials=True)
  16.  
  17.  
  18. class Users(Resource):
  19.     # methods go here
  20.     def post(self):
  21.  
  22.         parser = reqparse.RequestParser()  # initialize
  23.  
  24.         parser.add_argument("data", required=True)
  25.  
  26.         args = parser.parse_args()  # parse arguments to dictionary
  27.  
  28.         # create new dataframe containing new values
  29.         new_data = pd.DataFrame({"index": [[]], "data": args["data"]})
  30.         # read our CSV
  31.         data = pd.read_csv(PERMANENT_PATH + "users.csv")
  32.         # add the newly provided values
  33.         data = data.append(new_data, ignore_index=True)
  34.         text = test2.text_to_image(new_data)
  35.         # save back to CSV
  36.         data.to_csv(PERMANENT_PATH + "users.csv", index=False)
  37.         return {"data": URL + "/documents/MyFile.docx", "text": text}, 200
  38.  
  39.     def get(self):
  40.  
  41.         data = pd.read_csv("./users.csv")  # read CSV
  42.         data = data.to_dict()  # convert dataframe to dictionary
  43.         # return {"data": data}, 200  # return data and 200 OK code
  44.         return {"data": "works"}, 200  # return data and 200 OK code
  45.  
  46.     pass
  47.  
  48.  
  49. class Locations(Resource):
  50.     def get(self):
  51.  
  52.         data = pd.read_csv("./locations.csv")  # read CSV
  53.         data = data.to_dict()  # convert dataframe to dictionary
  54.         return {"data": data}, 200  # return data and 200 OK code
  55.         # return {'data': "works"}, 200  # return data and 200 OK code
  56.  
  57.     pass
  58.  
  59.  
  60. api.add_resource(Users, "/users")  # '/users' is our entry point for Users
  61. api.add_resource(
  62.     Locations, "/locations"
  63. )  # and '/locations' is our entry point for Locations
  64.  
  65.  
  66. if __name__ == "__main__":
  67.     app.run(host="0.0.0.0", port=81)  # run our Flask app

Raw Paste


Login or Register to edit or fork this paste. It's free.