https://sqrtminusone.xyz/posts/2021-05-01-org-python/
Config
#
requires emacs package jupyter and ipykernel (pip install ipykernel) some config
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t) ;; Other languages
(shell . t)
(python . t)
(jupyter . t)))
(require 'jupyter)
(require 'ob-jupyter)
(org-babel-jupyter-override-src-block "python")
(setq ob-async-no-async-languages-alist '("python" "jupyter-python"))
(setq org-babel-default-header-args:python '((:async . "yes")
(:kernel . "python3")
(:session . "python-default-session")))
;; to correctly render the colored error messages
(defun display-ansi-colors ()
(ansi-color-apply-on-region (point-min) (point-max)))
(add-hook 'org-babel-after-execute-hook #'display-ansi-colors)
Sample file after config
#
def square(x):
import math
return 2 ** (2 * math.log2(x))
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])
pass
import pandas as pd
pd.DataFrame({"a": [1, 2], "b": [3, 4]})
import time
from datetime import datetime, timedelta
def print_progress(percentage):
if not "last_percentage" in print_progress.__dir__():
print_progress.last_percentage = 1000
print_progress.start_time = None
now = datetime.now()
if percentage < print_progress.last_percentage:
print_progress.start_time = now
print_progress.last_percentage = percentage
if print_progress.start_time == now:
seconds_left = 0
else:
dt = (now-start_time)
seconds_left = ((1-percentage) / (percentage / (dt.seconds + dt.microseconds/1000000)))
seconds_left_td = timedelta(seconds=round(seconds_left))
seconds_left_str = str(seconds_left_td)
max = 50
p = int(percentage * max + 0.5)
num_done = p
num_to_go = max-p-1
print(f"\r|{'#' * num_done}>{'-' * num_to_go}| {round(percentage*100, 2)}% - {seconds_left_str} min left {' '*20}", end="")
for i in range(100):
p = i/99
print_progress(p)
time.sleep(0.4)
|##################################################>| 100.0% - 0:00:00 min left
Language interop
#
| 0.0 |
0.0 |
0.0 |
| 0.0 |
0.0 |
1.0 |
| 0.0 |
1.0 |
0.0 |
| 0.0 |
1.0 |
1.0 |
| 1.0 |
0.0 |
0.0 |
| 1.0 |
0.0 |
1.0 |
| 1.0 |
1.0 |
0.0 |
| 1.0 |
1.0 |
1.0 |
#include <stdio.h>
#include <math.h>
struct Vec3 {
float x;
float y;
float z;
};
float dot(Vec3 a, Vec3 b) {
return
a.x * b.x +
a.y * b.y +
a.z * b.z;
}
Vec3 operator-(Vec3 a, Vec3 b) {
return Vec3{
a.x - b.x,
a.y - b.y,
a.z - b.z
};
}
float length(Vec3 v) {
return sqrt(dot(v,v));
}
Vec3 cross(Vec3 a, Vec3 b) {
return Vec3{
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
};
}
float triangle_area(Vec3 side_a, Vec3 side_b) {
return length(cross(side_a, side_b)) / 2;
}
Vec3 get_ith_vertex(int i) {
return Vec3{
(float)vertex_buffer[i][0],
(float)vertex_buffer[i][1],
(float)vertex_buffer[i][2],
};
}
int main() {
for (int i = 0; i < index_buffer_rows; ++i) {
Vec3 side_a =
get_ith_vertex(index_buffer[i][1]) - get_ith_vertex(index_buffer[i][0]);
Vec3 side_b =
get_ith_vertex(index_buffer[i][2]) - get_ith_vertex(index_buffer[i][0]);
printf("%d\t%f\n", i, triangle_area(side_a, side_b));
}
return 0;
}
| 0 |
0.5 |
| 1 |
0.5 |
| 2 |
0.5 |
| 3 |
0.707107 |
create temporary table temp_table(idx int, area int);
.import $areas temp_table
select min(idx), max(area) from temp_table;