From: Vladimír Vondruš Date: Mon, 3 Jan 2022 18:21:22 +0000 (+0100) Subject: m.plots: update to work with matplotlib 3.5 as well. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=7d5a572ebeeb55965d2a6a42ab84d72075f1504d;p=blog.git m.plots: update to work with matplotlib 3.5 as well. The major change in 3.5 is that the attributes have a different order and styles have more whitespace and less semicolons. To avoid explosion of find/replace patterns, the search strings were converted to regexps -- which on the other hand allowed me to drop some duplicates, and discard the font name replacement altogether. On the CI I'm now using the latest version on Python 3.8+, 3.7 stays on 3.4 to avoid regressions on that versions, and 3.6 on 3.3 which is also a bit different. --- diff --git a/documentation/test_python/page_plugins/plots-34.html b/documentation/test_python/page_plugins/plots-34.html new file mode 100644 index 00000000..a0b54cf8 --- /dev/null +++ b/documentation/test_python/page_plugins/plots-34.html @@ -0,0 +1,163 @@ + + + + + And now something totally different | My Python Project + + + + + +
+
+
+
+
+

+ And now something totally different +

+
+ + + + + + + + + + 15.0 meters, i guess? + + + 30.0 meters, i guess? + + + + + + + + + + + + + + 0 + + + + + + + + + + 5 + + + + + + + + + + 10 + + + + + + + + + + 15 + + + + + + + + + + 20 + + + + + + + + + + 25 + + + + + + + + + + 30 + + + + meters, i guess? + + + + + + + + + + + + + + First + + + + + + + + + + Second + + + + + A plot with a single color + + + + + + + + + +
+
+
+
+
+ + diff --git a/documentation/test_python/page_plugins/plots.html b/documentation/test_python/page_plugins/plots.html index a0b54cf8..9d859681 100644 --- a/documentation/test_python/page_plugins/plots.html +++ b/documentation/test_python/page_plugins/plots.html @@ -27,7 +27,7 @@ div.m-plot svg { font-family: DejaVu Sans; }
- + @@ -35,122 +35,122 @@ div.m-plot svg { font-family: DejaVu Sans; } 15.0 meters, i guess? - + 30.0 meters, i guess? - + - + - + - 0 + 0 - + - 5 + 5 - + - 10 + 10 - + - 15 + 15 - + - 20 + 20 - + - 25 + 25 - + - 30 + 30 - meters, i guess? + meters, i guess? - + - + - First + First - + - Second + Second - A plot with a single color + A plot with a single color - + diff --git a/documentation/test_python/test_page.py b/documentation/test_python/test_page.py index 276dc199..0fa56449 100644 --- a/documentation/test_python/test_page.py +++ b/documentation/test_python/test_page.py @@ -93,8 +93,10 @@ class Plugins(BaseTestCase): # I assume this will be a MASSIVE ANNOYANCE at some point as well so # keeping it separate. (Yes, thank you past mosra. Very helpful.) - if LooseVersion(matplotlib.__version__) >= LooseVersion('3.4'): + if LooseVersion(matplotlib.__version__) >= LooseVersion('3.5'): self.assertEqual(*self.actual_expected_contents('plots.html')) + elif LooseVersion(matplotlib.__version__) >= LooseVersion('3.4'): + self.assertEqual(*self.actual_expected_contents('plots.html', 'plots-34.html')) else: self.assertEqual(*self.actual_expected_contents('plots.html', 'plots-32.html')) self.assertTrue(os.path.exists(os.path.join(self.path, 'output/tiny.png'))) diff --git a/package/ci/circleci.yml b/package/ci/circleci.yml index 91260a0f..26d29942 100644 --- a/package/ci/circleci.yml +++ b/package/ci/circleci.yml @@ -57,8 +57,6 @@ commands: steps: - run: name: Install Python dependencies - # Matplotlib 3.5.1 has different order of attributes than 3.4, so can't - # just use the latest (and 3.4 is not on the Python 3.6 image) # Pygments 2.11 (and apparently 2.10 as well) treats certain whitespace # differently, I have to update the expected output first. # Docutils 0.18 drops some attribute that htmlsanity relies on, I need @@ -206,6 +204,8 @@ jobs: - install-base: extra: graphviz cmake ninja-build wget - install-python-deps: + # Matplotlib 3.5 has significantly different output, be sure to have + # at least one job testing 3.4 so we don't regress matplotlib-version: ==3.4.3 - checkout - test-theme @@ -220,8 +220,7 @@ jobs: steps: - install-base: extra: graphviz cmake ninja-build wget - - install-python-deps: - matplotlib-version: ==3.4.3 + - install-python-deps - checkout - test-theme - test-plugins @@ -237,8 +236,7 @@ jobs: steps: - install-base: extra: graphviz cmake ninja-build wget - - install-python-deps: - matplotlib-version: ==3.4.3 + - install-python-deps - checkout - test-theme - test-plugins @@ -254,8 +252,7 @@ jobs: steps: - install-base: extra: graphviz cmake ninja-build wget - - install-python-deps: - matplotlib-version: ==3.4.3 + - install-python-deps - checkout - test-theme - test-plugins diff --git a/plugins/m/plots.py b/plugins/m/plots.py index 179b73f4..15733d06 100644 --- a/plugins/m/plots.py +++ b/plugins/m/plots.py @@ -79,22 +79,25 @@ style_mapping = { # field (which we're not interested in) and slightly different # formatting of the global style after (which we unify to the compact version). # Matplotlib 3.4 drops the "Created with" comment, as that's in the -# already anyway. +# already anyway. Matplotlib 3.5 changes order of the attributes (which +# we ignore anyway, so those aren't matched anymore), order of stroke CSS +# properties and whitespace/semicolons (which we preserve for consistency with +# the rest of the output). _patch_src = re.compile(r"""<\?xml version="1\.0" encoding="utf-8" standalone="no"\?> ( )? -viewBox="0 0 \d+ \d+(\.\d+)?") width="\d+(\.\d+)?pt" xmlns="http://www\.w3\.org/2000/svg" xmlns:xlink="http://www\.w3\.org/1999/xlink">( +]+(?PviewBox="0 0 \d+ \d+(\.\d+)?")[^>]+>( .+)? """, re.DOTALL) _patch_dst = r"""> - + """ @@ -104,47 +107,47 @@ _path_patch_dst = '\\g \\g' _path_patch2_src = re.compile(' ?\n"') _path_patch2_dst = '"' -# Mapping from color codes to CSS classes +# Mapping from color codes to CSS classes. Matplotlib 3.5 added quite a lot of +# spacing to the style attributes compared to previous versions (and dropped +# trailing semicolons), so there's a lot of ` ?` and ` ;` to cover both +# variants. _class_mapping = [ # Graph background - ('style="fill:#cafe01;"', 'class="m-background"'), + (re.compile('style="fill: ?#cafe01;?"'), 'class="m-background"'), # Tick definition in - ('style="stroke:#cafe02;stroke-width:0.8;"', 'class="m-line"'), - # , everything is defined in , no need to repeat - (', everything is defined in , no need to repeat. Matplotlib 3.5 + # has style at the end of the tag, so preserving whatever is in between. + (re.compile(']*) style="fill: ?#cafe02; ?stroke: ?#cafe02; ?stroke-width: ?0.8;?"'), ' definition in - ('style="stroke:#cafe0a;"', 'class="m-error"'), + (re.compile('style="stroke: ?#cafe0a;?"'), 'class="m-error"'), # , everything is defined in , no need to repeat - (']*) style="fill: ?#cafe0a; ?stroke: ?#cafe0a;?"'), ' + + + + m.plots | A Pelican Blog + + + + + + +
+
+
+
+
+
+

