Skip to content

Commit 9a735cb

Browse files
cleaned up and automated the examples page.
1 parent 0080b9d commit 9a735cb

13 files changed

Lines changed: 143 additions & 38 deletions

.github/workflows/conda_test.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Run tests with miniconda
1+
name: Run tests with miniforge
22
on: [push]
33

44
jobs:
@@ -36,6 +36,11 @@ jobs:
3636
run: |
3737
pytest --pyargs py_gd
3838
39+
- name: Test Examples
40+
shell: bash -l {0}
41+
run: |
42+
python docs/examples/run_all_examples.py
43+
3944
lint:
4045
name: Flake8 linting
4146
runs-on: "ubuntu-latest"

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#specific to this repo:
22

33
py_gd/py_gd.c
4-
docs/examples/Mandelbrot.png
4+
docs/examples/*.png
5+
docs/examples/*.gif
56

67

78
# mac files
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
ultra_simple_example.py
2+
Ultra Simple Example
33
44
About the simplest example for py_gd
55
@@ -12,5 +12,5 @@
1212

1313
img.draw_line((1, 1), (400, 300), color='red', line_width=10)
1414

15-
img.save('ultra_simple.png', 'png')
15+
img.save('0-ultra_simple.png', 'png')
1616

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
from pathlib import Path
3+
4+
HERE = Path(__file__).parent
5+
6+
HEADER = """.. _sample_scripts:
7+
8+
Sample Scripts
9+
==============
10+
11+
A good way to learn to use py_gd is to check out a few example scripts.
12+
13+
"""
14+
15+
all_examples = [fn for fn in HERE.glob("*.py") if not fn.name in {"build_color_images.py",
16+
"run_all_examples.py",
17+
"build_example_page.py"}
18+
]
19+
20+
def gen_example(script_path):
21+
if "draw_dots" in str(script_path.stem):
22+
img_filename = str(script_path.stem) + ".gif"
23+
else:
24+
img_filename = str(script_path.stem) + ".png"
25+
26+
docstring = []
27+
with open(script_path) as script:
28+
# find the docstring
29+
for line in script:
30+
if line.startswith('"""'):
31+
break
32+
for line in script:
33+
line = line.strip()
34+
if line.startswith('"""'):
35+
break
36+
else:
37+
docstring.append(line)
38+
section = f"""
39+
40+
{docstring[0]}\n{"-"*len(docstring[0])}
41+
{"\n".join(docstring[1:])}
42+
43+
.. image:: examples/{img_filename}
44+
:align: center
45+
"""
46+
return section
47+
48+
with open(HERE / "../sample_scripts.rst", 'w') as rst:
49+
rst.write(HEADER)
50+
for script in all_examples:
51+
rst.write(gen_example(script))

docs/examples/colorramp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
colorramp.py
2+
Using a Color Ramp
33
44
Example of how to use a colorramp
55
6-
IN this case, we plot a "Heatmap" of a surface from the equation:
6+
In this case, we plot a "Heatmap" of a surface from the equation:
77
88
z = sin(x) + sin(y)
99
"""

docs/examples/draw_dots.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
draw_dots.py
2+
Animation Example
33
44
55
This draws a bunch of dots in different colors
@@ -26,11 +26,11 @@
2626
img.draw_dots(points, diameter=10, color=colors)
2727

2828
# Save the first frame as a PNG
29-
img.save('dots.png', 'png')
29+
img.save('draw_dots.png', 'png')
3030

3131

3232
# create the animation
33-
anim = Animation('dots.gif',
33+
anim = Animation('draw_dots.gif',
3434
delay=10, # in 1/100s
3535
)
3636

docs/examples/mandelbrot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
2-
Using py_gd to draw a Madelbrot set
2+
Drawing a Madelbrot set
33
4-
This sets the colors pixel by pixel
4+
Draws a Madelbrot by setting colors pixel by pixel
55
"""
66

77
import numpy as np

docs/examples/moderate_complex.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
"""
2+
Example with Moderate Complexity
3+
24
py_gd demo with moderate complexity -- showing a number of the features
35
4-
CAn you tell what's being drawn without looking at the results?
6+
Can you tell what's being drawn from the code without looking at the results?
57
68
"""
79

810
# Everything you need is in the Image object
11+
from py_gd import Image
912

13+
# Need some math ...
1014
from math import sin, cos, radians
1115

12-
from py_gd import Image
1316

1417
img = Image(width=800, height=600, preset_colors='xkcd')
1518

docs/examples/run_all_examples.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212

1313
HERE = Path(__file__).parent
1414

15-
def not_example(path):
16-
return path.name in {"build_color_images.py",
17-
"run_all_examples.py"}
1815

1916
all_examples = [fn for fn in HERE.glob("*.py") if not fn.name in {"build_color_images.py",
20-
"run_all_examples.py"}]
17+
"run_all_examples.py",
18+
"build_example_page.py"}
19+
]
2120

2221
failures = 0
2322
for script in all_examples:
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
"""
2-
Drawing a star with a spline
2+
Smooth Polygon
3+
4+
This example draws a star with rounded points, with varying roundedness.
5+
6+
It uses a "Spline Polygon:
7+
38
"""
49

510
import numpy as np

0 commit comments

Comments
 (0)