python web py入门-2-显示一个demo网页

来源:互联网 发布:linux grub 配置 编辑:程序博客网 时间:2024/05/23 14:07

       本篇来一个动手显示一个网页,这个网页不只是打印hello world。这个网页包含一些html和css和javascript知识。所以,在学习web.py之前,我们还是需要有一些html语法和javascript的语法知识,如果一点也不会,请看我博客相关系列文章。

1.动手写一个hello.html,内容如下

<html><head><title>hello</title><script>function show_text(id, text){document.getElementById(id).innerHTML=text;}function show_color(id, color){document.getElementById(id).style.color=color;}</script><style>div p { color : #f00;}.py {font-size : 40px;}#11 {width:200px; font-size:40px;}</style></head><body><h1>hello</h1><div>World</div><p class="py">python</p><lable id="11">test</label><div><a href="javascript:void(0);" onclick='javascript:show_text("11","My First JavaScrit");'>My First JavaScrit</a><a href="javascript:void(0);" onclick='javascript:show_text("11","Hello Python");'> Hello Python </a><a href="javascript:void(0);" onclick='javascript:show_color("11","#f00");'>red</a><a href="javascript:void(0);" onclick='javascript:show_color("11","#0f0");'>green</a></div></body></html>

2.修改hello.py内容如下

import web        urls = (    '/(.*)', 'hello')app = web.application(urls, globals())class hello:            def GET(self, name):               return open(r'hello.html', 'r').read()if __name__ == "__main__":    app.run()
hello.html和hello.py放在同一个路径下。

3.cmd运行hello.py文件,浏览器查看效果


其实很简单,主要是在hello这个class里返回的是一个我们提前写好的html文件。


原创粉丝点击