簡體   English   中英

瓶靜態文件

[英]Bottle Static files

我曾嘗試閱讀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)

指數:

<!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>

要使用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")

現在在你的html中,你可以像這樣引用一個文件:

<link type="text/css" href="/static/css/main.css" rel="stylesheet">

目錄布局:

`--static
|  `--css
|  `--fonts
|  `--img
|  `--js

這里只是提供一個答案,因為我的一些學生在作業中使用了這段代碼,我對解決方案有點擔心。

在Bottle中提供靜態文件的標准方法是在文檔中

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

這樣,靜態文件夾下的所有文件都是從以/ 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教程中的三行解決方案,而不是本頁列出的更復雜的解決方案。 它的代碼更簡單(因此不太可能出錯)並且它允許您無需更改代碼即可無縫移動到生產環境。

如文檔中所示,您應該使用靜態函數提供靜態文件,而css是一個靜態文件。 靜態函數處理安全性以及您可以從源中找到的其他一些功能。 靜態函數的path參數應指向存儲css文件的目錄

我不想像在Sanketh的回答中那樣使用正則表達式匹配服務文件,而是不修改模板並明確提供靜態文件的路徑,如:

<script src="{{ get_url('static', filename='js/bootstrap.min.js') }}"></script>

你可以簡單地通過將靜態路由裝飾器中的<filename>替換為類型:path - 就像這樣:

@app.route('/static/<filename:path>', name='static')
def serve_static(filename):
    return static_file(filename, root=config.STATIC_PATH)

:path以非貪婪的方式匹配整個文件路徑,因此您不必擔心在切換到生產時更改模板 - 只需將所有內容保存在相同的相對文件夾結構中。

我過去曾使用Sanketh的模板,但隨着時間的推移將其濃縮為擴展不可知功能。 您只需在ext_map字典中添加擴展名文件夾映射。 如果未明確映射擴展,則默認為static / folder。

import os.path    

# Static Routes
@get('/<filename>')
def serve_static_file(filename):
    ext = os.path.splitext(filename)[1][1:]
    ext_map = {'image':['png','gif','jpg','ico'],'js':['js']}
    sub_folder = next((k for k, v in ext_map.items() if ext in v),'')
    return static_file(filename, root='static/'+sub_folder)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM