Skip to content

Commit 8e45d30

Browse files
committed
Add source code.
1 parent b08a6c6 commit 8e45d30

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

main.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* MetaCall Embedding Matplotlib Example by Parra Studios
3+
* An example of embedding matplotlib from Python into C/C++.
4+
*
5+
* Copyright (C) 2016 - 2020 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include <metacall/metacall.h>
22+
#include <stdio.h>
23+
24+
#define metacall_value_create_str(str) \
25+
metacall_value_create_string(str, sizeof(str) - 1)
26+
27+
static int cleanup(int code)
28+
{
29+
if (metacall_destroy() != 0)
30+
{
31+
return code != 0 ? -code : -255;
32+
}
33+
34+
return code;
35+
}
36+
37+
int main(int argc, char *argv[])
38+
{
39+
struct metacall_log_stdio_type log_stdio = { stdout };
40+
41+
printf(metacall_print_info());
42+
43+
// Define log stream
44+
if (metacall_log(METACALL_LOG_STDIO, (void *)&log_stdio) != 0)
45+
{
46+
return cleanup(1);
47+
}
48+
49+
// Initialize MetaCall
50+
if (metacall_initialize() != 0)
51+
{
52+
return cleanup(2);
53+
}
54+
55+
// Python
56+
{
57+
// Array of scripts to be loaded by MetaCall
58+
const char * py_scripts[] =
59+
{
60+
"plot.py"
61+
};
62+
63+
const void * array_x[] = { metacall_value_create_int(1), metacall_value_create_int(2) };
64+
const void * array_y[] = { metacall_value_create_int(3), metacall_value_create_int(4) };
65+
66+
// Parameters to be passed to the plot function
67+
void * args[] =
68+
{
69+
metacall_value_create_str("Hello World"), // title
70+
metacall_value_create_array(array_x, sizeof(array_x) / sizeof(array_x[0])), // x
71+
metacall_value_create_array(array_y, sizeof(array_y) / sizeof(array_y[0])), // y
72+
metacall_value_create_str("X"), // xlabel
73+
metacall_value_create_str("Y"), // ylabel
74+
metacall_value_create_str("plot.png") // output
75+
};
76+
77+
void * ret = NULL;
78+
79+
// Load scripts
80+
if (metacall_load_from_file("py", py_scripts, sizeof(py_scripts) / sizeof(py_scripts[0]), NULL) != 0)
81+
{
82+
return cleanup(3);
83+
}
84+
85+
// Call to plot function
86+
ret = metacallv("plot", args);
87+
88+
if (ret == NULL)
89+
{
90+
return cleanup(4);
91+
}
92+
93+
// Clean up arguments
94+
for (size_t it = 0; it < sizeof(args) / sizeof(args[0]); ++it)
95+
{
96+
metacall_value_destroy(args[it]);
97+
}
98+
99+
// Clean up return value
100+
metacall_value_destroy(ret);
101+
}
102+
103+
return cleanup(0);
104+
}

plot.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
3+
import matplotlib.pyplot as plt
4+
5+
def plot(title, x, y, xlabel, ylabel, output):
6+
plt.plot(x, y)
7+
plt.xlabel(xlabel)
8+
plt.ylabel(ylabel)
9+
plt.title(title)
10+
plt.grid(True)
11+
plt.savefig(output)

0 commit comments

Comments
 (0)