简体   繁体   English

Sanic如何定位到静态文件

[英]Sanic how to locate to static file

I want to locate to static file, because there are many relative path in the html, like:我想定位到静态文件,因为 html 中有很多相对路径,例如:

<a href="1.html"> page1 </a>
<a href="2.html"> page2 </a>
....

I can use app.send_static_file() in flask to made it.我可以在烧瓶中使用app.send_static_file()来制作它。

from flask import Flask
app = Flask(__name__, static_url_path='')
@app.route('/')
def index():
    return app.send_static_file('index.html')
if __name__ == '__main__':
    app.run(host="0.0.0.0",debug=True,port=8888)

But for Sanic I didn't find the relevant method.但是对于Sanic我没有找到相关的方法。

from sanic import Sanic
app = Sanic(__name__)
app.static('/static', './static')
@app.route('/')
async def index(request):
    #return "static/index.html" file with static state.
if __name__=='__main__':
    app.run(host='0.0.0.0',port=8888,debug=True, auto_reload=True)

Is there any way to achieve this?有什么办法可以做到这一点? Or sanic-jinja2, sanic-mako etc. method is fine, too.或者sanic-jinja2、sanic-mako等方法也可以。

It is a little unclear to me exactly what you are looking to do, so I will provide a couple of examples that might be what you are looking for.我不太清楚你到底想要做什么,所以我将提供几个可能是你正在寻找的例子。 Please let me know if this does or does not solve the problem and I can amend the answer.请让我知道这是否解决了问题,我可以修改答案。


Static files静态文件

If you have a single static file (this also works for a directory of static files) that you would like to serve, then you use app.static如果您有一个想要提供的静态文件(这也适用于静态文件目录),那么您可以使用app.static

app.static("/static", "/path/to/directory")
# So, now a file like `/path/to/directory/foo.jpg`
# is available at http://example.com/static/foo.jpg

This would also apply to deeply nested files in /path/to/directory .这也适用于/path/to/directory中的深层嵌套文件。

You can also choose to use this pattern on a single file, often helpful with index.html for example:您还可以选择在单个文件上使用此模式,这通常对index.html很有帮助,例如:

app.static("/", "/path/to/index.html")

Retrieving (or looking up) a URL for a static file检索(或查找)静态文件的 URL

If by "locating" a file you mean you want to get access to its URL, then you would use app.url_for如果通过“定位”文件意味着您想要访问其 URL,那么您将使用app.url_for

app.static(
    "/user/uploads",
    "/path/to/uploads",
    name="uploads",
)
app.url_for(
    "static",  # Note, for any file registered with app.static, this value is "static"
    name="uploads",
    filename="image.png",
)

Serving files from a route handler从路由处理程序提供文件

If on the other hand you have a regular route handler and would like to respond with a file (meaning you are doing something more than just serving a static file), then you can use sanic.response.file .另一方面,如果您有一个常规的路由处理程序并希望使用文件进行响应(这意味着您所做的不仅仅是提供静态文件),那么您可以使用sanic.response.file

Let's imagine a scenario where you need to lookup a user and fetch their profile image:让我们想象一个场景,您需要查找用户并获取他们的个人资料图片:

@app.get("/current-user/avatar")
async def serve_user_avatar(request: Request):
    user = await fetch_user_from_request(request)
    return await file(user.avatar)

Templating模板

Since you brought up jinja and mako, Sanic Extensions is an officially supported plugin that adds templating:既然你提到了 jinja 和 mako, Sanic Extensions是一个官方支持的插件,它添加了模板:

pip install "sanic[ext]"
@app.get("/")
@app.ext.template("foo.html")
async def handler(request: Request):
    return {"seq": ["one", "two"]}

See this PR for alternative methods of serving templates with a render function有关使用render功能提供模板的替代方法,请参阅此 PR

Looking back to your example...回头看看你的例子...

Your example shows this:您的示例显示了这一点:

app.static('/static', './static')

@app.route('/')
async def index(request):
    #return "static/index.html" file with static state.

To me, it looks like you have a file in ./static/index.html that you want to serve just that file.对我来说,看起来您在./static/index.html中有一个文件,您只想提供该文件。 In this case, the @app.route definition is unnecessary, since it will be server because of your app.static definition.在这种情况下, @app.route定义是不必要的,因为由于您的app.static定义,它将是服务器。 If you have a folder structure like this:如果您有这样的文件夹结构:

./root
├── static
│   └── index.html
└── server.py

Then all you need is:那么你只需要:

app.static("/static", "./static")

And now you will have: http://example.com/static/index.html现在您将拥有: http ://example.com/static/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM