zoukankan      html  css  js  c++  java
  • 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文件的文件夹路径了.

  • 相关阅读:
    DAO模式多表联查
    使用ADO.NET访问数据库
    连接查询和分组查询
    模糊查询和聚合函数
    poj 1220 NUMBER BASE CONVERSION
    poj 1964 City Game
    Odd number problem
    POJ 2983 M × N Puzzle
    L O V E
    【Mybatis】【3】处理大于号小于号及其他特殊字符
  • 原文地址:https://www.cnblogs.com/jaw-crusher/p/3455819.html
Copyright © 2011-2022 走看看