本文整理汇总了Python中whitenoise.WhiteNoise方法的典型用法代码示例。如果您正苦于以下问题:Python whitenoise.WhiteNoise方法的具体用法?Python whitenoise.WhiteNoise怎么用?Python whitenoise.WhiteNoise使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类whitenoise
的用法示例。
在下文中一共展示了whitenoise.WhiteNoise方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: static
# 需要导入模块: import whitenoise [as 别名]
# 或者: from whitenoise import WhiteNoise [as 别名]
def static(root: str, **kwargs) -> WSGIApp:
"""Return a WSGI app that serves static files under the given directory.
Powered by [WhiteNoise](http://whitenoise.evans.io).
[config-attrs]: http://whitenoise.evans.io/en/stable/base.html#configuration-attributes
# Parameters
root (str):
the path to a directory from where static files should be served.
If the directory does not exist, no files will be served.
**kwargs (any):
keyword arguments passed to the WhiteNoise constructor. See also [Configuration attributes (WhiteNoise docs)][config-attrs].
# Returns
app (callable): a WhiteNoise WSGI app.
"""
if exists(root):
kwargs["root"] = root
return WhiteNoise(empty_wsgi_app(), **kwargs)
示例2: init_app
# 需要导入模块: import whitenoise [as 别名]
# 或者: from whitenoise import WhiteNoise [as 别名]
def init_app(self, app):
"""Initializes the Flask application with Common."""
if not hasattr(app, 'extensions'):
app.extensions = {}
if 'common' in app.extensions:
raise RuntimeError("Flask-Common extension already initialized")
app.extensions['common'] = self
self.app = app
if 'COMMON_FILESERVER_DISABLED' not in app.config:
with app.test_request_context():
# Configure WhiteNoise.
app.wsgi_app = WhiteNoise(app.wsgi_app, root=url_for('static', filename='')[1:])
self.cache = Cache(app, config={'CACHE_TYPE': app.config.get("COMMON_CACHE_TYPE", 'simple')})
@app.before_request
def before_request_callback():
request.start_time = maya.now()
@app.after_request
def after_request_callback(response):
if 'COMMON_POWERED_BY_DISABLED' not in current_app.config:
response.headers['X-Powered-By'] = 'Flask'
if 'COMMON_PROCESSED_TIME_DISABLED' not in current_app.config:
response.headers['X-Processed-Time'] = maya.now().epoch - request.start_time.epoch
return response
@app.route('/favicon.ico')
def favicon():
return redirect(url_for('static', filename='favicon.ico'), code=301)
示例3: create_app
# 需要导入模块: import whitenoise [as 别名]
# 或者: from whitenoise import WhiteNoise [as 别名]
def create_app(*, testing=False):
app = Flask(__name__)
app.config["SECRET_KEY"] = SECRET_KEY
if testing:
app.config["TESTING"] = True
database_uri = "sqlite:///:memory:"
app.config["RQ_ASYNC"] = False
else:
database_uri = DATABASE_URI
app.config["SQLALCHEMY_DATABASE_URI"] = database_uri
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
migrate.init_app(app, db)
app.config["RQ_REDIS_URL"] = REDIS_URI
app.config["RQ_QUEUES"] = ["default"]
rq.init_app(app)
login_manager.init_app(app)
talisman.init_app(app)
app.register_error_handler(NotAuthorized, handle_http_error)
app.register_error_handler(OAuthError, handle_oauth_error)
app.register_error_handler(ValidationError, handle_validation_error)
app.register_blueprint(events_bp, cli_group=None)
app.register_blueprint(healthcheck_bp)
app.register_blueprint(github_bp, url_prefix="/github", cli_group=None)
app.register_blueprint(slack_bp, url_prefix="/slack")
app.register_blueprint(twitter_bp, cli_group=None)
app.register_blueprint(web_bp)
app.wsgi_app = WhiteNoise(
app.wsgi_app, root="busy_beaver/static/", prefix="assets/"
)
@app.before_request
def add_internal_dictionary():
"""Keep request specific data in `_internal` dictionary"""
if not getattr(request, "_internal", None):
request._internal = {}
return app