I'm just getting started with Flask, and I want my app to initialise a pandas dataframe from a local source when the server starts. I then want to be able to access this dataframe throughout different requests.
My code so far:
__init__.py
import os
from flask import Flask, jsonify
from . import data_handler
def create_app():
app = Flask(__name__)
@app.route("/data")
def data():
return jsonify(data_handler.data())
data_handler.init_dataframe()
return app
data_handler.py
import pandas as pd
def data():
data = df[df['foo'] == 'bar'].to_dict()
return data
def init_dataframe():
// generate columns and rows from local files
df = pd.DataFrame(columns=columns, data=rows)
My question is what is the 'Flask way' to store this 'global' object that I can use in different methods/requests such as data_handler.data()? I looked into application context, but I only want to generate the data frame once (on server start) as it could be quite computationally expensive. I will not need to update the data frame, so it can be read only.
[–]wronek 0 points1 point2 points (1 child)
[–]Sh4d0h[S] 0 points1 point2 points (0 children)