简体   繁体   English

如何通过 app.run() 将任意参数传递给 Flask?

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

I would like to pass an object to a newly initiated flask app.我想将 object 传递给新启动的 flask 应用程序。 I tried following the solution from the question: how-can-i-make-command-line-arguments-visible-to-flask-routes我尝试按照问题的解决方案: 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.我想采用从命令行启动 python 脚本时获取的值。

ie. 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()?是否有通过 app.run() 初始化传递任意 object 的方法?

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: 将print语句放在经典的烧瓶工厂函数中允许您首先解析命令行,然后获取您的对象然后使用它:

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()

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. 如果您在加载某个第三个模块时尝试访问该值,则需要提前在某处定义该值。

An update for more recent versions of Flask: when running through flask run you can now invoke an app factory and even pass arguments ( docs ).更新 Flask 版本的更新:运行flask run时,您现在可以调用应用程序工厂,甚至可以传递 arguments ( docs )。

Example code:示例代码:

from flask import Flask

def create_app(foo=None):
    app = Flask(__name__)
    app.config["foo"] = foo
    return app

# if __name__ == "__main__": ... not necessary

Assuming it is saved as flaskTest.py , you can run it using:假设它保存为flaskTest.py ,您可以使用以下方式运行它:

export FLASK_APP="flaskTest:create_app('value of foo')"
flask run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM