标签:统一 运行 handle inpu cat turn star style python
tornado中,Python文件是和html文件貌似在同一目录下,而非如django、flask那样,Python文件和templates文件夹同目录。
****************************************分割线****************************************
随机6个红球+1个蓝球:
红蓝球.py:
from tornado import web,httpserver,ioloop
import random
def createBall():
result = []
redBallList=list(range(1,34))
blueBallList=list(range(1,17))
for x in range(6):
random.shuffle(redBallList)
redNum=redBallList[:6]
# blueNum=[x for x in blueBallList if x not in redNums] #红、蓝球不重号
result.append({‘redNum‘:redNum , ‘blueNum‘: random.choice(blueBallList)})
return result
def getLuckNum(button):
if button==‘随机1注‘:
return [createBall()[0]] #原本是{},套个[],使得return的都统一为[{},]
elif button==‘随机5注‘:
return createBall()[1:]
class IndexPageHandler(web.RequestHandler):
def get(self, *args, **kwargs):
# self.write(‘hello,world!‘) #传文字或网页源代码用write;传html文件也是用render
self.render(‘index之红蓝球.html‘,rows=‘‘)
def post(self, *args, **kwargs):
button=self.get_argument(‘submit‘) #属性name为submit的标签,其value属性的值
luckNum=getLuckNum(button)
self.render(‘index之红蓝球.html‘,rows=luckNum)
app=web.Application([
(r‘/‘,IndexPageHandler),
])
#与flask、django不同,运行窗口不自动显示127.0.0.1:8080,要在浏览器地址栏手输
#并且改了后台代码后不会自动刷新,要点击红方块stop,并重新运行代码
if __name__ == ‘__main__‘:
httpServer=httpserver.HTTPServer(app)
httpServer.listen(8080)
ioloop.IOLoop.current().start()
***************分割线***************
index之红蓝球.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>红蓝球</title>
</head>
<body>
<h1 style="color: red">红蓝球</h1>
<form action="/" method="post">
<input type="submit" value="随机1注" name="submit">
<input type="submit" value="随机5注" name="submit">
</form>
<br>
<div>
{% if rows %}
{% for row in rows %}
{% for redNum in row[‘redNum‘] %}
<span style="color: red">{{redNum}}</span>
{% end %}
<span style="color: blue">{{row[‘blueNum‘]}}</span><br>
{% end %}
{% end %}
</div>
</body>
</html>
****************************************分割线****************************************
51job:
tornado
标签:统一 运行 handle inpu cat turn star style python
原文地址:http://www.cnblogs.com/scrooge/p/7792850.html