Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过Cron作业创建Flask网页-呼叫中断的url__Python_Cron_Flask_Jinja2_Url For - Fatal编程技术网

Python 通过Cron作业创建Flask网页-呼叫中断的url_

Python 通过Cron作业创建Flask网页-呼叫中断的url_,python,cron,flask,jinja2,url-for,Python,Cron,Flask,Jinja2,Url For,我的Flask应用程序中有一个脚本,每5分钟执行一次。原因很多。但这并不重要 当我运行此代码时,我现在希望在此网页中包含指向我的app.py应用程序中的函数的链接 app.py: @app.route('/Historical-Service-Transitions/<service>') @login_required @nocache def HSPC(service): return service Traceback (most recent call last):

我的Flask应用程序中有一个脚本,每5分钟执行一次。原因很多。但这并不重要

当我运行此代码时,我现在希望在此网页中包含指向我的app.py应用程序中的函数的链接

app.py

@app.route('/Historical-Service-Transitions/<service>')
@login_required
@nocache
def HSPC(service):
    return service
Traceback (most recent call last):
  File "kickoff.py", line 1199, in <module>
    the_time=the_time
  File "/usr/lib/python2.6/site-packages/Jinja2-2.7.3-py2.6.egg/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python2.6/site-packages/Jinja2-2.7.3-py2.6.egg/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<template>", line 534, in top-level template code
jinja2.exceptions.UndefinedError: 'url_for' is undefined
@app.route(“/Historical Service Transitions/”)
@需要登录
@诺卡奇
def HSPC(维修):
回程服务
按计划运行的kickoff.py

from jinja2 import Template
import paramiko 
import socket
import time
import pprint
import sys
import mysql.connector
from mysql.connector import errorcode
from collections import namedtuple
import datetime
from app import HSPC
from flask import url_for


source_html =Template(u'''
....#The part that is failing
<tbody>
{% for x in the_best %}
    <tr>
        <td><a href="{{ url_for('HSPC', service ='x.service') }}">{{x.service}}</a></td>
        <td>{{x.ip}}</td>
        <td>{{x.router}}</td>
        <td>{{x.detail}}</td>
        <td>{{x.time}}</td>
    </tr>
{% endfor %}
</tbody>
....''')

full_html = source_html.render(
the_best=the_best,
the_time=the_time
)


write_that_sheet = open("/var/www/flask-intro/templates/Test.html", "w+")
write_that_sheet.write(full_html)
write_that_sheet.close()
来自jinja2导入模板的

进口帕拉米科
导入套接字
导入时间
导入pprint
导入系统
导入mysql.connector
从mysql.connector导入错误代码
从集合导入namedtuple
导入日期时间
从应用程序导入HSPC
从的flask导入url_
source_html=模板(u''
..#失败的部分
{u best%}
{{x.ip}}
{{x.router}}
{{x.detail}}
{{x.time}
{%endfor%}
....''')
full\u html=source\u html.render(
最好的,
_时间=_时间
)
写下sheet=open(“/var/www/flask intro/templates/Test.html”,“w+”)
写那张纸。写(完整的html)
写下那张表。关闭()
错误

@app.route('/Historical-Service-Transitions/<service>')
@login_required
@nocache
def HSPC(service):
    return service
Traceback (most recent call last):
  File "kickoff.py", line 1199, in <module>
    the_time=the_time
  File "/usr/lib/python2.6/site-packages/Jinja2-2.7.3-py2.6.egg/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python2.6/site-packages/Jinja2-2.7.3-py2.6.egg/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<template>", line 534, in top-level template code
jinja2.exceptions.UndefinedError: 'url_for' is undefined
回溯(最近一次呼叫最后一次):
文件“kickoff.py”,第1199行,在
_时间=_时间
文件“/usr/lib/python2.6/site packages/Jinja2-2.7.3-py2.6.egg/Jinja2/environment.py”,第969行,在渲染中
返回self.environment.handle\u异常(exc\u info,True)
句柄中的文件“/usr/lib/python2.6/site packages/Jinja2-2.7.3-py2.6.egg/Jinja2/environment.py”,第742行
重放(exc_类型、exc_值、tb)
文件“”,第534行,顶层模板代码
jinja2.exceptions.UndelineError:“url_for”未定义
任何帮助都将不胜感激。


更新:

我找不到任何与我尝试做的事情非常接近的东西。我知道我可以重新构建它,这样背景Python代码就可以获取信息,而我的
app.py
可以构建HTML。后台应用程序将填充数据库,然后
app.py
中的函数将从数据库中获取所需信息并发布到HTML页面


虽然这是最后的手段,因为它要求我重新设计应用程序的整个部分,但我仍然想看看是否有一个解决方案可以让我在
app.py
Flask之外生成此网页。

您缺少Flask提供的Jinja上下文。用于呈现字符串中定义的模板,并将为您提供正确的模板上下文:

from flask import render_template_string

source_html = u'''
<tbody>
{% for x in the_best %}
    <tr>
        <td><a href="{{ url_for('HSPC', service ='x.service') }}">{{x.service}}</a></td>
        <td>{{x.ip}}</td>
        <td>{{x.router}}</td>
        <td>{{x.detail}}</td>
        <td>{{x.time}}</td>
    </tr>
{% endfor %}
</tbody>
'''

filename = "/var/www/flask-intro/templates/Test.html"

# provide a fake request context for the template
with app.test_request_context('/'), open(filename, "w") as outfh:
    full_html = render_template_string(
        source_html, the_best=the_best, the_time=the_time)
    outfh.write(full_html)
从flask导入渲染模板字符串
source_html=u''
{u best%}
{{x.ip}}
{{x.router}}
{{x.detail}}
{{x.time}
{%endfor%}
'''
filename=“/var/www/flask intro/templates/Test.html”
#为模板提供假请求上下文
使用app.test_request_上下文('/'),打开(文件名,“w”)作为outph:
完整的html=呈现模板字符串(
source\u html,the\u best=the\u best,the\u time=the\u time)
outph.write(完整的html)
但是,对于定期作业,您可以使用
curl
在Flask应用程序中调用一个特殊URL,让它将模板输出写入文件