python 3.5 sanic web framework template with jinga2

简介:
  1. 安装模板引擎Jinja2

MarkupSafe-0.23.tar.gz

Jinja2-2.8.tar.gz


2.配置模板引擎到blueprint

/home/webapp

     |-- main.py

     |-- my_blueprint.py

     templates

        |-- index.html


1).main.py


from sanic import Sanic

from my_blueprint import bp


app = Sanic(__name__)

app.blueprint(bp)


app.run(host='0.0.0.0', port=8000, debug=True)


2).my_blueprint.py


# more my_blueprint.py 

from sanic.response import json, text, html

from sanic import Blueprint

from jinja2 import Environment, PackageLoader

env = Environment(loader=PackageLoader('my_blueprint', 'templates'))


bp = Blueprint('my_blueprint')


@bp.route('/')

async def bp_root(request):

    template = env.get_template('index.html')

    content=template.render(title='Sanic',people='David')

    return html(content)



3).index.html

# more index.html 

<!doctype html>

<title>` title `</title>

<div class=page>

  <h1>hello, ` people `</h1>

</div>



3.运行测试


运行服务

/home/webapp/目录下

[root@localhost webapp]# 

[root@localhost webapp]# python main.py 


wKiom1hOUIqC4FskAAKowjhQOjU050.jpg-wh_50



浏览器访问网址 http://192.168.199.201:8000/



wKioL1hOTwTALBGEAABdymxWWGQ875.jpg-wh_50


本文转自 pgmia 51CTO博客,原文链接:http://blog.51cto.com/heyiyi/1881950


相关文章
|
3天前
|
存储 程序员 API
python web开发示例详解
python web开发示例详解
13 0
|
5天前
|
数据采集 Python
使用Python实现简单的Web爬虫
本文将介绍如何使用Python编写一个简单的Web爬虫,用于抓取网页上的信息。通过分析目标网页的结构,利用Python中的requests和Beautiful Soup库,我们可以轻松地提取所需的数据,并将其保存到本地或进行进一步的分析和处理。无论是爬取新闻、股票数据,还是抓取图片等,本文都将为您提供一个简单而有效的解决方案。
|
5天前
|
持续交付 Python
使用Python实现自动化Web内容巡检
使用Python实现自动化Web内容巡检
17 1
|
6天前
|
数据采集 存储 XML
如何利用Python构建高效的Web爬虫
本文将介绍如何使用Python语言以及相关的库和工具,构建一个高效的Web爬虫。通过深入讨论爬虫的基本原理、常用的爬虫框架以及优化技巧,读者将能够了解如何编写可靠、高效的爬虫程序,实现数据的快速获取和处理。
|
6天前
|
前端开发 JavaScript Python
使用Python读取本地行情csv文件,做出web网页画出K线图实现案例
【5月更文挑战第4天】使用Python绘制K线图的步骤:1) 安装pandas, matplotlib和Flask;2) 用pandas读取CSV文件并处理数据;3) 创建Flask应用,渲染包含K线图数据的HTML;4) 编写HTML,使用ECharts库绘制K线图。
25 0
|
12天前
|
缓存 前端开发 安全
Python web框架fastapi中间件的使用,CORS跨域详解
Python web框架fastapi中间件的使用,CORS跨域详解
|
12天前
|
API 数据库 Python
Python web框架fastapi数据库操作ORM(二)增删改查逻辑实现方法
Python web框架fastapi数据库操作ORM(二)增删改查逻辑实现方法
|
12天前
|
关系型数据库 MySQL API
Python web框架fastapi数据库操作ORM(一)
Python web框架fastapi数据库操作ORM(一)
|
12天前
|
Python
python web框架fastapi模板渲染--Jinja2使用技巧总结
python web框架fastapi模板渲染--Jinja2使用技巧总结
|
12天前
|
开发框架 网络协议 前端开发
Python高性能web框架--Fastapi快速入门
Python高性能web框架--Fastapi快速入门