Skip to content

Commit b1567d8

Browse files
Implement Jupyter kernel
1 parent 065a58a commit b1567d8

File tree

11 files changed

+937
-10
lines changed

11 files changed

+937
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ src/libasr/wasm_visitor.h
6161
src/libasr/pass/intrinsic_function_registry_util.h
6262
src/libasr/config.h
6363
share/jupyter/kernels/fortran/kernel.json
64+
share/jupyter/kernels/lpython/kernel.json
6465
src/runtime/*.o.empty.c
6566
python_ast.py
6667
python_ast.h

CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,14 @@ endif()
208208
# XEUS (Fortran kernel)
209209
set(WITH_XEUS no CACHE BOOL "Build with XEUS support")
210210
if (WITH_XEUS)
211-
find_package(xeus 0.24.1 REQUIRED)
211+
find_package(xeus 5.1.0 REQUIRED)
212+
find_package(xeus-zmq 3.0.0 REQUIRED)
212213
set(HAVE_LFORTRAN_XEUS yes)
213214

214215
# Generate kernel.json with correct paths
215216
configure_file (
216-
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/fortran/kernel.json.in"
217-
"${CMAKE_CURRENT_BINARY_DIR}/share/jupyter/kernels/fortran/kernel.json"
217+
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/lpython/kernel.json.in"
218+
"${CMAKE_CURRENT_BINARY_DIR}/share/jupyter/kernels/lpython/kernel.json"
218219
)
219220

220221
# Configuration and data directories for Jupyter and LFortran

doc/src/developers_example.ipynb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "c86338ac-53ca-4115-8c5a-8bf8a5c7113e",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"%%showast\n",
11+
"def add(x: i32, y: i32) -> i32:\n",
12+
" return x + y"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"id": "23834b08-2f3f-45e7-a1ce-21a9fd4e5117",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"%%showasr\n",
23+
"def add(x: i32, y: i32) -> i32:\n",
24+
" return x + y"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"id": "ec7426b4-e2e5-416c-bcae-9bb9c8926c9b",
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"%%showllvm\n",
35+
"def sub(x: i32, y: i32) -> i32:\n",
36+
" return add(x, -y)"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"id": "716c56ef-8210-4daf-aa23-96b385801014",
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"%%showasm\n",
47+
"def mul(x: i32, y: i32) -> i32:\n",
48+
" return x * y"
49+
]
50+
}
51+
],
52+
"metadata": {
53+
"kernelspec": {
54+
"display_name": "LPython",
55+
"language": "python",
56+
"name": "lpython"
57+
},
58+
"language_info": {
59+
"file_extension": ".f90",
60+
"mimetype": "text/x-python",
61+
"name": "python",
62+
"version": "2018"
63+
}
64+
},
65+
"nbformat": 4,
66+
"nbformat_minor": 5
67+
}

examples/example_notebook.ipynb

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "e87300c2-64ed-4636-8448-591f36faba29",
7+
"metadata": {},
8+
"outputs": [
9+
{
10+
"name": "stdout",
11+
"output_type": "stream",
12+
"text": [
13+
"Hello, LPython\n"
14+
]
15+
}
16+
],
17+
"source": [
18+
"print(\"Hello, LPython\")"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 2,
24+
"id": "dfcac851-7b49-4065-8c64-4a31658249f7",
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"def add(x: i32, y: i32) -> i32:\n",
29+
" return x + y"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 3,
35+
"id": "09213386-84d5-4e7c-83ba-c3b027f765dd",
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"def sub(x: i32, y: i32) -> i32:\n",
40+
" return x - y"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 4,
46+
"id": "a4b49fd3-bf17-4287-9d5e-60f14ebc9a0f",
47+
"metadata": {},
48+
"outputs": [
49+
{
50+
"data": {
51+
"text/plain": [
52+
"5"
53+
]
54+
},
55+
"execution_count": 4,
56+
"metadata": {},
57+
"output_type": "execute_result"
58+
}
59+
],
60+
"source": [
61+
"add(2, 3)"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": 5,
67+
"id": "d6f4961f-7f0c-45a6-9bf8-e549e97098b0",
68+
"metadata": {},
69+
"outputs": [
70+
{
71+
"data": {
72+
"text/plain": [
73+
"-1"
74+
]
75+
},
76+
"execution_count": 5,
77+
"metadata": {},
78+
"output_type": "execute_result"
79+
}
80+
],
81+
"source": [
82+
"sub(2, 3)"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": 6,
88+
"id": "398fd4be-d7cc-4912-8aa1-880aa58b37ab",
89+
"metadata": {},
90+
"outputs": [],
91+
"source": [
92+
"@dataclass\n",
93+
"class MyClass:\n",
94+
" x: i32\n",
95+
" y: f64\n",
96+
" z: str"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": 7,
102+
"id": "628f0b7d-09a6-49de-a0e6-2f6c664f2ba2",
103+
"metadata": {},
104+
"outputs": [
105+
{
106+
"name": "stdout",
107+
"output_type": "stream",
108+
"text": [
109+
"12 2.45000000000000000e+01 LPython\n"
110+
]
111+
}
112+
],
113+
"source": [
114+
"x: MyClass = MyClass(12, 24.5, \"LPython\")\n",
115+
"print(x)"
116+
]
117+
}
118+
],
119+
"metadata": {
120+
"kernelspec": {
121+
"display_name": "LPython",
122+
"language": "python",
123+
"name": "lpython"
124+
},
125+
"language_info": {
126+
"file_extension": ".f90",
127+
"mimetype": "text/x-python",
128+
"name": "python",
129+
"version": "2018"
130+
}
131+
},
132+
"nbformat": 4,
133+
"nbformat_minor": 5
134+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"display_name": "LPython",
3+
"argv": [
4+
"@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_BINDIR@/lpython",
5+
"kernel",
6+
"-f",
7+
"{connection_file}"
8+
],
9+
"language": "python"
10+
}

src/bin/lpython.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <libasr/config.h>
3030
#include <libasr/string_utils.h>
3131
#include <libasr/lsp_interface.h>
32+
#include <lpython/python_kernel.h>
3233
#include <lpython/utils.h>
3334
#include <lpython/python_serialization.h>
3435
#include <lpython/parser/tokenizer.h>
@@ -2012,8 +2013,12 @@ int main(int argc, char *argv[])
20122013
// }
20132014

20142015
if (kernel) {
2015-
std::cerr << "The kernel subcommand is not implemented yet for LPython." << std::endl;
2016-
return 1;
2016+
#ifdef HAVE_LFORTRAN_XEUS
2017+
return LCompilers::LPython::run_kernel(arg_kernel_f);
2018+
#else
2019+
std::cerr << "The kernel subcommand requires LFortran to be compiled with XEUS support. Recompile with `WITH_XEUS=yes`." << std::endl;
2020+
return 1;
2021+
#endif
20172022
}
20182023

20192024
// if (mod) {

src/lpython/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ endif()
2222

2323
if (WITH_XEUS)
2424
set(SRC ${SRC}
25-
# fortran_kernel.cpp
25+
python_kernel.cpp
2626
)
2727
endif()
2828
add_library(lpython_lib ${SRC})
@@ -35,7 +35,7 @@ endif()
3535
target_include_directories(lpython_lib BEFORE PUBLIC ${lpython_SOURCE_DIR}/src)
3636
target_include_directories(lpython_lib BEFORE PUBLIC ${lpython_BINARY_DIR}/src)
3737
if (WITH_XEUS)
38-
target_link_libraries(lpython_lib xeus)
38+
target_link_libraries(lpython_lib xeus xeus-zmq)
3939
endif()
4040
if (WITH_BFD)
4141
target_link_libraries(lpython_lib p::bfd)

0 commit comments

Comments
 (0)