Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Move flask_compress dependency to extras require #2291

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- [#2265](https://github.com/plotly/dash/pull/2265) Removed deprecated `before_first_request` as reported in [#2177](https://github.com/plotly/dash/issues/2177).
- [#2257](https://github.com/plotly/dash/pull/2257) Fix tuple types in the TypeScript component generator.

### Changed

- [#2291](https://github.com/plotly/dash/pull/2291) Move `flask-compress` dependency to new extras requires `dash[compress]`

## [2.6.2] - 2022-09-23

### Fixed
Expand Down
39 changes: 23 additions & 16 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from textwrap import dedent

import flask
from flask_compress import Compress

from pkg_resources import get_distribution, parse_version

Expand Down Expand Up @@ -69,9 +68,6 @@
_parse_query_string,
)


_flask_compress_version = parse_version(get_distribution("flask-compress").version)

# Add explicit mapping for map files
mimetypes.add_type("application/json", ".map", True)

Expand Down Expand Up @@ -261,6 +257,7 @@ class Dash:
:type serve_locally: boolean

:param compress: Use gzip to compress files and data served by Flask.
To use this option, you need to install dash[compress]
Default ``False``
:type compress: boolean

Expand Down Expand Up @@ -379,16 +376,6 @@ def __init__( # pylint: disable=too-many-statements
else:
raise ValueError("server must be a Flask app or a boolean")

if (
self.server is not None
and not hasattr(self.server.config, "COMPRESS_ALGORITHM")
and _flask_compress_version >= parse_version("1.6.0")
):
# flask-compress==1.6.0 changed default to ['br', 'gzip']
# and non-overridable default compression with Brotli is
# causing performance issues
self.server.config["COMPRESS_ALGORITHM"] = ["gzip"]

base_prefix, routes_prefix, requests_prefix = pathname_configs(
url_base_pathname, routes_pathname_prefix, requests_pathname_prefix
)
Expand Down Expand Up @@ -540,8 +527,28 @@ def init_app(self, app=None, **kwargs):
)

if config.compress:
# gzip
Compress(self.server)
try:
# pylint: disable=import-outside-toplevel
from flask_compress import Compress

# gzip
Compress(self.server)

_flask_compress_version = parse_version(
get_distribution("flask-compress").version
)

if not hasattr(
self.server.config, "COMPRESS_ALGORITHM"
) and _flask_compress_version >= parse_version("1.6.0"):
# flask-compress==1.6.0 changed default to ['br', 'gzip']
# and non-overridable default compression with Brotli is
# causing performance issues
self.server.config["COMPRESS_ALGORITHM"] = ["gzip"]
except ImportError as error:
raise ImportError(
"To use the compress option, you need to install dash[compress]"
) from error

@self.server.errorhandler(PreventUpdate)
def _handle_error(_):
Expand Down
1 change: 1 addition & 0 deletions requires-compress.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flask-compress
1 change: 0 additions & 1 deletion requires-install.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Flask>=1.0.4
flask-compress
plotly>=5.0.0
dash_html_components==2.0.0
dash_core_components==2.0.0
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def read_req_file(req_type):
"testing": read_req_file("testing"),
"celery": read_req_file("celery"),
"diskcache": read_req_file("diskcache"),
"compress": read_req_file("compress"),
},
entry_points={
"console_scripts": [
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,3 +482,8 @@ def test_debug_mode_enable_dev_tools(empty_environ, debug_env, debug, expected):
app = Dash()
app.enable_dev_tools(debug=debug)
assert app._dev_tools.ui == expected


def test_missing_flask_compress_raises():
with pytest.raises(ImportError):
Dash(compress=True)