Skip to content

Commit

Permalink
v.stream.profiler: fix lazy loading of matplotlib (#1254)
Browse files Browse the repository at this point in the history
* v.stream.profiler: fix lazy loading of matplotlib

Fixes error `ImportError: Cannot load backend 'WXAgg' which requires the 'wx' interactive framework, as 'headless' is currently running`.

Inspired by `vector/v.scatterplot/v.scatterplot.py`.

* v.convert.all.html: fix HTML typo
  • Loading branch information
neteler authored Dec 3, 2024
1 parent ad36f57 commit bf2796c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/vector/v.convert.all/v.convert.all.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h2>NOTES</h2>
in the old GRASS program.
<p>
As this GRASS version uses SQL for attribute management, there are
some <"a href="sql.html">SQL restrictings concerning the file names</a>.
some <a href="sql.html">SQL restrictings concerning the file names</a>.
This script changes dots (e.g. "foo.bar") in old vector map names into
underline(s) (e.g. "foo_bar").

Expand Down
4 changes: 3 additions & 1 deletion src/vector/v.scatterplot/v.scatterplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,9 @@ def lazy_import_matplotlib():
from matplotlib import cm
from matplotlib.colors import Normalize
except ModuleNotFoundError:
gs.fatal(_("Matplotlib is not installed. Please, install it."))
gs.fatal(
_("Matplotlib (python-matplotlib) is not installed. Please, install it.")
)


def get_valid_color(color):
Expand Down
28 changes: 17 additions & 11 deletions src/vector/v.stream.profiler/v.stream.profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,24 +153,30 @@ def moving_average(x, y, window):
###############


def lazy_import_matplotlib():
"""Lazy import matplotlib modules"""
global matplotlib
global plt
try:
import matplotlib

matplotlib.use("WXAgg")
from matplotlib import pyplot as plt
except ModuleNotFoundError:
gs.fatal(
_("Matplotlib (python-matplotlib) is not installed. Please, install it.")
)


def main():
"""
Links each river segment to the next downstream segment in a tributary
network by referencing its category (cat) number in a new column. "0"
means that the river exits the map.
"""
try:
import matplotlib

matplotlib.use("WXAgg")
from matplotlib import pyplot as plt
except ImportError as e:
raise ImportError(
_(
'v.stream.profiler needs the "matplotlib" '
"(python-matplotlib) package to be installed. {0}"
).format(e)
)
# lazy import modules
lazy_import_matplotlib()

options, flags = gscript.parser()

Expand Down

0 comments on commit bf2796c

Please sign in to comment.