Home > Mobile >  Updating DataFrame while API is running in Python
Updating DataFrame while API is running in Python

Time:03-10

So I am trying to create an API that constantly reads from a CSV and returns information about it when requested. So far, I have created a flask API that reads the CSV file once and returns correctly. However, I can't seem to make it constantly update. My working code is something like this.

app = flask.Flask(__name__)
app.config["DEBUG"] = True


dfchat = pd.read_csv(path)
escaper = None

# for now, this is just to make sure the program keeps running even if there is an error
def escape_route():
    global escaper
    while escaper != "Y":
        escaper = str(input("Exit now? Enter \'Y\': \n")).strip()
    os._exit(os.X_OK)
    
def sample_function(dfchat):

    @app.route('/sample_text', methods=['GET'])
    def sample_endpoint():
        # this function filters dfchat and returns whatever

def main():
    global dfchat
    escape_route_thread = threading.Thread(target = escape_route)
    escape_route_thread.start()
    sample_function(dfchat)
    app.run()


main()

I have tried creating another thread that updates the CSV file:

def retrieve_database():
    global dfchat
    while True:
        time.sleep(0.1)
        dfchat = pd.read_csv(path)

along with:

escape_route_thread = threading.Thread(target = retrieve_database)
escape_route_thread.start()

in the main function.

But that fails to update the dfchat data frame when the API launches. I have tested the thread by itself and it does update and return an updated data frame. From what I understand so far, once an API runs, python code cannot change the API itself.

So, Is there a way to update a running API with just python?

I'm asking for just python because I will not be able to manually enter a link like "/refresh" to do this. It has to be done by python.

Am I missing something?

Thank you very much for helping!

Edit:

I also tried to update the csv file for every API call. But that does but work either:

def sample_function():

    dfchat = pd.read_csv(path)
    @app.route('/sample_text', methods=['GET'])
    def sample_endpoint():
        # this function filters dfchat and returns whatever

CodePudding user response:

So I realized that the solution is really simple. I'm new to CS and APIs in general so I did not realize how @app.route worked in Flask. Outside of @app.route cannot change a route but updating a variable inside a route does work. I accidentally kept updating dfchat outside of @app.route.

def sample_function():

  # instead of putting dfchat here
  @app.route('/sample_text', methods=['GET'])
  def sample_endpoint():
      dfchat = pd.read_csv(path) # it should go here.
      # this function filters dfchat and returns whatever

Thank you yco for helping me realize this.

CodePudding user response:

Code defined at the root of the script (as was dfchat definition in your example) is executed once at the moment you start the flask server.

Code inside a Flask app route (function decorated with @app.route(...)) is executed at each API call to this route.

from flask import Flask

app = Flask(__name__)

path = "path/to/your/csv/file.csv"

@app.route('/sample_text', methods=['GET'])
def sample_endpoint():
  dfchat = pd.read_csv(path)
  # do what you have to do with the DF

Also note that Flask handles errors without stopping the API, and has great documentation to help you : https://flask.palletsprojects.com/en/2.0.x/quickstart/#a-minimal-application

  • Related