-1

I am taking input(city name) from the user using a form and using the input(city name) I am fetching a real time weather data after some pre processing a 'sample' numpy variable is created and I am trying to use 'sample' in a different funtion to display the output on a different page. Here is the app.py code:

from flask import Flask, request, jsonify, render_template
import requests 
import math
import numpy as np
import pickle

app = Flask(__name__)
modelMLR = pickle.load(open('modelMLR.pkl','rb'))

@app.route('/')
def home():
    return render_template('main.html')

@app.route('/algos', methods = ['GET','POST'])
def algos():
    city = request.form['location']
    api_address = "https://api.openweathermap.org/data/2.5/weather?appid=33a306ae5533eae0fe34e94953cde0a7&q="
    url = api_address+city
    json_data = requests.get(url).json()
    #for Multiple linear Regression
    L1=[]
    #temperature 
    kelvin_temp = json_data["main"]["temp_min"]
    celcisus_temp = kelvin_temp - 273.15
    fahrenheit_temp = celcisus_temp * ( 9 / 5 ) + 32
    formatted_data1 = math.floor(fahrenheit_temp)
    L1.append(formatted_data1)
    kelvin_temp = json_data["main"]["temp_max"]
    celcisus_temp = kelvin_temp - 273.15
    fahrenheit_temp = celcisus_temp * ( 9 / 5 ) + 32
    formatted_data2 = math.floor(fahrenheit_temp)
    L1.append(formatted_data2)
    #Humidity
    L1.append(80)
    formatted_data4 = json_data["main"]["humidity"]
    L1.append(formatted_data4)
    #sea level pressure
    L1.append(990)
    L1.append(1010)
    formatted_data5 = json_data["main"]["pressure"]
    L1.append(formatted_data5)
    #cloud cover
    formatted_data6 = json_data["clouds"]["all"]
    L1.append(formatted_data6)
    L1.append(formatted_data6)
    L1.append(formatted_data6)
    L1.append(formatted_data6)
    #shortwave radiation
    L1.append(0)
    #wind speed
    L1.append(0)
    formatted_data7 = json_data["wind"]["speed"]
    L1.append(formatted_data7)   
    #wind gust
    L1.append(0)
    L1.append(0)
    sample = np.array(L1)
    sample= sample.reshape(1, -1)

    return render_template("algos.html")

@app.route('/MLR')
def predict_MLR():
    prediction = modelMLR.predict(sample)
    return render_template('MLR.html', prediction_text = "The current precipitation is: {}".format(prediction)) 

if __name__ == "__main__":
    app.run(debug = False)

I want to use variable 'sample' of algos() function in predict_MLR() function so that I can predict the output and display it on MLR.html page.

main.html:

<!doctype html>
<body>
<h1> Rainfall prediction.. </h1><br>
<form method = "POST" action="{{url_for('algos')}}">
<button type="button"><img width="13px" height="13px" src="back.jpg"></button>
<input type = "text" placeholder="Search for your city.." style="width:300px" name = "location">
<button type = "submit" value="submit"><a href = "algos.html"><img src = "search.png" height="13px" width="13px"></a></button>
</form>
</body>
</html>

algos.html:

    <!doctype html>
    <html>
    <body>
    <h1> Rainfall prediction.. </h1><br>
    <h1><a href = "MLR">Multiple Linear Regression</a><h1>
    </body>
    </html>

MLR.html:

<!doctype html>
<html>
<h1> Rainfall prediction.. </h1><br> 
<h2>{{prediction_text}}</h2>
</html>
davidism
  • 121,510
  • 29
  • 395
  • 339
  • If you don't want to it to be global, you could return it in the function that calculates it. And then call that function in your predict function. – Sri Apr 14 '20 at 20:33
  • Look at the answer from VC.One in this thread: https://stackoverflow.com/questions/56088646/flask-pass-variable-from-one-function-to-another-function – Jan Michael Wallace Apr 14 '20 at 20:37

1 Answers1

0

A simpler way to write this would be to first define the agios function which returns sample:

def algos(city):    
    api_address = "https://api.openweathermap.org/data/2.5/weather?appid=33a306ae5533eae0fe34e94953cde0a7&q="
    url = api_address+city
    json_data = requests.get(url).json()
    #for Multiple linear Regression
    L1=[]
    #temperature 
    kelvin_temp = json_data["main"]["temp_min"]
    celcisus_temp = kelvin_temp - 273.15
    fahrenheit_temp = celcisus_temp * ( 9 / 5 ) + 32
    formatted_data1 = math.floor(fahrenheit_temp)
    L1.append(formatted_data1)
    kelvin_temp = json_data["main"]["temp_max"]
    celcisus_temp = kelvin_temp - 273.15
    fahrenheit_temp = celcisus_temp * ( 9 / 5 ) + 32
    formatted_data2 = math.floor(fahrenheit_temp)
    L1.append(formatted_data2)
    #Humidity
    L1.append(80)
    formatted_data4 = json_data["main"]["humidity"]
    L1.append(formatted_data4)
    #sea level pressure
    L1.append(990)
    L1.append(1010)
    formatted_data5 = json_data["main"]["pressure"]
    L1.append(formatted_data5)
    #cloud cover
    formatted_data6 = json_data["clouds"]["all"]
    L1.append(formatted_data6)
    L1.append(formatted_data6)
    L1.append(formatted_data6)
    L1.append(formatted_data6)
    #shortwave radiation
    L1.append(0)
    #wind speed
    L1.append(0)
    formatted_data7 = json_data["wind"]["speed"]
    L1.append(formatted_data7)   
    #wind gust
    L1.append(0)
    L1.append(0)
    sample = np.array(L1)
    sample= sample.reshape(1, -1)

    return sample

Then have a single route which returns a different result based on the request.method:

@app.route('/')
def home():
    if request.method == 'POST':
        city = request.form['location']
        sample = algos(city)
        prediction = modelMLR.predict(sample)
        return render_template('main.html', prediction_text = "The current precipitation is: {}".format(prediction)) 
    else:
        # GET request, just render the home page
        return render_tempate('main.html')

if __name__ == "__main__":
    app.run(debug = False)

Notice that prediction_text is only passed through in the event of a POST request.

Now just define a template which handles this case with an if clause:

<!doctype html>
<body>
  <h1> Rainfall prediction.. </h1><br>
  <form method = "POST" action="{{url_for('home')}}">
    <button type="button"><img width="13px" height="13px" src="back.jpg"></button>
    <input type = "text" placeholder="Search for your city.." style="width:300px" name = "location">
    <input type = "submit" value="submit" />
  </form>

  {% if prediction_text %}
    <h1> Rainfall prediction.. </h1><br> 
    <h2>{{prediction_text}}</h2>
  {% endif %}
</body>
</html>

In the event that you hit the / endpoint with a GET request, the prediction is not shown.

v25
  • 7,096
  • 2
  • 20
  • 36