简体   繁体   English

在Python上使用app.route循环

[英]Loop using app.route on Python

I'm trying to create several URLs on my serv thanks to a loop . 由于循环,我试图在serv上创建几个URL。 The issue is that each function I create in a app.route can't have the same name than the others . 问题是我在app.route中创建的每个函数的名称不能相同。 And I don't know how to create different function names ... Here is the code : 而且我不知道如何创建不同的函数名称...这是代码:

json_tweets = []

for line in open('C:\Users\Benjamin\Desktop\DashboardProject\last_rated_set.json',"r"):
    json_tweets.append(json.loads(line,"ISO-8859-1"))

cashtag_tab = []  

for tweet in json_tweets:
    if not(tweet['cashtag'] in cashtag_tab) :
        cashtag_tab.append(tweet['cashtag'])        

for i in range(0,(len(cashtag_tab)-1)) :
    var=cashtag_tab[i]
    @app.route("/"+var)
    def company(var) :
        finance=Share(var)
        datas =  finance.get_historical('2014-01-01', '2014-12-31')
        datas = json.dumps(datas, default=json_util.default)    
        return datas

I'm getting the error AssertionError : View function mapping is overwritting an existing endpoint function : company 我收到错误AssertionError:视图函数映射覆盖了现有的端点函数:company

This fails because Flask derives the endpoint name from the function by default, but it would anyway fail later because the function company requires an argument var and the route is not parameterised. 之所以失败是因为Flask默认情况下是从该函数派生端点名称的,但是稍后它将失败,因为该函数company需要一个参数var且该路由未参数化。 The simplest option would be just checking the value inside the handler: 最简单的选择是只检查处理程序中的值:

@api.route('/<var>')
def company(var):
    if var not in cashtag_tab:
        abort(404)

If you want all the routes to be in the routing map for any reason, I once needed a similar thing and came up with something like this: 如果您出于某种原因希望所有路线都在路线图中,那么我曾经需要类似的东西,并提出了类似以下内容:

def url_family(source, methods=('GET',)):
    def decorator(f):
        for entry in source:
            # create a handler that delegates to your function
            def view_func(entry=entry, **kwargs):
                return f(entry, **kwargs)

            endpoint = '{0}_{1}'.format(f.__name__, entry)
            url = '/{0}'.format(entry)

            api.add_url_rule(url, 
                             methods=methods,
                             endpoint=endpoint,
                             view_func=view_func)

    return decorator

Then you register the handlers as: 然后将处理程序注册为:

@url_family(cashtag_tab)
def company(var):
    ...

Assuming that you are using flask now, you should consider Custom URL Converter. 假设您现在正在使用flask,则应考虑使用Custom URL Converter。 Check links below 检查下面的链接

http://flask.pocoo.org/docs/0.10/api/#flask.Flask.url_map - url_map UrlConverter API https://exploreflask.com/views.html#url-converters - example url converter https://stackoverflow.com/a/5872904/3451543 - RegexConverter by Philip Southam http://flask.pocoo.org/docs/0.10/api/#flask.Flask.url_map-url_map UrlConverter API https://exploreflask.com/views.html#url-converters-示例网址转换器https:// stackoverflow .com / a / 5872904/3451543 - Philip Southam的 RegexConverter

Anyway, specifying more details on your question is always helpful to get accurate answer :) 无论如何,为您的问题指定更多详细信息始终有助于获得准确的答案:)

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

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