cherrypy 入门笔记(1) hello world

cherrypy 是一个小型web框架,可以用来做一点小型玩具程序。最近闲的无聊,拿来学习一下.

hello world 应用

hello.py:

import cherrypy

import os.path

current_dir = os.path.dirname(os.path.abspath(__file__))

class Hello(object):
    content = """<html><head><title>hello</title>
    <link href="/static/hello-style.css" rel="stylesheet" type="text/css"/></head>
    <body>
    <h1 id="first-step">hello world</h1>
    <script src="/static/hello.js"></script>
    </body>
    </html>"""
    @cherrypy.expose
    def index(self):     # define the default page
        return Hello.content

    @cherrypy.expose
    def hello(self):     # define the hello page
        return "Hello"
    

if __name__ == '__main__':
    cherrypy.quickstart(Hello(), config={
        '/static':
        {'tools.staticdir.on': True,
         'tools.staticdir.dir': os.path.join(current_dir, "static")}
    })

css和js文件放在和python文件相同目录下的static文件夹中

css文件  hello-style.css:

h1#first-step {
    color: green;
    border: 1px dotted #d5d5d5;
    font-size: 30px;
     text-align:center;  
}

h1#first-step:hover {
    color: orange;
    font-size: 30px;
    border: 1px solid #e5e5e5;
    -webkit-transition: all 0.6s;
    -moz-transition: all 0.6s;
    -ms-transition: all 0.6s;
    -o-transition: all 0.6s;
    transition: all 0.6s;
}

为了好玩再加个js弹窗效果 hello.js :

alert("hello world!");

效果如下:

其中 __file__的用法 可以参考这里 http://andylin02.iteye.com/blog/933237

官方文档的说法是__file__是模块加载的路径。不过使用绝对路径,也就是文件目录下的static文件夹了。

但是使用IDLE下__file__没有定义,因为没有在任何文件中执行。

可以使用以下代码测试是否得到文件的绝对路径, 当然是要在命令行中执行的,或者在Windows下双击运行:

import os.path
current_dir = os.path.dirname(os.path.abspath(__file__))
print current_dir
raw_input()

就能显示出来当前执行python文件的文件夹路径了.

 

 

转载于:https://www.cnblogs.com/jaw-crusher/p/3455819.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值