OSGeo / grass

GRASS GIS - free and open-source geospatial processing engine
https://grass.osgeo.org
Other
846 stars 308 forks source link

[Feat] grass.jupyter: map.show #4340

Open cwhite911 opened 1 month ago

cwhite911 commented 1 month ago

GRASS Jupyter currently returns an IPython.display Image object when displaying a map image with a Jupyter notebook. As shown here.

def show(self):
        """Displays a PNG image of map"""
        # Lazy import to avoid an import-time dependency on IPython.
        from IPython.display import Image  # pylint: disable=import-outside-toplevel

        return Image(self._filename)

This is fine for many use cases but does not work when the image needs to be displayed inline with various Jupyter frontends (JupyterHub, Colab, VSCode, etc..). My specific use-case is using quarto (.qmd) to create figure layouts with VSCode that are rendered to html, pdf, or latex formats. Using the the current grass.jupyter api, you are unable to use the layout functionality of quarto without calling map.show() within the IPython.display display function like display(map.show().

Example using quarto layouts

#| label: fig-dsm-map
#| fig-cap-location: bottom
#| fig-cap: 1m DEMs
#| fig-subcap: 
#|      - "DSM"
#|      - "DTM"
#| column: body
#| output: true
#| layout-ncol: 2
#| echo: false

from IPython.display import display

map_dsm = gj.Map(width=250, use_region=False)
map_dsm.d_rast(map=dsm)
map_dsm.d_legend(raster=dsm, at="7,35,2,5", flags="b", unit="m")
map_dsm.d_barscale(at=(1,5), flags="n")
display(map_dsm.show())

map_dtm = gj.Map(width=250, use_region=False)
map_dtm.d_rast(map=dtm)
map_dtm.d_legend(raster=dtm, at="7,35,2,5", flags="b", unit="m")
map_dtm.d_barscale(at=(1,5), flags="n")
display(map_dtm.show())

To address this we should use the IPython.display display function to display the map image so that we let the Jupyter frontends decide which representation is used and how.

def show(self):
        """Displays a PNG image of map"""
        # Lazy import to avoid an import-time dependency on IPython.
        from IPython.display import Image, display  # pylint: disable=import-outside-toplevel

        display(Image(self._filename))

Read more here: https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#IPython.display.display

wenzeslaus commented 1 month ago

With that maybe we can also explore methods such as _repr_pretty_: https://ipython.readthedocs.io/en/stable/config/integrating.html

rohannallamadge commented 4 weeks ago

hello sir , I interested to resolve this issue , can you assign to me sir