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

Use "# %%" as code block separators in gallery examples #2660

Closed
seisman opened this issue Sep 4, 2023 · 1 comment · Fixed by #2662
Closed

Use "# %%" as code block separators in gallery examples #2660

seisman opened this issue Sep 4, 2023 · 1 comment · Fixed by #2662
Labels
maintenance Boring but important stuff for the core devs
Milestone

Comments

@seisman
Copy link
Member

seisman commented Sep 4, 2023

For gallery examples, it usually makes sense to split a long script into multiple blocks using block separators. The Sphinx-Gallery package supports three different "block separators/splitters"

  1. more than 20 # characters, i.e., ###################################################
  2. # %%
  3. #%%

Reference: https://sphinx-gallery.github.io/stable/syntax.html#embed-rest-in-your-example-python-files

The separator #%% will always be formatted to # %% by black, following PEP-8 convention, so this kind of separator can be ignored.

Currently, we use the # separator in our gallery examples (e.g., https://github.com/GenericMappingTools/pygmt/blob/main/examples/gallery/maps/tilemaps.py). I propose to use # %% instead.

The main reason is that # %% is also supported by Visual Studio Code and other editors/IDEs (see https://code.visualstudio.com/docs/python/jupyter-support-py#_jupyter-code-cells), making Python scripts working like Jupyter Notebooks. The # %% also can save us a lot of keystrokes.

@seisman seisman added the maintenance Boring but important stuff for the core devs label Sep 4, 2023
@weiji14
Copy link
Member

weiji14 commented Sep 6, 2023

Agree to use this 'percent' format, which was introduced by Spyder in 2013 according to https://jupytext.readthedocs.io/en/latest/formats-scripts.html#the-percent-format. For reference, the 3 different types of separators for code blocks in Sphinx Gallery is documented at https://sphinx-gallery.github.io/stable/tutorials/plot_parse.html#alternating-text-and-code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants