Skip to content

Commit 7f456fe

Browse files
committed
Fix Jupyter notebook import.
1 parent 0242f16 commit 7f456fe

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

client_server/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ set(CADABRA_SERVER_SRC
3030
Snoop.cc
3131
${CADABRA_CORE_DIR}/InstallPrefix.cc
3232
${CADABRA_CORE_DIR}/DataCell.cc
33+
${CADABRA_CORE_DIR}/Exceptions.cc
3334
${CADABRA_CORE_DIR}/CdbPython.cc
3435
${CADABRA_CORE_DIR}/Stopwatch.cc
3536
${CADABRA_LIBS_DIR}/whereami/whereami.c
@@ -42,6 +43,7 @@ set(CADABRA_CLIENT_SRC
4243
Actions.cc
4344
Snoop.cc
4445
${CADABRA_CORE_DIR}/DataCell.cc
46+
${CADABRA_CORE_DIR}/Exceptions.cc
4547
${CADABRA_CORE_DIR}/InstallPrefix.cc
4648
${CADABRA_LIBS_DIR}/whereami/whereami.c
4749
)

core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ add_executable(cadabra2python
319319
CdbPython.cc
320320
InstallPrefix.cc
321321
DataCell.cc
322+
Exceptions.cc
322323
${CADABRA_LIBS_DIR}/whereami/whereami.c
323324
${CADABRA_LIBS_DIR}/base64/base64.cc
324325
)
@@ -334,6 +335,7 @@ add_executable(cadabra2ipynb
334335
CdbPython.cc
335336
InstallPrefix.cc
336337
DataCell.cc
338+
Exceptions.cc
337339
${CADABRA_LIBS_DIR}/whereami/whereami.c
338340
${CADABRA_LIBS_DIR}/base64/base64.cc
339341
)
@@ -348,6 +350,7 @@ add_executable(cadabra2cadabra
348350
cadabra2cadabra.cc
349351
DataCell.cc
350352
InstallPrefix.cc
353+
Exceptions.cc
351354
${CADABRA_LIBS_DIR}/whereami/whereami.c
352355
${CADABRA_LIBS_DIR}/base64/base64.cc
353356
)

core/DataCell.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "InstallPrefix.hh"
33
#include "Config.hh"
44
#include "DataCell.hh"
5+
#include "Exceptions.hh"
56
#include <sstream>
67
#include <fstream>
78
#include <regex>
@@ -687,11 +688,11 @@ void cadabra::python_recurse(const DTree& doc, DTree::iterator it, std::ostrings
687688

688689
nlohmann::json cadabra::ipynb2cnb(const nlohmann::json& root)
689690
{
690-
auto nbf = root["nbformat"];
691+
int nbf = root.value("nbformat", 0);
691692
nlohmann::json json;
692693

693-
if(!nbf)
694-
return json;
694+
if(nbf==0)
695+
throw RuntimeException("Not a Jupyter notebook.");
695696

696697
json["description"]="Cadabra JSON notebook format";
697698
json["version"]=1.0;

tests/ipynb_module.ipynb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 2,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"text/markdown": [
11+
"${}A_{m n p}$"
12+
]
13+
},
14+
"metadata": {},
15+
"output_type": "display_data"
16+
}
17+
],
18+
"source": [
19+
"ex:=A_{m n p};"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": null,
25+
"metadata": {},
26+
"outputs": [],
27+
"source": []
28+
}
29+
],
30+
"metadata": {
31+
"kernelspec": {
32+
"display_name": "Cadabra2",
33+
"language": "python",
34+
"name": "cadabra2"
35+
},
36+
"language_info": {
37+
"codemirror_mode": "cadabra",
38+
"file_extension": ".ipynb",
39+
"mimetype": "text/cadabra",
40+
"name": "cadabra2",
41+
"pygments_lexer": "cadabra"
42+
}
43+
},
44+
"nbformat": 4,
45+
"nbformat_minor": 4
46+
}

tests/modules.cdb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ def test03():
3636
test03()
3737

3838

39+
from ipynb_module import ex as ex5
40+
41+
def test04():
42+
assert(ex5==$A_{m n p}$)
43+
print("Test 04 passed")
44+
45+
test04()
46+
3947

4048
# import module02
4149
#

0 commit comments

Comments
 (0)