0

I made a script python app.py and I managed to store data from a javascript form, thanks to a flask app :

app = Flask(__name__)
app.config.from_object('config')
db.init_app(app)

@app.route('/getFormData', methods=['POST'])
def get_javascript_data():
    params = request.form.to_dict()
    sunElevation = params['sunElevation']
    cloudCoverage = params['cloudCoverage']
    thresholdNDVI = params['thresholdNDVI']
    limitScene = params['limitScene']
    city = params['city']
    data_search = passData(sunElevation, cloudCoverage, thresholdNDVI, limitScene, city)
    return jsonify(data_search.data_dict)

if __name__ == '__main__':
    app.run()

Here is the definition of the class passData, in models.py :

class passData:
    def __init__(self, sunElevation, cloudCoverage, thresholdNDVI, limitScene, city):
        self.sunElevation = sunElevation
        self.cloudCoverage = cloudCoverage
        self.thresholdNDVI = thresholdNDVI
        self.limitScene = limitScene
        self.city = city
        self.data_dict = [{'sunElevation':self.sunElevation,'cloudCoverage':self.cloudCoverage, 'thresholdNDVI':self.thresholdNDVI, 'limit':self.limitScene, 'city':self.city}]

I need to use those different parameters (sunElevation...) in an other script, in a other folder, to execute a search of imagery and then run the script associated. My problem is that I don't know how to pass those data, because they seem to only exist in the fonction defined in the get_javascript_data().

If someone has an idea that could help me !

1
  • 1
    why did you define passData as a class while it seems that you need to have it as a function that actually does something with its arguments? Rigth now you're just creating an instance of passData and do nothing with it. Commented Aug 1, 2017 at 9:46

2 Answers 2

1

You just have to import the other script, call the function and pass the parameters:

app = Flask(__name__)
app.config.from_object('config')
db.init_app(app)

from path.to.your.file import your_function


@app.route('/getFormData', methods=['POST'])
def get_javascript_data():
    params = request.form.to_dict()
    sunElevation = params['sunElevation']
    cloudCoverage = params['cloudCoverage']
    thresholdNDVI = params['thresholdNDVI']
    limitScene = params['limitScene']
    city = params['city']
    data_search = passData(sunElevation, cloudCoverage, thresholdNDVI, limitScene, city)

    # call the function and pass the parameters
    your_function(sunElevation, cloudCoverage) #...

    return jsonify(data_search.data_dict)
Sign up to request clarification or add additional context in comments.

9 Comments

I also did it exactly that way when I made a flask app, but did wonder at the time if it was the one and only correct way, although it worked.
It really depends on what this other function does. You can also create another function callable through ajax, or a method inside your class passData, but if it is a simple different script you can do like that.
Thanks ! But I have a problem to import the function, I'm not sure of the syntax of my path to the file, could you give me an example ? I put from ../../eosLandviewer/main_naturalite import search
You can't use "../" when importing. what is the structure of your project? Check this: flask.pocoo.org/docs/0.12/patterns/packages
In fact this is not a simple script, and inside I'm calling other functions. Someone told me the best way was to use a class and implement a method, but as I'm new to python, I don't see how to instance the method inside my class
|
0

Try adding the following code in your file, then import the script

import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))

from eosLandviewer.main_naturalite import search

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.