Python 如何在瓶子服务器中返回html文件?

Python 如何在瓶子服务器中返回html文件?,python,bottle,Python,Bottle,所以我有一个瓶子网络框架运行,但我想有一个网页在它 我已经创建了html和css的网页,但我不知道如何使用它。 我让它只显示html,但css部分不起作用 我试过在谷歌上搜索,但似乎找不到这样的例子 @get('/test') def test(): return static_file('index.html' , root="views") 我的css文件与视图文件夹位于同一目录中。瓶子最适合于API,其中路由不返回HTML文件。但是,您可以使用static\u file功能提供静

所以我有一个瓶子网络框架运行,但我想有一个网页在它

我已经创建了html和css的网页,但我不知道如何使用它。 我让它只显示html,但css部分不起作用

我试过在谷歌上搜索,但似乎找不到这样的例子

@get('/test')
def test():
    return static_file('index.html' , root="views")

我的css文件与视图文件夹位于同一目录中。

瓶子最适合于API,其中路由不返回HTML文件。但是,您可以使用
static\u file
功能提供静态HTML文件

如果您正在寻找一个更全面的HTML模板框架,请尝试。

从瓶子导入静态文件
@路由(“/static/”)
def服务器_静态(文件名):
返回静态文件(文件名,根='/path/to/your/static/files')

这是瓶子文档提供的用于提供静态文件的代码

如果js和css文件有不同的文件夹(在html文件中使用),这是所有项目中的常见情况,那么我们需要分别显式地为js和css目录内容提供服务

有关更多详细信息,请参阅以下代码:

from bottle import route
from bottle import static_file

#Hosts html file which will be invoked from browser.
@route('/filesPath/<staticFile>')
def serve_static_file(staticFile):
    filePath = '/path/to/your/static/file/'
    return static_file(staticFile, filePath)

#host css files which will be invoked implicitly by your html files.
@route('/files_path/css/<cssFile>')
def serve_css_files(cssFile):
    filePath = '/path/to/your/css/file/'
    return static_file(cssFile, filePath)

# host js files which will be invoked implicitly by your html files.
@route('/files_path/js/<jsFile>')
def serve_js_files(jsFile):
    filePath = '/path/to/your/jss/file/'
    return static_file(jsFile, filePath)
从瓶子进口路线
从瓶子导入静态文件
#托管将从浏览器调用的html文件。
@路由(“/filepath/”)
def SERVER_静态文件(静态文件):
filePath='/path/to/your/static/file/'
返回静态文件(静态文件,文件路径)
#宿主css文件,这些文件将被html文件隐式调用。
@路由(“/files\u path/css/”)
def SERVER_css_文件(cssFile):
filePath='/path/to/your/css/file/'
返回静态_文件(cssFile,filePath)
#宿主js文件,该文件将被html文件隐式调用。
@路由(“/files\u path/js/”)
def SERVER_js_文件(jsFile):
filePath='/path/to/your/jss/file/'
返回静态_文件(jsFile,filePath)

你说的是瓶子框架吗?是的,我会把它添加到主要问题中,我知道怎么做,它会返回我的html,它工作正常,但我的css不工作。这是我最关心的。@Ramis您的.css文件也在同一目录中吗?发布用于调用
static_file()
的代码以及文件夹层次结构的描述将使调试更容易。添加到原始PostThank,使用static file,我发现我必须创建到css和图像文件的路由。在那之后,一切都正常了。是的,这是我开始选择瓶子的原因,但我现在需要一个带有api的网页。我真的没有时间更改web框架。
from bottle import route
from bottle import static_file

#Hosts html file which will be invoked from browser.
@route('/filesPath/<staticFile>')
def serve_static_file(staticFile):
    filePath = '/path/to/your/static/file/'
    return static_file(staticFile, filePath)

#host css files which will be invoked implicitly by your html files.
@route('/files_path/css/<cssFile>')
def serve_css_files(cssFile):
    filePath = '/path/to/your/css/file/'
    return static_file(cssFile, filePath)

# host js files which will be invoked implicitly by your html files.
@route('/files_path/js/<jsFile>')
def serve_js_files(jsFile):
    filePath = '/path/to/your/jss/file/'
    return static_file(jsFile, filePath)