Skip to content

Commit 653c2ba

Browse files
Sfonxuslayoo
andauthored
create test and pylint CI workflows; use devops_tests as a submodule; add show_anim(); bump checkout & setup-python actions in pylint CI job; move more setup entries into pyproject.toml; add example notebook with animation (#30)
Co-authored-by: Sylwester Arabas <[email protected]>
1 parent 692d4f6 commit 653c2ba

File tree

9 files changed

+181
-19
lines changed

9 files changed

+181
-19
lines changed

.github/workflows/pylint.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ jobs:
1212
runs-on: ubuntu-latest
1313

1414
steps:
15-
- uses: actions/checkout@v2
16-
- name: Set up Python 3.9
17-
uses: actions/setup-python@v2
15+
- uses: actions/checkout@v4
16+
- name: Set up Python 3.12
17+
uses: actions/setup-python@v5
1818
with:
19-
python-version: 3.9
19+
python-version: 3.12
2020
- name: Install dependencies
2121
run: |
22-
python -m pip install --upgrade pip
22+
python -m pip install --upgrade pip setuptools
2323
pip install pylint
2424
python setup.py egg_info
2525
pip install -r *.egg-info/requires.txt

.github/workflows/tests.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
with:
17+
submodules: true
18+
- name: Set up Python 3.12
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: 3.12
22+
- name: Run tests
23+
run: |
24+
pip install -e .
25+
pip install -r tests/devops_tests/requirements.txt
26+
pytest -vv -rP -We tests/devops_tests/test_notebooks.py::test_run_notebooks

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "tests/devops_tests.git"]
2+
path = tests/devops_tests
3+
url = [email protected]:open-atmos/devops_tests.git

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
Utility routines used in Jupyter notebooks in [PySDM](https://github.com/open-atmos/PySDM), [PyMPDATA](https://github.com/open-atmos/PyMPDATA) and [PyPartMC](https://github.com/open-atmos/PyPartMC) projects:
88
- [``show_plot()``](https://open-atmos.github.io/jupyter-utils/open_atmos_jupyter_utils/show_plot.html) - a drop-in replacement for matplotlib's show() displaying the figure inline using vector graphics (svg) by default and offering a download-as-pdf-or-svg widget just below (on Colab the widget triggers Google Drive download)
9+
- [``show_anim()``](https://open-atmos.github.io/jupyter-utils/open_atmos_jupyter_utils/show_anim.html) - a replacement for matplotlib's FuncAnimate() that inline-displays animations in gif format (thus github renderer compatible) and offers a way to download the .gif file (on Colab the widget triggers Google Drive download)
910
- [``TemporaryFile``](https://open-atmos.github.io/jupyter-utils/open_atmos_jupyter_utils/temporary_file.html) - a class equipped with ``make_link_widget()`` method returning a click-to-download Colab-compatible widget to be display()-ed in a Jupyter notebook
1011
- [``pip_install_on_colab('package_a', 'package_b', ...)``](https://open-atmos.github.io/jupyter-utils/open_atmos_jupyter_utils/pip_install_on_colab.html) - a function handling execution of ``pip`` (and ``ldconfig``) on Colab
1112

examples/show_anim_example.ipynb

+83
Large diffs are not rendered by default.

open_atmos_jupyter_utils/show_anim.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""inline animation function that allows rendering on github
2+
preview and showing click to download gif button"""
3+
4+
#pylint: disable=consider-using-with
5+
import os
6+
import tempfile
7+
import base64
8+
import imageio
9+
import matplotlib.pyplot as plt
10+
from IPython.display import HTML, display
11+
from open_atmos_jupyter_utils.temporary_file import TemporaryFile
12+
13+
14+
def show_anim(plot_func, frame_range, duration=0.01, loop=0):
15+
"""plot_func is called with one argument - the frame number from frame_range
16+
and is expected to return a matplotlib figure instance (on which savefig()
17+
and close() are subsequently called)"""
18+
gif_file = TemporaryFile(suffix=".gif")
19+
with tempfile.TemporaryDirectory() as tmpdirname:
20+
for frame in frame_range:
21+
fig = plot_func(frame)
22+
fig.savefig(f"{tmpdirname}/{frame:05d}.png")
23+
plt.close(fig)
24+
__merge_images_into_gif(tmpdirname, gif_file, duration, loop)
25+
26+
b64 = base64.b64encode(open(gif_file.basename,'rb').read()).decode("ascii")
27+
display(HTML(f'<img src="data:image/gif;base64,{b64}" />'))
28+
display(gif_file.make_link_widget())
29+
30+
31+
def __merge_images_into_gif(image_folder, gif_name, duration, loop):
32+
"""creates a GIF file from a series of animation frames"""
33+
with imageio.get_writer(
34+
gif_name.basename, duration=duration, loop=loop, mode="I"
35+
) as writer:
36+
for filename in sorted(os.listdir(image_folder)):
37+
image = imageio.v3.imread(os.path.join(image_folder, filename))
38+
writer.append_data(image)

pyproject.toml

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
[tool.setuptools]
2+
13
[tool.setuptools_scm]
24

35
# TODO
46
[build-system]
5-
requires = ['setuptools==56.0.0', 'setuptools-scm==6.0.1']
7+
requires = ['setuptools==75.3.0', 'setuptools-scm==8.1.0']
8+
build-backend = "setuptools.build_meta"
69

10+
[project]
11+
name = 'open-atmos-jupyter-utils'
12+
description = "utility routines used in PySDM, PyMPDATA and PyPartMC examples and tests"
13+
license = {text = "GPL-3.0"}
14+
readme = "README.md"
15+
dynamic = ["version"]
16+
dependencies = [
17+
"ipywidgets",
18+
"IPython",
19+
"matplotlib",
20+
"imageio",
21+
]
722
[project.urls]
823
"Homepage" = "https://github.com/open-atmos/jupyter-utils"
924
"Source" = "https://github.com/open-atmos/jupyter-utils"

setup.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
the magick behind ``pip install ...``
33
"""
4+
45
from setuptools import setup, find_packages
56

67

@@ -12,19 +13,13 @@ def get_long_description():
1213

1314

1415
setup(
15-
name='open-atmos-jupyter-utils',
16-
description='utility routines used in PySDM, PyMPDATA and PyPartMC examples and tests',
17-
use_scm_version={
18-
"local_scheme": lambda _: "",
19-
"version_scheme": "post-release"
20-
},
21-
setup_requires=['setuptools_scm'],
22-
install_requires=['ipywidgets', 'IPython', 'matplotlib'],
23-
author='https://github.com/open-atmos/jupyter-utils/graphs/contributors',
24-
author_email='[email protected]',
25-
license="GPL-3.0",
26-
packages=find_packages(include=['open_atmos_jupyter_utils', 'open_atmos_jupyter_utils.*']),
16+
use_scm_version={"local_scheme": lambda _: "", "version_scheme": "post-release"},
17+
setup_requires=["setuptools_scm", "setuptools"],
18+
install_requires=["ipywidgets", "IPython", "matplotlib", "imageio"],
19+
packages=find_packages(
20+
include=["open_atmos_jupyter_utils", "open_atmos_jupyter_utils.*"]
21+
),
2722
long_description=get_long_description(),
2823
long_description_content_type="text/markdown",
29-
url='https://github.com/open-atmos/jupyter-utils',
24+
url="https://github.com/open-atmos/jupyter-utils",
3025
)

tests/devops_tests

Submodule devops_tests added at 8eb4d96

0 commit comments

Comments
 (0)