Basic Flask:添加有用的功能 [英] Basic Flask: Adding Helpful Functions

查看:185
本文介绍了Basic Flask:添加有用的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个在终端上运行的python脚本,并使用Flask把它移植到web上。我已经完成了教程的部分内容(特别是: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world



我正在努力将Python脚本中使用的所有函数放在哪里。 author()使用这个代码作为基本视图:

  def index():
user = {'nickname ':'Miguel'}#假用户
posts = [#虚假的帖子数组
{
'author':{'nickname':'John'},
'body ':'美丽的一天,在波特兰!'
},
{
'作者':{'昵称':'苏珊'},
'身体':'复仇者电影太棒了!'
}
]
return render_template(index.html,
title ='Home',
user = user,
posts = posts)

问题是我没有一个函数可以调用。我有15个左右,看起来像Flask只允许我调用每个视图的一个功能。所以我不太确定在哪里放置我的main函数将调用的所有帮助函数。

以作者的示例代码为例。如果我有一个返回post对象数组的函数 getPosts(),那么我会把它放在哪里?



即使我被允许把它放在路线的主要功能(我认为是不允许的),这样做似乎是一个糟糕的组织。



编辑:

这是我的views.py文件:

  1 flask导入烧瓶
2 app =烧瓶(__ name__)$ b $ 3从烧瓶导入render_template
4从应用导入应用
5从应用导入helpfulFunctions
6
7 def testFunction():
8 return 5;
9
10 @ app.route('/')
11 @ app.route('/ index')
12 def index():
13# allPlayers = processGender(mainURL,menTeams)
14 myNum = testFunction()
15返回render_template('index.html',title ='Home',user = user)


解决方案

您并不限于每个视图的一个函数 - 您可以拥有

  from flask import Flask 
app = Flask(__ name__)

def f ():
...
def g():
...
@ app.route('/ index')
def index():
<在这里你可以使用f和g>
...

函数不需要对应于视图 - 只有< code $ @ app.route(...)装饰者可以做到这一点。



如果你有很多其他的功能,把它们放在另一个文件里也不会有什么伤害的。然后你可以 import 这个文件并像上面一样使用它们。


I've written a python script that works in terminal and am porting it to the web using Flask. I've gone through parts of a tutorial (specifically: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world)

I'm struggling a bit with where to put all the functions I use in my Python script. The author () uses this code for a basic view:

def index():
    user = {'nickname': 'Miguel'}  # fake user
    posts = [  # fake array of posts
        { 
            'author': {'nickname': 'John'}, 
            'body': 'Beautiful day in Portland!' 
        },
        { 
            'author': {'nickname': 'Susan'}, 
            'body': 'The Avengers movie was so cool!' 
        }
    ]
    return render_template("index.html",
                           title='Home',
                           user=user,
                           posts=posts)

The problem is I don't have one function to call. I have 15 or so and it looks like Flask only lets me call one function per view. So I'm not really sure where to put all the helper functions that my "main" function will call.

Take the author's example code. If I had a function getPosts() that returns an array of post objects, where would I put this?

Even if I'm allowed to put it under the route's main function (which I don't think is allowed anyway), it seems like poor organization to do that.

Edit:

Here's my views.py file:

  1 from flask import Flask
  2 app = Flask(__name__)
  3 from flask import render_template
  4 from app import app
  5 from app import helpfulFunctions
  6
  7 def testFunction():
  8     return 5;
  9
 10 @app.route('/')
 11 @app.route('/index')
 12 def index():
 13     #allPlayers = processGender(mainURL, menTeams)
 14     myNum = testFunction()
 15     return render_template('index.html', title = 'Home', user = user)

解决方案

You're not restricted to one function per view -- you can have as many as you want.

from flask import Flask
app = Flask(__name__)

def f():
    ...
def g():
    ...
@app.route('/index')
def index():
    <here you can use f and g>
    ...

Functions don't need to correspond to views -- only the @app.route(...) decorator makes it do that.

If you have a large number of other functions, it couldn't hurt to put them in another file. Then you can import the file and use them as above.

这篇关于Basic Flask:添加有用的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