2016-10-24 09:21:06 +01:00
|
|
|
# Static Files
|
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
Static files and directories, such as an image file, are served by Sanic when
|
|
|
|
registered with the `app.static` method. The method takes an endpoint URL and a
|
|
|
|
filename. The file specified will then be accessible via the given endpoint.
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
```python
|
2017-01-20 03:18:52 +00:00
|
|
|
from sanic import Sanic
|
2017-09-06 12:17:52 +01:00
|
|
|
from sanic.blueprints import Blueprint
|
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
app = Sanic(__name__)
|
|
|
|
|
|
|
|
# Serves files from the static folder to the URL /static
|
2016-10-25 10:45:28 +01:00
|
|
|
app.static('/static', './static')
|
2017-09-06 12:17:52 +01:00
|
|
|
# use url_for to build the url, name defaults to 'static' and can be ignored
|
|
|
|
app.url_for('static', filename='file.txt') == '/static/file.txt'
|
|
|
|
app.url_for('static', name='static', filename='file.txt') == '/static/file.txt'
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
# Serves the file /home/ubuntu/test.png when the URL /the_best.png
|
|
|
|
# is requested
|
2017-09-06 12:17:52 +01:00
|
|
|
app.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
|
|
|
|
|
|
|
|
# you can use url_for to build the static file url
|
|
|
|
# you can ignore name and filename parameters if you don't define it
|
|
|
|
app.url_for('static', name='best_png') == '/the_best.png'
|
|
|
|
app.url_for('static', name='best_png', filename='any') == '/the_best.png'
|
|
|
|
|
|
|
|
# you need define the name for other static files
|
|
|
|
app.static('/another.png', '/home/ubuntu/another.png', name='another')
|
|
|
|
app.url_for('static', name='another') == '/another.png'
|
|
|
|
app.url_for('static', name='another', filename='any') == '/another.png'
|
|
|
|
|
|
|
|
# also, you can use static for blueprint
|
|
|
|
bp = Blueprint('bp', url_prefix='/bp')
|
|
|
|
bp.static('/static', './static')
|
|
|
|
|
|
|
|
# servers the file directly
|
|
|
|
bp.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
|
|
|
app.url_for('static', name='bp.static', filename='file.txt') == '/bp/static/file.txt'
|
|
|
|
app.url_for('static', name='bp.best_png') == '/bp/test_best.png'
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
app.run(host="0.0.0.0", port=8000)
|
|
|
|
```
|