Skip to content

Fetch flask_compress version from __version__ module #1797

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 10 additions & 3 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import flask
from flask_compress import Compress
from werkzeug.debug.tbtools import get_current_traceback
from pkg_resources import get_distribution, parse_version
from pkg_resources import get_distribution, parse_version, DistributionNotFound
from dash import dcc
from dash import html
from dash import dash_table
Expand Down Expand Up @@ -62,9 +62,16 @@
grouping_len,
)

"""
Handle "pkg_resources.DistributionNotFound: The 'flask-compress' distribution was not found and is required by the application" while building app with cx_freeze.
Since __version__ is included only in and above cx_freeze 1.10.0, adding the try/ except condition.
"""

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

try:
_flask_compress_version = parse_version(get_distribution("flask-compress").version)
except DistributionNotFound:
from flask_compress import __version__ as _flask_compress_version
_flask_compress_version = parse_version(_flask_compress_version)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, nice find! Unfortunately flask_compress.__version__ doesn't seem to exist until v1.10, which is probably why get_distribution was used in the first place.

I'd suggest a further fallback for the AttributeError: module 'flask_compress' has no attribute '__version__' you'll get before v1.10.0 that assumes it's maybe parse_version("1.9.0") (all that matters is >=1.6.0)

Another thing to note though: the default for compress was changed to False in Dash 2.0 (on the theory that for local dev this compression just slows things down, and for production deployments this is normally handled at a lower level like gunicorn). So this whole issue is irrelevant for most people as long as it doesn't error.

So if you're feeling adventurous, we could probably take this this whole block, plus the original from flask_compress import Compress AND the block where we use this to modify the server config:

dash/dash/dash.py

Lines 308 to 316 in b32ac95

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"]

and move it into the block where we actually turn compression on, so the only people who will hit any of this code will be those who explicitly enable compression, everyone else won't even import the module.

dash/dash/dash.py

Lines 453 to 455 in b32ac95

if config.compress:
# gzip
Compress(self.server)

If we do this we'll just want to manually check that you actually do get gzip compression with current flask_compress

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

Expand Down