Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/build-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
# Trigger the workflow on push
push:
# Selected branches
branches: [develop, master, docs]
branches: [develop, master, docs, patch]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

Expand Down Expand Up @@ -104,7 +104,7 @@ jobs:
cp ../assets-branding/easyscience-org/icons/eso-icon_bw.svg overrides/.icons/easyscience.svg

# Convert python scripts in the notebooks directory to Jupyter notebooks
# Strip output from the notebooks and simpify cell ids
# Strip output from the notebooks, simpify cell ids and replace '# !pip' with '!pip'
# The notebooks are used to generate the documentation
- name:
Convert ${{ env.NOTEBOOKS_DIR }}/*.py to docs/${{env.NOTEBOOKS_DIR
Expand All @@ -113,6 +113,7 @@ jobs:
cp -R ${{ env.NOTEBOOKS_DIR }}/data docs/${{ env.NOTEBOOKS_DIR }}/
jupytext ${{ env.NOTEBOOKS_DIR }}/*.py --from py:percent --to ipynb
nbstripout ${{ env.NOTEBOOKS_DIR }}/*.ipynb
python tools/nb_uncomment_pip.py ${{ env.NOTEBOOKS_DIR }}/
mv ${{ env.NOTEBOOKS_DIR }}/*.ipynb docs/${{ env.NOTEBOOKS_DIR }}/

# The following step is needed to avoid the following message during the build:
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/publish-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ jobs:
- name: Upgrade package installer for Python
run: pip install --upgrade pip

- name: Install Python dependencies
run: pip install '.[dev]'

- name: Create Python package
run: python -m build

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test-tutorials-colab.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Testing tutorials on Colab
name: Test tutorials on Colab

on:
# Trigger the workflow on push
Expand Down Expand Up @@ -49,8 +49,8 @@ jobs:

- name: Install Python dependencies
run:
python -m pip install 'easydiffraction[charts]' nbconvert nbmake
pytest pytest-xdist
python -m pip install 'easydiffraction[visualization]' nbconvert
nbmake pytest pytest-xdist

- name: Check if Jupyter Notebooks run without errors
run: >
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test-tutorials.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ jobs:
# Check if tutorials as Jupyter Notebooks run without errors
# Convert all Python scripts in the folder tutorials/ to Jupyter Notebooks
# Strip output from Jupyter Notebooks and rename cell ids
# Replace '# !pip' with '!pip' in Jupyter Notebooks
# Run all Jupyter Notebooks in the folder tutorials/ in parallel
# -n auto: run as many in parallel as you have cores (auto mode)
# --nbmake-timeout=600: set timeout to 600 seconds
Expand All @@ -150,6 +151,7 @@ jobs:
run: |
jupytext ${{ env.NOTEBOOKS_DIR }}/*.py --from py:percent --to ipynb
nbstripout ${{ env.NOTEBOOKS_DIR }}/*.ipynb
python tools/nb_uncomment_pip.py ${{ env.NOTEBOOKS_DIR }}/

- name: Run tutorials as Jupyter Notebooks (using src/ as the source dir)
shell: bash
Expand Down
14 changes: 7 additions & 7 deletions docs/installation-and-setup/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ installed using `pip`. We strongly recommend installing it within a virtual
environment, as described in the [Environment Setup](#environment-setup)
section.

We recommend installing the latest release of EasyDiffraction with the `charts`
extras, which include optional dependencies used for simplified visualization of
charts and tables. This can be especially useful for running the Jupyter
Notebook examples. To do so, use the following command:
We recommend installing the latest release of EasyDiffraction with the
`visualization` extras, which include optional dependencies used for simplified
visualization of charts and tables. This can be especially useful for running
the Jupyter Notebook examples. To do so, use the following command:

```console
pip install 'easydiffraction[charts]'
pip install 'easydiffraction[visualization]'
```

If only the core functionality is needed, the library can be installed simply
Expand Down Expand Up @@ -128,10 +128,10 @@ To install EasyDiffraction from, e.g., the `develop` branch of GitHub:
pip install git+https://github.com/easyscience/diffraction-lib@develop
```

To include extra dependencies (e.g., charts):
To include extra dependencies (e.g., visualization):

```console
pip install 'easydiffraction[charts] @ git+https://github.com/easyscience/diffraction-lib@develop'
pip install 'easydiffraction[visualization] @ git+https://github.com/easyscience/diffraction-lib@develop'
```

## How to Run Tutorials
Expand Down
4 changes: 2 additions & 2 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

# Project information
site_name: EasyDiffraction Library
site_url: https://docs.easydiffraction.org/lib
site_url: https://docs.easydiffraction.org/lib/

# Repository
repo_url: https://github.com/easyscience/diffraction-lib
repo_url: https://github.com/easyscience/diffraction-lib/
edit_uri: edit/develop/docs/

# Copyright
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ classifiers = [
'Operating System :: OS Independent',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3',
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
Expand Down Expand Up @@ -74,7 +73,7 @@ visualization = [

[project.urls]
homepage = 'https://easydiffraction.org'
documentation = 'https://easyscience.github.io/diffraction-lib'
documentation = 'https://docs.easydiffraction.org/lib'
source = 'https://github.com/easyscience/diffraction-lib'
tracker = 'https://github.com/easyscience/diffraction-lib/issues'

Expand Down
108 changes: 108 additions & 0 deletions tools/nb_uncomment_pip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"""
Uncomment `# !pip ...` lines in Jupyter notebooks so they become `!pip ...`.

- Operates only on code cells (does not touch outputs/metadata/markdown).
- Matches lines that start with optional whitespace, then `# !pip` (e.g., " # !pip install ...").
- Rewrites to keep the original indentation and replace the leading "# !pip" with "!pip".
- Processes one or more paths (files or directories) given as CLI args, recursively for directories.
"""

from __future__ import annotations

import argparse
import re
import sys
from pathlib import Path

import nbformat

# Regex: beginning-of-line, capture leading whitespace, then "#", spaces, then "!pip"
_PATTERN = re.compile(r'^(\s*)#\s*!pip\b')


def fix_cell_source(src: str) -> tuple[str, int]:
"""
Replace lines starting with optional whitespace + '# !pip' with '!pip'.
Returns the updated source and number of replacements performed.
"""
changed = 0
new_lines: list[str] = []
for line in src.splitlines(keepends=False):
m = _PATTERN.match(line)
if m:
# Replace only the first '# !pip' at the beginning, preserve the rest of the line
# e.g., " # !pip install foo" -> " !pip install foo"
new_line = _PATTERN.sub(r'\1!pip', line, count=1)
if new_line != line:
changed += 1
new_lines.append(new_line)
else:
new_lines.append(line)
return ('\n'.join(new_lines), changed)


def process_notebook(path: Path) -> int:
"""
Process a single .ipynb file. Returns number of lines changed.
"""
nb = nbformat.read(path, as_version=4)
total_changes = 0
for cell in nb.cells:
if cell.cell_type != 'code':
continue
new_src, changes = fix_cell_source(cell.source or '')
if changes:
cell.source = new_src
total_changes += changes
if total_changes:
nbformat.write(nb, path)
return total_changes


def iter_notebooks(paths: list[Path]):
for p in paths:
if p.is_dir():
yield from (q for q in p.rglob('*.ipynb') if q.is_file())
elif p.is_file() and p.suffix == '.ipynb':
yield p


def main(argv: list[str]) -> int:
ap = argparse.ArgumentParser(description="Uncomment '# !pip ...' to '!pip ...' in code cells of .ipynb notebooks.")
ap.add_argument('paths', nargs='+', help='Notebook files or directories to process')
ap.add_argument('--dry-run', action='store_true', help='Report changes without writing files')
args = ap.parse_args(argv)

targets = list(iter_notebooks([Path(p) for p in args.paths]))
if not targets:
print('No .ipynb files found.', file=sys.stderr)
return 1

total_files = 0
total_changes = 0
for nb_path in targets:
changes = process_notebook(nb_path) if not args.dry_run else 0
if args.dry_run:
# For dry-run, compute changes without writing
nb = nbformat.read(nb_path, as_version=4)
changes = 0
for cell in nb.cells:
if cell.cell_type != 'code':
continue
_, c = fix_cell_source(cell.source or '')
changes += c
if changes:
action = 'UPDATED' if not args.dry_run else 'WOULD UPDATE'
print(f'{action}: {nb_path} ({changes} line(s))')
total_files += 1
total_changes += changes

if total_files == 0:
print('No changes needed.')
else:
print(f'Done. Files changed: {total_files}, lines changed: {total_changes}')
return 0


if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))
2 changes: 1 addition & 1 deletion tutorials/advanced_joint-fit_pd-neut-xray-cwl_PbSO4.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"if hasattr(builtins, '__IPYTHON__'):\n",
" if importlib.util.find_spec('easydiffraction') is None:\n",
" print('Installing the easydiffraction library...')\n",
" # !pip install 'easydiffraction[visualization]'"
" !pip install 'easydiffraction[visualization]'"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion tutorials/basic_single-fit_pd-neut-cwl_LBCO-HRPT.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"if hasattr(builtins, '__IPYTHON__'):\n",
" if importlib.util.find_spec('easydiffraction') is None:\n",
" print('Installing the easydiffraction library...')\n",
" # !pip install 'easydiffraction[visualization]'"
" !pip install 'easydiffraction[visualization]'"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion tutorials/cryst-struct_pd-neut-cwl_CoSiO4-D20.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"if hasattr(builtins, '__IPYTHON__'):\n",
" if importlib.util.find_spec('easydiffraction') is None:\n",
" print('Installing the easydiffraction library...')\n",
" # !pip install 'easydiffraction[visualization]'"
" !pip install 'easydiffraction[visualization]'"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion tutorials/cryst-struct_pd-neut-cwl_HS-HRPT.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"if hasattr(builtins, '__IPYTHON__'):\n",
" if importlib.util.find_spec('easydiffraction') is None:\n",
" print('Installing the easydiffraction library...')\n",
" # !pip install 'easydiffraction[visualization]'"
" !pip install 'easydiffraction[visualization]'"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion tutorials/cryst-struct_pd-neut-tof_Si-SEPD.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"if hasattr(builtins, '__IPYTHON__'):\n",
" if importlib.util.find_spec('easydiffraction') is None:\n",
" print('Installing the easydiffraction library...')\n",
" # !pip install 'easydiffraction[visualization]'"
" !pip install 'easydiffraction[visualization]'"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"if hasattr(builtins, '__IPYTHON__'):\n",
" if importlib.util.find_spec('easydiffraction') is None:\n",
" print('Installing the easydiffraction library...')\n",
" # !pip install 'easydiffraction[visualization]'"
" !pip install 'easydiffraction[visualization]'"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"if hasattr(builtins, '__IPYTHON__'):\n",
" if importlib.util.find_spec('easydiffraction') is None:\n",
" print('Installing the easydiffraction library...')\n",
" # !pip install 'easydiffraction[visualization]'"
" !pip install 'easydiffraction[visualization]'"
]
},
{
Expand Down
Loading