Skip to content

Commit 1aa34a5

Browse files
committed
Added bazel support to matplotlib-cpp
1 parent ef0383f commit 1aa34a5

File tree

13 files changed

+1776
-0
lines changed

13 files changed

+1776
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333

3434
# Build
3535
/examples/build/*
36+
bazel-*
3637

3738
# vim temp files
3839
*.sw*
40+
41+
# IDE files
42+
.clwb

BUILD.bazel

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
load("@my_pip//:requirements.bzl", "all_requirements")
2+
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
3+
load("//tools:embedding.bzl", "cc_py_library")
4+
5+
# This stanza calls a rule that generates targets for managing pip dependencies
6+
# with pip-compile. To update, run `bazel run //:requirements.update`
7+
compile_pip_requirements(
8+
name = "requirements",
9+
src = "requirements.in",
10+
requirements_txt = "requirements_lock.txt",
11+
requirements_windows = "requirements_windows.txt",
12+
)
13+
14+
cc_py_library(
15+
name = "matplotlibcpp",
16+
srcs = ["matplotlibcpp.h"],
17+
hdrs = ["matplotlibcpp.h"],
18+
includes = ["matplotlibcpp.h"],
19+
py_deps = all_requirements,
20+
visibility = ["//visibility:public"],
21+
deps = [
22+
"@rules_python//python/cc:current_py_cc_headers",
23+
"@rules_python//python/cc:current_py_cc_libs",
24+
],
25+
)

MODULE.bazel

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
###############################################################################
2+
# Bazel now uses Bzlmod by default to manage external dependencies.
3+
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
4+
#
5+
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
6+
###############################################################################
7+
8+
## MODULE.bazel
9+
module(
10+
name = "matplotlibcpp",
11+
repo_name = "matplotlibcpp",
12+
)
13+
14+
bazel_dep(name = "rules_python", version = "0.31.0")
15+
16+
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
17+
python.toolchain(
18+
is_default = True,
19+
python_version = "3.11",
20+
)
21+
use_repo(python, "python_versions")
22+
23+
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
24+
pip.parse(
25+
hub_name = "my_pip",
26+
python_version = "3.11",
27+
requirements_lock = "//:requirements_lock.txt",
28+
requirements_windows = "//:requirements_windows.txt",
29+
)
30+
use_repo(pip, "my_pip")

MODULE.bazel.lock

Lines changed: 1078 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WORKSPACE

Whitespace-only changes.

examples/bazel/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cc_binary(
2+
name = "test_plot",
3+
srcs = ["test_matplotlib.cpp"],
4+
deps = ["//:matplotlibcpp"],
5+
)

examples/bazel/test_matplotlib.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
#define _USE_MATH_DEFINES
3+
#include <iostream>
4+
#include <cmath>
5+
#include "matplotlibcpp.h"
6+
7+
namespace plt = matplotlibcpp;
8+
9+
int main() {
10+
// Prepare data.
11+
int n = 5000;
12+
std::vector<double> x(n), y(n), z(n), w(n,2);
13+
for(int i=0; i<n; ++i) {
14+
x.at(i) = i*i;
15+
y.at(i) = sin(2*M_PI*i/360.0);
16+
z.at(i) = log(i);
17+
}
18+
19+
// Set the size of output image = 1200x780 pixels
20+
plt::figure_size(1200, 780);
21+
22+
// Plot line from given x and y data. Color is selected automatically.
23+
plt::plot(x, y);
24+
25+
// Plot a red dashed line from given x and y data.
26+
plt::plot(x, w,"r--");
27+
28+
// Plot a line whose name will show up as "log(x)" in the legend.
29+
plt::named_plot("log(x)", x, z);
30+
31+
// Set x-axis to interval [0,1000000]
32+
plt::xlim(0, 1000*1000);
33+
34+
// Add graph title
35+
plt::title("Sample figure");
36+
37+
// Enable legend.
38+
plt::legend();
39+
40+
// save figure
41+
plt::show();
42+
std::string filename {"/tmp/basic.png"};
43+
std::cout << "Saving result to " << filename.c_str() << std::endl;;
44+
plt::save(filename);
45+
46+
return 0;
47+
}
48+

matplotlibcpp.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ struct _interpreter {
145145
}
146146

147147
private:
148+
std::string old_python_launcher{};
149+
std::string old_python_home{};
150+
std::string old_python_path{};
148151

149152
#ifndef WITHOUT_NUMPY
150153
# if PY_MAJOR_VERSION >= 3
@@ -172,6 +175,17 @@ struct _interpreter {
172175
char name[] = "plotting";
173176
#endif
174177
Py_SetProgramName(name);
178+
#ifdef BAZEL_BUILD
179+
// Copy current environment variables
180+
old_python_launcher = getenv("__PYVENV_LAUNCHER__") ? getenv("__PYVENV_LAUNCHER__") : "";
181+
old_python_home = getenv("PYTHONHOME") ? getenv("PYTHONHOME") : "";
182+
old_python_path = getenv("PYTHONPATH") ? getenv("PYTHONPATH") : "";
183+
184+
// Set variables from Bazel definitions in tools/embedding.bzl
185+
setenv("__PYVENV_LAUNCHER__", CPP_PYVENV_LAUNCHER, true);
186+
setenv("PYTHONHOME", CPP_PYTHON_HOME, true);
187+
setenv("PYTHONPATH", CPP_PYTHON_PATH, true);
188+
#endif
175189
Py_Initialize();
176190

177191
wchar_t const *dummy_args[] = {L"Python", NULL}; // const is needed because literals must not be modified
@@ -286,6 +300,12 @@ struct _interpreter {
286300

287301
~_interpreter() {
288302
Py_Finalize();
303+
304+
#ifdef BAZEL_BUILD
305+
setenv("__PYVENV_LAUNCHER__", old_python_launcher.c_str(), true);
306+
setenv("PYTHONHOME", old_python_home.c_str(), true);
307+
setenv("PYTHONPATH", old_python_path.c_str(), true);
308+
#endif
289309
}
290310
};
291311

requirements.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
numpy<2
2+
matplotlib
3+
PyQt5
4+
PySide2

0 commit comments

Comments
 (0)