send_file
is function in the Flask flask.helpers
module.
send_file
transfers the contents of a file to the client using the most
efficient method available and configured in the Flask settings. It
attempts to guess the correct mimetype to use but it can also be
explicitly configured.
Note that send_file
is usually imported directly from flask
instead of
from flask.helpers
, even though it is defined within the helpers
module.
It's the same function that is imported, but it's less characters to type
when you leave off the .helpers
part.
flash,
get_root_path,
make_response,
safe_join,
and url_for
are several other callables with code examples from the same flask.helpers
package.
CTFd (homepage) is a capture the flag (CTF) hacking web app built with Flask. The application can be used as-is to run CTF events, or modified for custom rules for related scenarios. CTFd is open sourced under the Apache License 2.0.
# views.py
import os
from flask import Blueprint, abort
from flask import current_app as app
from flask import redirect, render_template, request, send_file, session, url_for
from flask.helpers import safe_join
from jinja2.exceptions import TemplateNotFound
from sqlalchemy.exc import IntegrityError
from CTFd.cache import cache
from CTFd.constants.config import (
AccountVisibilityTypes,
ChallengeVisibilityTypes,
ConfigTypes,
RegistrationVisibilityTypes,
ScoreVisibilityTypes,
)
from CTFd.constants.themes import DEFAULT_THEME
from CTFd.models import (
Admins,
Files,
Notifications,
Pages,
Teams,
Users,
UserTokens,
db,
)
from CTFd.utils import config, get_config, set_config
## ... source file abbreviated to get to send_file examples ...
if team.banned:
abort(403)
else:
pass
if file_id != f.id:
abort(403)
except (BadTimeSignature, SignatureExpired, BadSignature):
abort(403)
uploader = get_uploader()
try:
return uploader.download(f.location)
except IOError:
abort(404)
@views.route("/themes/<theme>/static/<path:path>")
def themes(theme, path):
for cand_path in (
safe_join(app.root_path, "themes", cand_theme, "static", path)
for cand_theme in (theme, *config.ctf_theme_candidates())
):
if os.path.isfile(cand_path):
return send_file(cand_path)
abort(404)
## ... source file continues with no further send_file examples...
Flask-VueJs-Template (demo site) is a minimal Flask boilerplate starter project that combines Flask, Vue.js, and Flask-RESTPlus. The project provides some sensible defaults that are easy to continue building on, and the source code is open source under the MIT license.
Flask-VueJs-Template / app / init.py
# __init__.py
import os
from flask import Flask, current_app, send_file
from .api import api_bp
from .client import client_bp
app = Flask(__name__, static_folder='../dist/static')
app.register_blueprint(api_bp)
from .config import Config
app.logger.info('>>> {}'.format(Config.FLASK_ENV))
@app.route('/')
def index_client():
dist_dir = current_app.config['DIST_DIR']
entry = os.path.join(dist_dir, 'index.html')
return send_file(entry)
## ... source file continues with no further send_file examples...