#πŸ”’ flask application context error

25 messages Β· Page 1 of 1 (latest)

shell pike
#

I got an error message that sent me here https://flask.palletsprojects.com/en/stable/appcontext/

and I'm not sure where to put that

def create_app():
    app = Flask(__name__)

    with app.app_context():
        init_db()

    return app

code snippet that it sounds like I need to avoid this error

(that I still don't understand because reading the page just says "hey we do this so we don't get recursive issues" but I don't understand this kind of vague statement without an example to compare it to.)

https://paste.pythondiscord.com/3SRQ traceback here

languid quiverBOT
#

@shell pike

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wet lantern
#

can you show the CO2 function, as well as where it's called from?

shell pike
#

tried to add error handling and that managed to break the functions (this is the first one in the file, oh goodie)

wet lantern
#

ok, what about where it's being called from? (File "/var/www/html/app.py", line 346, in <module>)

shell pike
#

the functions are at the bottom of the file

wet lantern
#

when you wrap something in app.route, Flask takes care of providing the app context to your route-handler function

#

but when you call it manually, that doesn't happen

shell pike
#

like the defining them then using them

wet lantern
#

can you share the entire file?

wet lantern
shell pike
shell pike
wet lantern
#

they're supposed to be called by Flask automatically when the corresponding HTTP route is hit

shell pike
#

so I don't need to have them at the bottom like that?

wet lantern
#

nope. in fact, you shouldn't.

shell pike
#

OK I"m going to change the code and try running it again

#

it works now

#

thank you so so much

wet lantern
#

no problem!

#

@shell pike if you're done with this thread, you can close it by sending !close

shell pike
#

!close

languid quiverBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.