m.plots

+ +

Note: the test uses DejaVu Sans instead of Source Sans Pro in order to have +predictable rendering on the CIs.

+
+ + + + + + + + + + 15.0 meters, i guess? + + + 30.0 meters, i guess? + + + + + + + + + + + + + + 0 + + + + + + + + + + 5 + + + + + + + + + + 10 + + + + + + + + + + 15 + + + + + + + + + + 20 + + + + + + + + + + 25 + + + + + + + + + + 30 + + + + meters, i guess? + + + + + + + + + + + + + + First + + + + + + + + + + Second + + + + + A plot with a single color + + + + + + + + + +
+
+

A plot with separate colors, extra labels, error bars and custom width + height

+
+ + + + + + + + + + 3.0 ± 0.1 Mondays + + + 4.0 ± 2.1 Mondays + + + 5.0 ± 1.0 Mondays + + + + + + + + + + + + + + 0 + + + + + + + + + + 1 + + + + + + + + + + 2 + + + + + + + + + + 3 + + + + + + + + + + 4 + + + + + + + + + + 5 + + + + + + + + + + 6 + + + + Mondays + + + + + + + + + + + + + + January + + + + + + + + + + + February + + + + + + + + + + March + + + + + + + + + + + + + + + + + + + + + + + + + + + + + a paradise + + + + hell! + + + Yes. + + + + + + + + + +
+
+
+ + + + + + + + + + 111.9 kB + + + 74.4 kB + + + 52.1 kB + + + 731.2 kB + + + 226.3 kB + + + 226.0 kB + + + + + + + + + + + + + + 0 + + + + + + + + + + 100 + + + + + + + + + + 200 + + + + + + + + + + 300 + + + + + + + + + + 400 + + + + + + + + + + 500 + + + + + + + + + + 600 + + + + + + + + + + 700 + + + + + + + + + + 800 + + + + kB + + + + + + + + + + + + + + A + + + + + + + + + + B + + + + + + + + + + C + + + + + Stacked plot + + + + + + + + + +
+
+ + + + + + + + + + 111.9 ± 25.0 kB + + + 74.4 ± 15.3 kB + + + 731.2 ± 200.0 kB + + + 226.3 ± 5.0 kB + + + + + + + + + + + + + + 0 + + + + + + + + + + 200 + + + + + + + + + + 400 + + + + + + + + + + 600 + + + + + + + + + + 800 + + + + + + + + + + 1000 + + + + kB + + + + + + + + + + + + + + A + + + + + + + + + + B + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Stacked plot with errors and full colors + + + + + + + + + +
+ +
+
+
+
+
+ + diff --git a/plugins/m/test/plots/page.html b/plugins/m/test/plots/page.html index 87863bad..b01ee253 100644 --- a/plugins/m/test/plots/page.html +++ b/plugins/m/test/plots/page.html @@ -30,7 +30,7 @@ div.m-plot svg { font-family: DejaVu Sans; }
- + @@ -38,122 +38,122 @@ div.m-plot svg { font-family: DejaVu Sans; } 15.0 meters, i guess? - + 30.0 meters, i guess? - + - + - + - 0 + 0 - + - 5 + 5 - + - 10 + 10 - + - 15 + 15 - + - 20 + 20 - + - 25 + 25 - + - 30 + 30 - meters, i guess? + meters, i guess? - + - + - First + First - + - Second + Second - A plot with a single color + A plot with a single color - + @@ -163,7 +163,7 @@ div.m-plot svg { font-family: DejaVu Sans; }
- + @@ -171,100 +171,100 @@ div.m-plot svg { font-family: DejaVu Sans; } 3.0 ± 0.1 Mondays - + 4.0 ± 2.1 Mondays - + 5.0 ± 1.0 Mondays - + - + - + - 0 + 0 - + - 1 + 1 - + - 2 + 2 - + - 3 + 3 - + - 4 + 4 - + - 5 + 5 - + - 6 + 6 - Mondays + Mondays - + - + @@ -275,17 +275,17 @@ div.m-plot svg { font-family: DejaVu Sans; } - + - February + February - + @@ -295,25 +295,25 @@ div.m-plot svg { font-family: DejaVu Sans; } - - - + + + - + - - - + + + - - - + + + @@ -325,13 +325,13 @@ div.m-plot svg { font-family: DejaVu Sans; } hell! - Yes. + Yes. - + @@ -340,7 +340,7 @@ div.m-plot svg { font-family: DejaVu Sans; }
- + @@ -348,164 +348,164 @@ div.m-plot svg { font-family: DejaVu Sans; } 111.9 kB - + 74.4 kB - + 52.1 kB - + 731.2 kB - + 226.3 kB - + 226.0 kB - + - + - + - 0 + 0 - + - 100 + 100 - + - 200 + 200 - + - 300 + 300 - + - 400 + 400 - + - 500 + 500 - + - 600 + 600 - + - 700 + 700 - + - 800 + 800 - kB + kB - + - + - A + A - + - B + B - + - C + C - Stacked plot + Stacked plot - + @@ -513,7 +513,7 @@ div.m-plot svg { font-family: DejaVu Sans; }
- + @@ -521,153 +521,153 @@ div.m-plot svg { font-family: DejaVu Sans; } 111.9 ± 25.0 kB - + 74.4 ± 15.3 kB - + 731.2 ± 200.0 kB - + 226.3 ± 5.0 kB - + - + - + - 0 + 0 - + - 200 + 200 - + - 400 + 400 - + - 600 + 600 - + - 800 + 800 - + - 1000 + 1000 - kB + kB - + - + - A + A - + - B + B - - - - - - + + - + - - + + - - + + + + + + - - + + - - + + - Stacked plot with errors and full colors + Stacked plot with errors and full colors - + diff --git a/plugins/m/test/test_plots.py b/plugins/m/test/test_plots.py index 5211ad73..aaaea2b5 100644 --- a/plugins/m/test/test_plots.py +++ b/plugins/m/test/test_plots.py @@ -42,8 +42,10 @@ class Plots(PelicanPluginTestCase): }) # FUCK this is annoying - if LooseVersion(matplotlib.__version__) >= LooseVersion('3.4'): + if LooseVersion(matplotlib.__version__) >= LooseVersion('3.5'): self.assertEqual(*self.actual_expected_contents('page.html')) + elif LooseVersion(matplotlib.__version__) >= LooseVersion('3.4'): + self.assertEqual(*self.actual_expected_contents('page.html', 'page-34.html')) elif LooseVersion(matplotlib.__version__) >= LooseVersion('3.2'): self.assertEqual(*self.actual_expected_contents('page.html', 'page-32.html')) elif LooseVersion(matplotlib.__version__) >= LooseVersion('3.0'):