How to pass an arbitrary argument to Flask through app.run()?

17,340

Solution 1

Well the script is executing from top to bottom, so you can't print something you don't have yet. Putting the print statement inside a classic flask factory function allow you to first parse command line, then get your object and then use it:

from flask import Flask

def create_app(foo):
    app = Flask(__name__)
    app.config['foo'] = foo
    print('Passed item: ', app.config['foo'])
    return app

if __name__ == '__main__':
  from argparse import ArgumentParser
  parser = ArgumentParser()
  parser.add_argument('-a')
  args = parser.parse_args()
  foo = args.a
  app = create_app(foo)
  app.run()

Solution 2

So, the problem is that you're trying to access the value before you define it. You would need to do something like this in your case:

from flask import Flask

app = Flask(__name__)
app.config['foo'] = 'Goo'

print('Passed item: ', app.config['foo'])

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

If you're trying to access that value while loading some third module, you'll need to define the value somewhere ahead of time.

Share:
17,340

Related videos on Youtube

Kyle Swanson
Author by

Kyle Swanson

I am a graduate student studying physics at the University of Nevada, Reno. Currently, my research is in high energy density physics, specifically laboratory astrophysics. Eventually I would like to be working on the frontier of space propulsion. I would like to make our future spaceships SUPER DUPER INSANELY FAST. Of course with racing stripes... Duhhh.. makes em go like 30% faster! My other interests include: music, art, languages, and travel. Oh also... coding... is awesome!

Updated on September 15, 2022

Comments

  • Kyle Swanson
    Kyle Swanson over 1 year

    I would like to pass an object to a newly initiated flask app. I tried following the solution from the question: how-can-i-make-command-line-arguments-visible-to-flask-routes

    Edit

    I would like to take a value that I pick up from initiating the python script from the command line.

    ie.

    $ run python flaskTest.py -a goo
    

    I am not seeing the difference between this and the solution to the question I am trying to replicate. Edit

    Thus, I tried the following:

    from flask import Flask
    
    app = Flask(__name__)
    
    print('Passed item: ', app.config.get('foo'))
    
    if __name__ == '__main__':
      from argparse import ArgumentParser
    
      parser = ArgumentParser()
      parser.add_argument('-a')
      args = parser.parse_args()
      val = args.a
    
      app.config['foo'] = val
      app.run()
    

    Hoping to get the result...

    'Passed item: Goo'
    

    Is there a method for passing an arbitrary object through the initialization with app.run()?

  • flbyrne
    flbyrne about 2 years
    Could you please give an example of how to run it when deployed to an url?
  • rrobby86
    rrobby86 about 2 years
    It depends on where and how it is deployed. If you somehow have a Python script calling/instantiating your Flask app (e.g. a .wsgi file on Apache+mod_wsgi) you call your factory function from the script and is easy to pass arguments. In other situations (e.g. using gunicorn, uwsgi or similar) try looking at the relevant documentation.