jinja2

flask 渲染jinja2模版和传参

ぐ巨炮叔叔 提交于 2020-03-21 10:20:13
渲染模版(html文件) A、模版文件(html)放入到template目录下,项目启动的时候会从template目录里查找, B、从flask中导入“render_tempalte”函数 C、在视图函数中,使用render_template函数,渲染模版(只需要填写模版名称即可) 示例: from flask import Flask,url_for,redirect,render_template      #导入模版函数 app = Flask(__name__) @app.route('/') def index(): info = {                    #定义字典 'username' :'name', 'gender':"man", 'height' : "178" }      #如果有多个参数,可以将所有的参数放到字典中,然后以**kwargs的方式传递进去,info为上面定义的字典 return render_template('index.html',**info)      #这里直接写模版文件名称,如果在模版文件在temlate/html目录下,则这里需要写'html/index.html'      #渲染模版,传参数,如果参数较少,可以直接写关键字参数及值,如下:   #return render_template('index

Jinja-like for Pdf in Python

人走茶凉 提交于 2020-03-17 05:44:48
问题 I am looking for the best accurate tool for PDF in Python that works like Jinja does for HTML. What are your suggestions? 回答1: As answered by jbochi, ReportLab is the foundation for almost all Python projects that generate PDF. But for your needs you might want to check out Pisa / xhtml2pdf. You would generate your HTML with a Jinja template and then use Pisa to convert the HTML to PDF. Pisa is built on top of ReportLab. Edit: another option I'd forgotten about is wkhtmltopdf 回答2: Have a look

Docker容器技术-在开发中引用Docker

随声附和 提交于 2020-03-13 09:21:53
明确一点: 容器不适合构建那种发布周期以周或月为单位的大型单一架构企业软件,容器适合采用微服务的方式,以及探索诸如持续部署这样的技术,使得我们能安全地在一天内多次更新生产环境。 一、在开发中引用Docker 1.Hello World [root@bogon ~]# tree identidock/ identidock/ └── app └── identidock.py 1 directory, 1 file [root@bogon ~]# cat identidock/app/identidock.py from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!\n' if __name__ == '__main__': app.run(debug=True,host='0.0.0.0') [root@bogon identidock]# cat Dockerfile FROM python:3.4 RUN pip install Flask==0.10.1 WORKDIR /app COPY app /app CMD ["python","identidock.py"] [root@bogon identidock]# docker

Ansible dictionary key as variable

