.Tang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

import tornado.ioloop
import tornado.web
import tornado.httpserver  # 非阻塞
import tornado.options   # 提供了多种选择 python xxx.py --port=xxxx
from tornado.options import define,options


import util.ui_methods
import util.ui_modules

define('port',default=8000,help='run port',type=int)  # windows通过Ctrl+鼠标左键
define('version',default=0.1,help='version',type=str)


class ExtHandler(tornado.web.RequestHandler):
    def haha(self):
        return 'succeed to use haha'

    def get(self):
        self.render("five-extend.html",
                    haha=self.haha,            # 引用haha函数
                    new=New,
                    )


class New:
    def area(self,a,b):
        return a*b


application = tornado.web.Application(
    handlers=[                      # 列表按顺序匹配
        (r"/ext", ExtHandler),
        ],
    template_path='templates',  # 表明页面html的路径
    static_path='static',
    ui_methods=util.ui_methods,               #  导入ui_methods函数
    ui_modules=util.ui_modules,               # 等同下 dict  导入类 ui_modules
    # ui_modules={'UiModule':util.ui_modules.UiModule},
    debug=True,           # 上传代码后服务器自动重启

)

if __name__ == '__main__':
    tornado.options.parse_command_line()   # 通过sys.arg获取命令行输入参数(python xxx.py --port=xxx)
    print(options.port)
    print(options.version)
    http_server = tornado.httpserver.HTTPServer(application)  # 非阻塞
    application.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()   # 启动io循环

extend,及函数,类的导入

{% extends five-moudle.html %}

{%block title1%}Extend{% end %}

{%block body%}
    this is tornado extend

    {% include five-include.html %}

    {% include five-include.html %} <br>

    {{ haha() }}  <br>                        调用haha函数
{# 类的地址 #}
    {{ new }}   <br>
{# 类实例 #}
    {{ new() }} <br>

    {{ new().area(2,3) }} <br>

    {% import time %}                       导入time函数
    {{time.time()}}  <br>                               使用time.time

    {% from util.mod_file import add,upper %}    从util.mod_file模块导入函数add,upper
    {{add(22,11)}} <br>

    {% module UiModule() %}  <br>           调用UiModule这个类
    {{ methods1() }} <br>                调用UiModule这个类的methods1方法

    {% module Advertisement() %}  <br>

    {% apply upper %}                  了解apply ,linkify
        hello world
    {% end %} <br>

    {% raw linkify('百度链接:https://www.baidu.com') %}

{% end %}

# 知识点

# 模板
# 继承模板extend
#{% extends 父模板.html %} 子模版继承父模板所有的内容
# block块子模版重写父模板内容 name相当于变量
# 1.{% block name %} 父模板中的内容 {% end %}}
# 2.{% block name %} 在子模版修改新的内容 {% end %}}
# 单继承就好,别多继承。html不会报错所以可以继承多个页面

# 在子模版中新增内容 include (多个html相同的不完整html内容,把内容写在一个模板通过include导入)
# 在继承(extend)的模块里新增内容
# five-include.html为新增的内容
# 写在extend的子模版需要新增内容的位置{% include five-include.html %}
# 一般include的模块中不使用block块和extend

# 在模板中导入函数
# 先在render方法加上 haha = self.haha, 类里面的一个函数调用类的另一个函数 self.haha
# 再通过 子模版 {{ haha() }} 调用

# 在子模板中导入类
# 情况一 通过tornado渲染
# 导入父模板中New这个类
# 在render方法中加入 new = New,
# 在extend子模版中写入
# {{new}} 类本身
# {{new()}} 类的实例
# {{new().area(2, 3)}} 类的area方法
# 情况二 import 和 from import
# 子模版导入另一个模板里面的类 util为文件名
# 1. {% from util.mod_file import add %}
# 2. {{add(2+3)}}
# 情况三 更好点
# 在父模板中导入导入其他模板的函数和类
# 1.新建函数文件ui_methods,py和类文件ui_modules.py
# 分别写入 函数 和 类
#函数 def methods1(self): # 加上self
# return 'this is ui_methods1'

#类 from tornado.web import UIModule
# class UiModule(UIModule): # 类需要继承UIModule
# def render(self, *args, **kwargs): # 并重写render方法
# return '我是 ui_modules'
# 2.在项目中导入
# import util.ui_methods
# import util.ui_modules
# 3.配置application参数
# ui_methods = util.ui_methods,
# ui_modules = util.ui_modules, 等同下 dict的形式
# 类名 路径
# ui_modules={'UiModule':util.ui_modules.UiModule},
# 4.在子模板中调用
# {% module UiModule() %} 类
# {{ methods1() }} 函数
# 了解
# {% apply upper %} hello world {% end %} def upper(a): return a.upper() 将所有的字符串都在upper执行一边
  # {% raw linkify('百度链接:https://www.baidu.com') %} 将内容变成a链接
 

 

posted on 2018-03-02 16:42  .Tang  阅读(571)  评论(0编辑  收藏  举报