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

Fix of Sphinx Gallery display of Chemiscope Visualizer #334

Closed
wants to merge 73 commits into from

Conversation

sofiia-chorna
Copy link
Collaborator

@sofiia-chorna sofiia-chorna commented May 17, 2024

Allow to display chemiscope viewers in sphinx documentations using sphinx-gallery.

@sofiia-chorna sofiia-chorna requested a review from Luthaf May 17, 2024 17:44
Copy link
Contributor

Choose a reason for hiding this comment

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

This file should not be committed to the repo long term (since it is pretty big and auto-generated). It's fine for now, I'll help replace it with the generated version of the file later

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I see, thanks!

docs/src/conf.py Outdated
import sphinx_bootstrap_theme

sys.path.insert(0, os.path.abspath('.'))
from scraper import SphinxGalleryScraper
Copy link
Contributor

Choose a reason for hiding this comment

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

this should something like from chemiscope.sphinx_gallery import ChemiscopeScraper, i.e. the code should live inside the chemiscope package to be usable by anyone with chemiscope installed (We should not just use it in this repo)

sphinx_gallery_conf = {
"examples_dirs": os.path.join(ROOT, "python", "examples"),
"gallery_dirs": "examples",
"filename_pattern": ".*",
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this is required to include the JSON files?

Comment on lines 56 to 58
<!-- Font-awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css" />
<link rel="icon" type="image/png" href="../../../_static/chemiscope-icon.png" sizes="32x32" />
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think these are needed here, since we are not adding any icons

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is an icon for loading

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, might be worth to try to use something smaller than the whole of fa here. Maybe a small svg instead?

></script>

<!-- JS scripts -->
<script src="../../../_static/js/chemiscope.min.js"></script>
Copy link
Contributor

Choose a reason for hiding this comment

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

You should load these from gallery_dirs, not the root!

self.copy_chemiscope_files(target_dir, source_files)

# Use the custom html content
widget._repr_html_ = lambda: self.generate_html_content(widget.value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of passing widget.value here, you should save the corresponding data in a JSON (or JSON/gzip file) — maybe using sphinx-gallery to create a filename like fig_xxx_.json.gz, there should be an iterator (block_vars["image_path_iterator"]) to generate such names — and then load it from JS with fetch.


def __call__(self, block, block_vars, gallery_conf):
variables = block_vars.get("example_globals", {})
widget = variables.get("___")
Copy link
Contributor

Choose a reason for hiding this comment

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

this might fail if the user tries to show multiple chemiscope in a single sphinx-gallery "cell"

widget._repr_html_ = lambda: self.generate_html_content(widget.value)

# Use sphinx-gallery standard scraper
return matplotlib_scraper(block, block_vars, gallery_conf)
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to do this? since the matplotlib scrapper is already in the list of scrappers in the conf

@ceriottm ceriottm marked this pull request as ready for review May 31, 2024 18:41
@sofiia-chorna sofiia-chorna requested a review from Luthaf June 4, 2024 15:55
@sofiia-chorna
Copy link
Collaborator Author

  • add a doc on how to set it up

@ceriottm
Copy link
Contributor

ceriottm commented Jun 7, 2024

Works great and I think this only needs some polish before we can merge and release. Amazing work @sofiia-chorna I'd say this is worthy of a point release at the very least, @Luthaf and I will also help starting on Tuesday. Meanwhile, something that clearly needs some polish is the documentation of this new feature. If I understand correctly, this can now be used in plain sphinx too - which is fantastic - but there's no example or explanation of how. @sofiia-chorna could you perhaps write down some docs? I'd rename the "use in sphinx-gallery" to a more generic "use with spinx" and discuss both direct inclusion and the generation from a python file using chemiscope.show.

@ceriottm
Copy link
Contributor

OK, it works - and so does the software cookbook so all is really nice, great job @sofiia-chorna .
I'd just like to understand a bit better how it works - for instance, it looks like I should be able to add a chemiscope to an arbitrary rst file by writing

.. chemiscope::
:filepath: /home/michele/lavoro/code/chemiscope/docs/src/examples/datasets/fig_trajectory_005.json.gz
            :mode: default

in it, however this does not work. Could you add a documentation of how this is supposed to be used wit (and without) sphinx-gallery?

@ceriottm
Copy link
Contributor

closing in favor of #341

@ceriottm ceriottm closed this Jun 13, 2024
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.

None yet

3 participants