两盒软妹~` 提交于 2020-03-01 07:19:57
问题 Let's have something like this in role defaults/main.yml: num: 0 config: 0: a: true b: 'x' 1: a: false b: 'y' 2: a: false b: 'z' Now I send -e num=1 in playbook call, and I want to use values a and b based on this value somewhere else in the role, something like: aValue: '{{config[num].a}}' bValue: '{{config[num].b}}' How do I do that? I tried aValue: '{{config[num].a}}' but got an error: 'dict object' has no attribute u'1' aValue: '{{config["num"].a}}' but got an error: 'dict object' has no

python学习之flask基础

感情迁移 提交于 2020-02-26 09:31:53
什么是Flask? Flask是一个Web框架,就是提供一个工具,库和技术来允许你构建一个Web应用程序.这个Web应用程序 可以是一些Web页面,博客, wiki ,基于 Web 的日历应用或商业网站。 Flask依赖模块: web服务网关接口(Python Web Server Gateway Interface,缩写为WSGI Werkzeug 一个WSGI工具包, 是为python语言定义的web服务器和web应用程序或框架之间的一 种简单而通用的借口,其他语言也有类似的接口) jinja2模板引擎 Flask的优势 Flask属于微框架( micro-framework )这一类别,微架构通常是很小的不依赖外部库的框架. 框架很轻量 更新时依赖小 专注于安全方面的bug 第一个flask程序 from flask import Flask app = Flask(__name__) #导入Flask对象 @app.route('/') #把修饰的函数注册为路由 def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run() 变量规则 要给url添加变量部分,可以把这些特殊的字符标记为<variable_name> 这部分将会作为命名参数传递到你的函数 from flask

Django 模板语言 标签

偶尔善良 提交于 2020-02-25 14:16:34
前言:django的模板语法基本和flask的jinja2基本一样。下面比较一下两个模板语法的区别。 ------ 深度变量的查找(万能的句点号) 在 Django 模板中遍历复杂数据结构的关键是句点字符 ( . )。 1.模板变量 django :{{ 变量 }} # 因为django只有一个context返回,全部数据都集中在一起 jinja2 :{{ 对象.变量 }} 2.根据列表的下标获取值 django :{{ 列表.0 }} jinja2 :{{ 列表[0] }} 3.根据字典的键获取字典的值 django :{{ 字典.key }} jinja2 :{{ 字典[key] }}或者{{ 字典.key }} 4.for循环时取序号 django:{% for item in 列表 %} {{forloop.counter}} <1-- 表示当前是第几次循环,从1开始 --> {{forloop.counter0}} <!-- 表示当前是第几次循环,从0开始 -->{% endfor %}jinja2:{% for item in 列表 %} {{loop.index}} <1-- 表示当前是第几次循环,从1开始 --> {{loop.index0}} <!-- 表示当前是第几次循环,从0开始 -->{% endfor %} # for遍历字典{{ for key,val

jinja2批量生成python脚本

帅比萌擦擦* 提交于 2020-02-25 08:04:25
​ 在使用airflow的过程中需要大量的dag脚本进行性能测试,如果一个个去编写dag脚本未免太过麻烦,于是想到用python的jinja2模板引擎实现批量脚本生成。 先通过pip命令安装jinja2模块: $ pip install jinja2 然后创建模板文件(模板可以是任何形式的文本格式,没有特定扩展名,甚至可以不要扩展名): dag_template from datetime import timedelta, datetime import pytz from airflow.operators.bash_operator import BashOperator from airflow.operators.dummy_operator import DummyOperator from airflow.models import DAG default_args = { 'owner': 'cord', # 'depends_on_past': False, 'depends_on_past': True, # 'start_date': airflow.utils.dates.days_ago(2), 'wait_for_downstream': True, 'execution_timeout': timedelta(minutes=3), 'email':

flask基础1

别等时光非礼了梦想. 提交于 2020-02-24 03:03:39
可以利用Jinja2模板引擎技术,而不需要从函数返回硬编码HTML。 Jinja2模板引擎使用以下分隔符来从HTML转义。 {% ... %} 用于多行语句 {{ ... }} 用于将表达式打印输出到模板 {# ... #} 用于未包含在模板输出中的注释 # ... ## 用于单行语句 来自客户端网页的数据作为全局请求对象发送到服务器。要处理请求数据,请求对旬应该从Flask模块导入。 请求对象的重要属性如下所列 - form - 它是包含表单参数及其值的键和值对的字典对象。 args - 解析问号( ? )后的URL部分查询字符串的内容。 cookies - 保存Cookie名称和值的字典对象。 file - 与上传文件有关的数据。 method - 当前请求方法。 在URL规则中指定http方法。URL映射的函数接收到的表单数据可以以字典对象的形式收集,并将其转发给模板以在相应的网页上呈现它。 模板其实是一个包含响应文本的文件,其中用占位符(变量)表示动态部分,告诉模板引擎其具体的值需要从使用的数据中获取 使用真实值替换变量,再返回最终得到的字符串,这个过程称为“渲染” Flask是使用 Jinja2 这个模板引擎来渲染模板 视图函数只负责业务逻辑和数据处理(业务逻辑方面) 而模板则取到视图函数的数据结果进行展示(视图展示方面) 代码结构清晰,耦合度低 模板: Jinja2:是

Pyinstaller Jinja2 TemplateNotFound

余生颓废 提交于 2020-02-23 09:03:08
问题 I am using pyinstaller to build my flask application, everything is working fine except I get problems with Jinja2 templates. It gave me jinja2.exceptions.TemplateNotFound , I tried to put from app import template which is the templates folder, but it didn't work (I guess since they don't contain any py file). I also tried changing the .spec file to include the templates folder added_files = [ ( '..\\CommerceApp\\app\\templates', 'templates' ), ( '..\\CommerceApp\\app\\static', 'static' ) ] a

Translating %% with gettext and jinja2 and pyramid

混江龙づ霸主 提交于 2020-02-22 08:11:04
问题 Doing i18n work with Python using Jinja2 and Pyramid. Seems to have a problem knowing how it should translate %%. I'm beginning to suspect the bug is in Jinja2. So I've done some more investigation and it appears the problem is more with gettext than with jinja2 as illustrated with the repl >>>gettext.gettext("98%% off %s sale") % ('holiday') '98% off holiday sale' >>>gettext.gettext("98%% off sale") '98%% off sale' >>>gettext.gettext("98% off %s sale") % ('holiday') Traceback (most recent