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

Rich, HTML representations for Jupyter Notebook #573

Closed
wants to merge 5 commits into from

Conversation

moradology
Copy link
Contributor

@moradology moradology commented Jul 20, 2021

Related Issue(s): #508

Description:

This PR is intended to provide useful HTML representations of some core STAC objects as well as a structure appropriate to adding more such representations. These are primarily used within Jupyter notebooks, but could in principle find use elsewhere.

image

PR Checklist:

  • Code is formatted (run pre-commit run --all-files)
  • Tests pass (run scripts/test)
  • Documentation has been updated to reflect changes, if applicable
  • This PR maintains or improves overall codebase code coverage.
  • Changes are added to the CHANGELOG. See the docs for information about adding to the changelog.

@moradology moradology changed the title Feature/html repr Rich, HTML representations for Jupyter Notebook Jul 20, 2021
@moradology
Copy link
Contributor Author

In the above screenshot, detail/summary pairs don't appear to be rendering with arrows on their left (as expected). I'm not sure if this is a quirk of my particular installation, but would be interested if anyone has ideas

@moradology moradology marked this pull request as draft July 20, 2021 16:02
@codecov-commenter
Copy link

codecov-commenter commented Jul 20, 2021

Codecov Report

Merging #573 (6d44d06) into main (76ef6a3) will decrease coverage by 0.06%.
The diff coverage is 60.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #573      +/-   ##
==========================================
- Coverage   94.67%   94.60%   -0.07%     
==========================================
  Files          75       77       +2     
  Lines       10732    10751      +19     
  Branches     1048     1048              
==========================================
+ Hits        10160    10171      +11     
- Misses        396      404       +8     
  Partials      176      176              
Impacted Files Coverage Δ
pystac/collection.py 92.73% <50.00%> (-0.75%) ⬇️
pystac/item.py 89.61% <50.00%> (-0.89%) ⬇️
pystac/link.py 89.09% <50.00%> (-0.98%) ⬇️
pystac/catalog.py 91.21% <60.00%> (-0.44%) ⬇️
pystac/html/__init__.py 100.00% <100.00%> (ø)
pystac/html/jinja_env.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 76ef6a3...6d44d06. Read the comment docs.

Copy link
Contributor

@duckontheweb duckontheweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @moradology , I like the idea of having these rich representations for contexts like Jupyter.

My biggest concern with this approach is adding jinja2 as a dependency. PySTAC has been extremely careful about not adding non-essential dependencies (see this comment from @lossyrob that gives some good rationale for this), and since jinja2 is only being added to support a narrow set of features I don't feel that it's worth breaking that tradition.

I'm wondering if this kind of functionality would be better in a separate library (e.g. pystac-jupyter, pystac-html, etc.) that could provide more specific support in these environments. If we do want it in PySTAC, I think we should try to accomplish this through a combination of factory functions and f-strings instead of adding a new dependency.

@moradology
Copy link
Contributor Author

After writing this, reading your comment and a very brief discussion with @lossyrob, I'm inclined to agree that adding 1/4th more dependencies(!) is probably not wise. I'm not a huge fan of the f-string approach as it stands to add some clutter to already rather large classes and jinja templates are awfully convenient by comparison. Ironically, the f-string approach has fewer downsides within an external project focused on ergonomics inside the Jupyter context. My vote for preserving this kind of functionality - and I do think it is valuable for those who like to develop and to produce learning materials with Jupyter - would be to extend the primary pystac classes as necessary from inside an external project like pystac-jupyter, as you mentioned.

@lossyrob
Copy link
Member

I'd agree that creating a pystac-jupyter package is the best route, though a bit heavier of a lift.

Noting that @mmcfarland pointed to these docs previously, which allows you to set custom formatters for types and would probably be useful here https://ipython.readthedocs.io/en/stable/config/integrating.html#formatters-for-third-party-types

@duckontheweb
Copy link
Contributor

@moradology Are you okay with me closing this PR since it sounds like this will be implemented as a separate library?

@moradology
Copy link
Contributor Author

Very

@moradology moradology closed this Jul 27, 2021
DahnJ added a commit to DahnJ/pystac that referenced this pull request Feb 3, 2022
Starting point is stac-utils#573
Improvements
- Jinja dependency is optional, otherwise __repr__ is used
- Catalog shows children
- get_items() is kept lazy
- Minor improvements in the Jinja templates
duckontheweb added a commit that referenced this pull request May 13, 2022
* Add rudimentary optional _repr_html_()

Starting point is #573
Improvements
- Jinja dependency is optional, otherwise __repr__ is used
- Catalog shows children
- get_items() is kept lazy
- Minor improvements in the Jinja templates

* Refactor Jinja templates, Assets show title

- Use macros to simplify Jinja templates
- Assets show title before expansion

* All pre-commit checks pass

* Jinja templates are distributed with the package

Tested by
building a wheel (python -m build) after a pip install build
creating a new virtualenv
installing from the built wheel in the new virtualenv
installing jupyter notebooks and testing the html repr

* Jinja is imported lazily at the first _repr_html_ call

Previously, importing pystac would attempt to import jinja
This means jinja would be imported even if the html repr was never used

* REF: Separate out Catalog and Collection templates

* Style: All list <detail>s have identical margins

* Refactor: Collection template inherits from Catalog

* Remove unused parameter

* Add provider template

* Catalog-derived templates use class name as title

* Add PR link to changelog

* WIP: HTML repr does not retrieve all children&items

* Remove accidentally commited notebook

* Formatting (black)

* Refactor: Limit try/except scope

* Mypy ignores optional dependency jinja2

jinja2 is used for optional html_repr for Jupyter Notebooks
and should thus not be required

* Moving unreleased PR from 1.4.0 to unreleased

* ItemCollection implements _repr_html_

* ItemCollection only shows at most 10 items

ItemCollection could theoretically have hundreds or thousands of items
which shouldn't all be shown in the HTML representation at once

Co-authored-by: Jon Duckworth <[email protected]>
Co-authored-by: Pete Gadomski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants