瓶静态文件

IT *_*nja 24 python bottle python-2.7

我曾尝试阅读Bottle的文档,但是,我仍然不确定静态文件服务是如何工作的.我有一个index.tpl文件,并在其中附加了一个css文件,它的工作原理.但是,我正在读取Bottle不会自动提供css文件,如果页面正确加载则不能为true.

但是,在请求页面时,我遇到了速度问题.那是因为我没用过return static_file(params go here)吗?如果有人能够清理它们的工作方式,以及在加载页面时如何使用它们,那就太棒了.

服务器代码:

from Bottle import route,run,template,request,static_file



@route('/')
def home():
    return template('Templates/index',name=request.environ.get('REMOTE_ADDR'))

run(host='Work-PC',port=9999,debug=True)
Run Code Online (Sandbox Code Playgroud)

指数:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="content-type"
 content="text/html; charset=ISO-8859-1">
  <title>index</title>
  <link type="text/css"
 href="cssfiles/mainpagecss.css"
 rel="stylesheet">
</head>
<body>
<table
 style="width: 100%; text-align: left; margin-left: auto; margin-right: auto;"
 border="0" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td>
      <h1><span class="headertext">
      <center>Network
Website</center>
      </span></h1>
      </td>
    </tr>
  </tbody>
</table>
%if name!='none':
    <p align="right">signed in as: {{name}}</p>
%else:
    pass
%end
<br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2"
 cellspacing="2">
  <tbody>
    <tr>
      <td>
      <table style="text-align: left; width: 100%;" border="0"
 cellpadding="2" cellspacing="2">
        <tbody>
          <tr>
            <td style="width: 15%; vertical-align: top;">
            <table style="text-align: left; width: 100%;" border="1"
 cellpadding="2" cellspacing="2">
              <tbody>
                <tr>
                  <td>Home<br>
                  <span class="important">Teamspeak Download</span><br>
                  <span class="important">Teamspeak Information</span></td>
                </tr>
              </tbody>
            </table>
            </td>
            <td style="vertical-align: top;">
            <table style="text-align: left; width: 100%;" border="1"
 cellpadding="2" cellspacing="2">
              <tbody>
                <tr>
                  <td>
                  <h1><span style="font-weight: bold;">Network Website</span></h1>
To find all of the needed information relating to the network's social
capabilities, please refer to the links in the side bar.</td>
                </tr>
              </tbody>
            </table>
            </td>
          </tr>
        </tbody>
      </table>
      </td>
    </tr>
  </tbody>
</table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

San*_*tta 71

要使用bottle您提供静态文件,您需要使用提供的static_file功能并添加一些其他路由.以下路由指示静态文件请求,并确保仅访问具有正确文件扩展名的文件.

from bottle import get, static_file

# Static Routes
@get("/static/css/<filepath:re:.*\.css>")
def css(filepath):
    return static_file(filepath, root="static/css")

@get("/static/font/<filepath:re:.*\.(eot|otf|svg|ttf|woff|woff2?)>")
def font(filepath):
    return static_file(filepath, root="static/font")

@get("/static/img/<filepath:re:.*\.(jpg|png|gif|ico|svg)>")
def img(filepath):
    return static_file(filepath, root="static/img")

@get("/static/js/<filepath:re:.*\.js>")
def js(filepath):
    return static_file(filepath, root="static/js")
Run Code Online (Sandbox Code Playgroud)

现在在你的html中,你可以像这样引用一个文件:

<link type="text/css" href="/static/css/main.css" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

目录布局:

`--static
|  `--css
|  `--fonts
|  `--img
|  `--js
Run Code Online (Sandbox Code Playgroud)

  • 使用`<:re:.*/> <filename:re:.*\.js>`以便`some/path/some.js`和`some.js`有效 (4认同)

Ste*_*idy 11

这里只是提供一个答案,因为我的一些学生在作业中使用了这段代码,我对解决方案有点担心.

在Bottle中提供静态文件的标准方法是在文档中:

from bottle import static_file
@route('/static/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='/path/to/your/static/files')
Run Code Online (Sandbox Code Playgroud)

这样,静态文件夹下的所有文件都是从以/ static开头的URL提供的.在HTML中,您需要引用资源的完整URL路径,例如:

<link rel='stylesheet' type='text/css' href='/static/css/style.css'>

Sanketh的答案使得对于URL空间中任何位置的图像,css文件等的任何引用都是从静态文件夹内的给定文件夹提供的.所以/foo/bar/baz/picture.jpg和/picture.jpg都将由static/images/picture.jpg提供.这意味着您无需担心在HTML代码中获取正确的路径,并且您始终可以使用相对文件名(即只是src ="picture.jpg").

当您尝试部署应用程序时,会出现此方法的问题.在生产环境中,您希望静态资源由nginx等Web服务器提供,而不是由Bottle应用程序提供.为了实现这一点,它们应该从URL空间的单个部分提供,例如./静态的.如果您的代码中包含相对文件名,则无法轻松转换为此模型.

因此,我建议使用Bottle教程中的三行解决方案,而不是本页列出的更复杂的解决方案.它的代码更简单(因此不太可能出错),它允许您无需更改代码即可无缝移动到生产环境.


cob*_*bie 5

如文档中所述,您应该使用static函数提供静态文件,而 css 是一个静态文件。静态函数处理安全性和其他一些可以从源代码中找到的函数。静态函数的路径参数应该指向你存储css文件的目录