Skip to content

Commit 0554e25

Browse files
authored
feat: adding pip example (#64)
1 parent f1878d2 commit 0554e25

File tree

6 files changed

+444
-0
lines changed

6 files changed

+444
-0
lines changed

projects/bzl7/MODULE.bazel

+8
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,11 @@ use_repo(
9393
"python_3_12",
9494
"python_versions",
9595
)
96+
97+
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
98+
pip.parse(
99+
hub_name = "pip_example_3_12",
100+
python_version = "3.12",
101+
requirements_lock = "@bzl7//python/pip:requirements_lock.txt",
102+
)
103+
use_repo(pip, "pip_example_3_12")

projects/bzl7/python/pip/BUILD.bazel

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
load("@aspect_rules_py//py:defs.bzl", "py_binary")
2+
load("@pip_example_3_12//:requirements.bzl", "requirement")
3+
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
4+
5+
compile_pip_requirements(
6+
name = "python_requirements",
7+
requirements_in = ":requirements.txt",
8+
requirements_txt = ":requirements_lock.txt",
9+
tags = ["manual"],
10+
)
11+
12+
py_binary(
13+
name = "main",
14+
srcs = ["main.py"],
15+
visibility = ["//visibility:public"],
16+
deps = [
17+
requirement("matplotlib"),
18+
requirement("numpy"),
19+
],
20+
)

projects/bzl7/python/pip/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Updating Requirements
2+
To update the requirements, execute the following:
3+
```
4+
bazel run //python/pip:python_requirements.update
5+
```

projects/bzl7/python/pip/main.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
import warnings
4+
5+
from pathlib import Path
6+
7+
def compute_mandelbrot(max_iter: int, threshold: float, width: int, height: int) -> np.ndarray:
8+
"""
9+
Computes the Mandelbrot set over a specified grid.
10+
11+
Parameters:
12+
- max_iter: maximum number of iterations per point
13+
- threshold: escape threshold
14+
- width: number of points along the x-axis
15+
- height: number of points along the y-axis
16+
"""
17+
x = np.linspace(-2, 1, width)
18+
y = np.linspace(-1.5, 1.5, height)
19+
c = x[:, None] + 1j * y[None, :]
20+
21+
z = c.copy()
22+
23+
# Suppress any overflow.
24+
with warnings.catch_warnings():
25+
warnings.simplefilter("ignore")
26+
for _ in range(max_iter):
27+
z = z**2 + c
28+
mandelbrot_set = (np.abs(z) < threshold)
29+
30+
return mandelbrot_set
31+
32+
if __name__ == '__main__':
33+
MAX_ITER = 50
34+
THRESHOLD = 50.0
35+
WIDTH = 600
36+
HEIGHT = 400
37+
38+
mandelbrot_set = compute_mandelbrot(MAX_ITER, THRESHOLD, WIDTH, HEIGHT)
39+
40+
plt.imshow(mandelbrot_set.T, extent=[-2, 1, -1.5, 1.5])
41+
42+
# Save the file and print out where it was located.
43+
file_path = Path('example.png')
44+
plt.savefig(file_path)
45+
print("File created at:\n\t{}".format(file_path.resolve()))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
matplotlib
2+
numpy

0 commit comments

Comments
 (0)