0

I am building a Web App in which the user should put some numbers , and server will calculate some functions and generates some plots. Calculation and Plotting happens in a separate module (*.py file). Now the problem is integrating them together.

lets say my Calculations happens in calculate.py and plotting.py ,

so I have to import them as :

import calculate
import plotting

depending on what user gives me as an input , I have to calculate and plot , so in the beginning I define a function for doing so

def calculation(a,b)
    #do something and 
    #save the plots 
    #and give me the result

and then my flask :

app = Flask(__name__)

if __name__ == '__main__':

    app.run(debug=True,port=8080)
    calculation(a,b)

but calculation function, never runs . Flask however runs properly and can also render my other html pages , but the calculation function never gets to run.

what seems to be the problem ? Moreover, I found out that print() function also doesnt work when flask app is initiated.

Soviut
  • 88,194
  • 49
  • 192
  • 260
  • In your code, you're not doing anything with the result of your `calculation()` function, so how do you know it didn't run? Does it have side effects? – brunns Apr 29 '19 at 15:35

3 Answers3

3

app.run blocks while flask is running. You have to have that function called via a flask route or called in the background beforehand.

Look into other options here Run code after flask application has started

CasualDemon
  • 5,790
  • 2
  • 21
  • 39
1

As mentioned above, app.run() runs an app and no code after that is started.

Also, your code doesn't make much sense. As you said, you want user to be able to make some calculations. So, you probably want him or her to input some data. You couldn't do that while starting an app.

So, as smallpants says, you want to make some page, which user can interact with. It can look like this (using smallpants answer):

@app.route('/calculate')
def calculation()
    a = 1
    b = 2
    return template(result.html, result=a+b)

- but here user cannot input anything.

So, maybe something like this:

@app.route('/calculate/<int:a>/<int:b>')
def calculation(a,b)
    return template(result.html, result=a+b)

- here user can input data to url.

Or maybe page with form (I recommand flask-wtforms)

To keep your app somewhat MVC, I'd recommand to make your page for input (should it be form, url parameter, ...), which then runs your calculation() from calculation.py file.

Good luck in learning Flask!

janpeterka
  • 568
  • 4
  • 17
0

You'll need to call that function from an app route. For example:

@app.route('/calculate')
def calculation(a,b)
    #do something and 
    #save the plots 
    #and give me the result

    return

This should be put inside your main app script for convenience.

If you really want to do it your way, then calculation(a,b) should be run above app.run() but obviously return something so it can be used elsewhere in the app. But the result can't be passed to app.run().

smallpants
  • 460
  • 7
  • 20