Skip to content
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

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

Open
cwhite911 opened this issue Sep 18, 2024 · 2 comments · May be fixed by #4476
Open

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

cwhite911 opened this issue Sep 18, 2024 · 2 comments · May be fixed by #4476
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@cwhite911
Copy link
Contributor

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

@cwhite911 cwhite911 added enhancement New feature or request good first issue Good for newcomers labels Sep 18, 2024
@wenzeslaus
Copy link
Member

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

@rohannallamadge
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants