本文整理匯總了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