Skip to content

Commit

Permalink
Improve the built examples (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharsadhwani authored May 19, 2024
1 parent b2ec05d commit 3da7b8e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 65 deletions.
46 changes: 36 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,20 @@ jobs:
id: build
shell: bash
run: |
# ./examples/matplotlib
MATPLOTLIB_NAME="matplotlib-${{ matrix.target }}.bin"
MATPLOTLIB_PATH=${PWD}/${MATPLOTLIB_NAME}
packaged $MATPLOTLIB_NAME 'pip install -r requirements.txt' 'python bubble_sort_curve.py' ./example/matplotlib
# aliens: requires no source
ALIENS_NAME="aliens-${{ matrix.target }}.bin"
ALIENS_PATH=${PWD}/${ALIENS_NAME}
packaged $ALIENS_NAME 'pip install pygame' 'python -m pygame.examples.aliens'
# textual: requires no source
TEXTUAL_NAME="textual-${{ matrix.target }}.bin"
TEXTUAL_PATH=${PWD}/${TEXTUAL_NAME}
packaged $TEXTUAL_NAME 'pip install pygame' 'python -m textual'
# ./examples/mandelbrot
MANDELBROT_NAME="mandelbrot-${{ matrix.target }}.bin"
MANDELBROT_PATH=${PWD}/${MANDELBROT_NAME}
packaged $MANDELBROT_NAME 'pip install -r requirements.txt' 'python mandelbrot.py' ./example/mandelbrot
# ./examples/minesweeper
MINESWEEPER_NAME="minesweeper-${{ matrix.target }}.bin"
Expand All @@ -87,16 +97,32 @@ jobs:
mv ./minesweeper.bin $MINESWEEPER_NAME
# Setup output paths for upload
echo "MATPLOTLIB_NAME=${MATPLOTLIB_NAME}" >> $GITHUB_OUTPUT
echo "MATPLOTLIB_PATH=${MATPLOTLIB_PATH}" >> $GITHUB_OUTPUT
echo "ALIENS_NAME=${ALIENS_NAME}" >> $GITHUB_OUTPUT
echo "ALIENS_PATH=${ALIENS_PATH}" >> $GITHUB_OUTPUT
echo "TEXTUAL_NAME=${TEXTUAL_NAME}" >> $GITHUB_OUTPUT
echo "TEXTUAL_PATH=${TEXTUAL_PATH}" >> $GITHUB_OUTPUT
echo "MANDELBROT_NAME=${MANDELBROT_NAME}" >> $GITHUB_OUTPUT
echo "MANDELBROT_PATH=${MANDELBROT_PATH}" >> $GITHUB_OUTPUT
echo "MINESWEEPER_NAME=${MINESWEEPER_NAME}" >> $GITHUB_OUTPUT
echo "MINESWEEPER_PATH=${MINESWEEPER_PATH}" >> $GITHUB_OUTPUT
- name: Upload matplotlib
- name: Upload aliens
uses: actions/upload-artifact@v4
with:
name: ${{ steps.build.outputs.ALIENS_NAME }}
path: ${{ steps.build.outputs.ALIENS_PATH }}

- name: Upload textual
uses: actions/upload-artifact@v4
with:
name: ${{ steps.build.outputs.TEXTUAL_NAME }}
path: ${{ steps.build.outputs.TEXTUAL_PATH }}

- name: Upload mandelbrot
uses: actions/upload-artifact@v4
with:
name: ${{ steps.build.outputs.MATPLOTLIB_NAME }}
path: ${{ steps.build.outputs.MATPLOTLIB_PATH }}
name: ${{ steps.build.outputs.MANDELBROT_NAME }}
path: ${{ steps.build.outputs.MANDELBROT_PATH }}

- name: Upload minesweeper
uses: actions/upload-artifact@v4
Expand All @@ -110,7 +136,7 @@ jobs:
with:
draft: true
files: |
${{ steps.build.outputs.MATPLOTLIB_PATH }}
${{ steps.build.outputs.MANDELBROT_PATH }}
${{ steps.build.outputs.MINESWEEPER_PATH }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ All examples below create a self contained executable. You can send the produced
binary file to another machine with the same OS and architecture, and it will
run the same.

### Graphs / matplotlib
### Mandelbrot (`numpy`, `matplotlib`, GUI)

```bash
packaged ./curve.bin 'pip install -r requirements.txt' 'python bubble_sort_curve.py' ./example/matplotlib
packaged ./mandelbrot.bin 'pip install -r requirements.txt' 'python mandelbrot.py' ./example/mandelbrot
```

This produces a `./curve.bin` binary with:
This produces a `./mandelbrot.bin` binary with:

- Python 3.12
- `matplotlib`
- `numba`
- `llvmlite`
- `pillow`

That outputs an interactive graph GUI.
That outputs an interactive mandelbrot set GUI.

### Minesweeper (using `packaged.toml` for configuration)

Expand All @@ -64,14 +64,14 @@ without `pyproject.toml` would be:
packaged minesweeper.bin 'pip install .' 'python -m minesweeper' ./example/minesweeper
```

### Textual Demo
### Textual (TUI) Demo

Since the dependencies themselves contain all the source code needed, you can
skip the last argument. With this, no other files will be packaged other than
what is produced in the build step.

```bash
packaged ./textualdemo.bin 'pip install textual' 'python -m textual'
packaged ./textual.bin 'pip install textual' 'python -m textual'
```

This will simply package the `textual` library's own demo into a single file.
Expand Down
44 changes: 44 additions & 0 deletions example/mandelbrot/mandelbrot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Calculate the Mandelbrot set. Program taken from RealPython:
https://realpython.com/mandelbrot-set-python
"""

import time
import warnings

import matplotlib.pyplot as plt
import numba
import numpy as np

warnings.filterwarnings("ignore")


@numba.njit
def create_mandelbrot(pixel_density, num_iterations):
xmin, xmax, ymin, ymax = -2, 0.5, -1.5, 1.5
re = np.linspace(xmin, xmax, int((xmax - xmin) * pixel_density))
im = np.linspace(ymin, ymax, int((ymax - ymin) * pixel_density))
c = re[np.newaxis, :] + im[:, np.newaxis] * 1j

z = np.zeros_like(c)
for _ in range(num_iterations):
z = z**2 + c
return z.real**2 + z.imag**2 <= 2


def main():
print("Calculating the mandelbrot set...")
t0 = time.time()
mandelbrot = create_mandelbrot(pixel_density=2048, num_iterations=20)
t1 = time.time()
print(f"Mandelbrot calculed in {t1 - t0:.2f} seconds")

plt.imshow(mandelbrot, cmap="binary")
plt.gca().set_aspect("equal")
plt.axis("off")
plt.tight_layout()
plt.show()


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
numpy
matplotlib
numba
49 changes: 0 additions & 49 deletions example/matplotlib/bubble_sort_curve.py

This file was deleted.

0 comments on commit 3da7b8e

Please sign in to comment.