0

App works perfectly locally but crushes after deployment- seems like it does so when I get to points where operations on variables take place.

I've looked up other similar questions but with no luck.

I assume I am making some fundamental mistake however I am not able to identify it. I was thinking perhaps something with Flask app session settings and the way requests are handled.

It's my first independent project and I realize that there must be more pythonic way of achieving things however I was just focusing on problem solving with this one.

After deployment app either crushes (NoneType object) or goes into the loop looking like variables are not being set at all- none of which happens when app is tested locally.

Fragment of code (up to the point where app crushes- I don't want to spam entire code here):

from flask import Flask, render_template, request, flash, redirect, url_for
import codecs
import datetime
from sq_search import *
from list_creator import list_creator


app = Flask(__name__)


app.secret_key= 'dev'




# Global variables created- to avoid "not defined error" in certain parts of script-
# depending on user choice

stype=None 
namesearch=None
final_name=None
results=None
ticker_result=None
name_result=None
company=None
from_date=None
to_date=None
disable_1st=None
email_name=None





@app.route('/')
def home():
# Setting global variables used in search to None- in case user stops search, goes to 
# other page and returns back to search- avoids errors 
    global stype, namesearch, final_name, results, ticker_result, name_result, company
    
    stype=None 
    namesearch=None
    final_name=None
    results=None
    ticker_result=None
    name_result=None
    company=None

    return render_template("home.html")




@app.route('/seng/', methods=['POST','GET'])
def seng():
    global stype, namesearch, final_name, results, ticker_result, name_result
    
    search_options_title="Chosen search options:"

# Using 'try except' block to avoid errors- in case user presses go back or refreshes the
# page the script will execute further and eventuall assign variables to None letting
# the user start new search rather than returning error
   
    if stype == None:

        try:
            
            if request.method=="POST":
                global search_options
                stype=request.form["stype"]

                if stype == 'name':
                    
                    search_options=[">Chosing by Name"]
                    
                    return render_template("seng.html", pic1="pic1.html", search_options=search_options_title+"<br><br>", search_by=search_options[0],
                                                choice2="choice2.html")

                if stype == 'ticker':
                    
                    search_options=[">Chosing by ticker"]
                    return render_template("seng.html", pic1="pic1.html", search_options=search_options_title+"<br><br>", search_by=search_options[0],
                                                choice2="choice2tick.html")
        except:
            pass


    if namesearch==None and stype=='ticker':
        try:
            if request.method=="POST":

                ticker_search=request.form["tickersearch"].upper()
                
                get_ticker(ticker_search)

                if ticker_result:
                        stype=None
                        return redirect(url_for('final_stage'))

                else:
                    ticker_search=None
                    notick_error="<p style='font-size: 1.4vw;color: red'>Ticker incorrect! Inster S&P 500 ticker, search again by name or browse all companies from main menu</p>"
                    return render_template("seng.html", pic1="pic1.html", search_options=search_options_title+"<br><br>", search_by=search_options[0],
                                                    choice2="choice2tick.html", notick_error=notick_error)
        except:
            stype=None
            pass


    elif namesearch==None and stype=='name':
        if len(search_options) > 1: # Delets previously used search from right side menu if there was one already
            del search_options[1]

        

        if request.method=="POST":
            try:

                namesearch=request.form["namesearch"]
                if namesearch != None:
                
                    get_names(namesearch)
                    
                    if results:
                        list_creator(results) # Creates HTML script with drop down list of all matches
                        search_options.append(namesearch)
                        number_of_options=f"<br><p style='font-size: 1.3vw'>Number of matching results: {len(results)}</p>"
                        results=None
                        namesearch=None
                        
                        return render_template("seng.html",pic1="pic1.html", pic2="pic2.html", search_options=search_options_title+"<br><br>", 
                                                search_by=search_options[0]+"<br><br>", search_name=">Look for: '"+search_options[1]+"'<br>",
                                                number_of_options=number_of_options, choice3="choice3.html")
                        
                    else:
                        
                        noname_error= "<br><p style='font-size: 1.4vw;color: red'>No matches- no such company in S&P 500. Broaden the search or browse all companies in main menu</p>"
                        results=None
                        namesearch=None
                        return render_template("seng.html", pic1="pic1.html", search_options=search_options_title+"<br><br>", search_by=search_options[0],
                                                choice2="choice2.html", noname_error=noname_error)
            except:
                

                stype=None
                namesearch=None
                results=None                # Setting all variables to None- in case user went back a page during search-
                ticker_result=None          # otherwise would return an error
                final_name=None
                name_result=None

           
    if final_name==None:
       
        try:
            if request.method=="POST":
                final_name=request.form["name_final"]
                name_result=get_all_byname(final_name) # Function retrives full data based on final user choice from drop down list
                return redirect(url_for('final_stage'))
        except:
            pass

    else:
        namesearch=None # Same reason as previously- avoiding errors
        stype=None
        final_name=None
        results=None
        ticker_result=None
        name_result=None
        
     
    return render_template("seng.html", choice1="choice1.html")
Bart
  • 76
  • 6

1 Answers1

1

The interpreter is confused about choosing definition for variable. Why do you keep global in function? Anything outside function is global. In case of switching contexts, you could use Flask session or Flask g.

Official Documentation

Henshal B
  • 1,540
  • 12
  • 13
  • Thanks for your answer. Either because some of them are defined in function and afterwards accessed by another function or to use conditional != None so I make sure there are no mess ups when user refreshes/goes back a page. Also I have some jinja conditionals in HTML templates so in certain parts of scripts I might need to use defined variable few times. Does it mean that I will have to rewrite entire code to adapt all variables to either use them with Flask-Session or Flask-Cache? There is no way to use them on live server in the current shape? – Bart Apr 23 '21 at 22:52