grass.jupyter: Add legend support to InteractiveMap (#5468)#7072
grass.jupyter: Add legend support to InteractiveMap (#5468)#7072saurabh12nxf wants to merge 11 commits intoOSGeo:mainfrom
Conversation
1f252e8 to
72fe5a9
Compare
|
Please include one or more screenshots, thank you. |
|
Hi @petrasovaa I have updated the description with the screenshots where i have shown the map without legend and map with legend |
Hey @saurabh12nxf, thanks for your contribution! Just a comment and question: the elevation map has continuous data, as you can see from the values; however, the legend appears as categories. Is that a default behaviour the user needs to change? d.legend in GRASS automatically creates a continuous bar for continuous data and categories for integer data. Why does the elevation map in your screenshot has a categorical legend? |
|
Good catch, thank you! @veroandreo I will push an update shortly. |
|
Thanks for the feedback @veroandreo @petrasovaa I updated the implementation to detect raster datatype using • elevation (FCELL/DCELL) - continuous gradient I also updated the screenshots in the PR description. Please let me know if anything else should be adjusted . And I have updated PR description which includes screenshots . |
|
Hi @petrasovaa and @veroandreo, Thank you again for all the helpful feedback during this PR! Seeing the legend feature come to life in the notebooks has been really satisfying. While looking at the codebase, I noticed that since legend.py I also noticed that the "Improve GRASS user experience in Jupyter Notebook" GSoC 2026 project idea has a Test of Skills that involves writing tests for grass.jupyter. I am planning to apply for this project, and writing test_legend.py feels like the perfect way to add genuine value to the codebase while also fulfilling that requirement. Would it be okay with you if I open a new PR dedicated to adding the pytest test suite for the legend |
|
Sorry for the late reply... The gradient legend in the screenshot looks incorrect. Yes, the PR should have a pytest testing the new functionality. |
| info = gs.raster_info(mapname) | ||
| datatype = info.get("datatype", "CELL") | ||
| is_categorical = datatype == "CELL" | ||
|
|
There was a problem hiding this comment.
[pre-commit] reported by reviewdog 🐶
| val_range = max_val - min_val if max_val != min_val else 1 | ||
|
|
||
| # Ipywidgets HTML sanitizer aggressively strips linear-gradient(). | ||
| # To ensure cross-frontend compatibility, we manually interpolate the |
There was a problem hiding this comment.
[pre-commit] reported by reviewdog 🐶
| # To ensure cross-frontend compatibility, we manually interpolate the | |
| # To ensure cross-frontend compatibility, we manually interpolate the |
| steps = 50 | ||
| for i in range(steps): | ||
| # Top of the bar is max_val, bottom is min_val | ||
| val = max_val - (i / max(1, steps - 1)) * val_range |
There was a problem hiding this comment.
[pre-commit] reported by reviewdog 🐶
| val = max_val - (i / max(1, steps - 1)) * val_range | |
| val = max_val - (i / max(1, steps - 1)) * val_range |
b80b3bb to
21f30e3
Compare
| # Gradient bar | ||
| ( | ||
| '<div style="width: 20px; height: 150px; ' | ||
| 'display: flex; flex-direction: column; ' |
There was a problem hiding this comment.
[ruff] reported by reviewdog 🐶
| 'display: flex; flex-direction: column; ' | |
| "display: flex; flex-direction: column; " |
21f30e3 to
3fe9ef1
Compare
|
Hi @petrasovaa, thanks for pointing that out! The issue was that Jupyter’s ipywidgets.HTML sanitizes CSS and strips To fix this, I moved the gradient generation to Python and render it as a stack of small I also added a pytest suite ( All tests passes locally, and I have updated the PR description with the screenshot. |

Added raster legend support to
grass.jupyter.InteractiveMapas requested in #5468.Changes
Usage
Map without legend
Map with legend