Skip to content

Commit 063910e

Browse files
initial commit
0 parents  commit 063910e

File tree

10 files changed

+1122
-0
lines changed

10 files changed

+1122
-0
lines changed

.gitignore

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
#.idea/

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Installation Instruction for Jaclang Jupyter Kernel
2+
3+
- Step 1: Open the Terminal and go to `jaclang/support/`
4+
- Step 2: Run the command `jupyter kernelspec install --replace --user jupyter_kernel`
5+
- Step 3: Start the Jupyter Notebook; If you already installed you have to refresh the server.
6+
- Step 4: Open a new notebook and select the kernel as the `Jac`,
7+
8+
![Alt Text](start_image.png)

examples/simple_example.ipynb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "c37e3f66",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"\"\"\" This is a Hello World Program \"\"\"\n",
11+
"\n",
12+
"with entry{\n",
13+
" print(\"Hello world\");\n",
14+
"}"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": null,
20+
"id": "87b5906e",
21+
"metadata": {},
22+
"outputs": [],
23+
"source": []
24+
}
25+
],
26+
"metadata": {
27+
"kernelspec": {
28+
"display_name": "Jac",
29+
"language": "",
30+
"name": "jackernel"
31+
}
32+
},
33+
"nbformat": 4,
34+
"nbformat_minor": 5
35+
}

jackernel/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Init file for jackernel."""

jackernel/kernel.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"argv": [
3+
"python",
4+
"-m",
5+
"jackernel.kernel",
6+
"-f",
7+
"{connection_file}"
8+
],
9+
"display_name": "Jac",
10+
"language_info": {
11+
"name": "python",
12+
"mimetype": "text/plain",
13+
"pygments_lexer": "jac_lexer",
14+
"file_extension": ".jac"
15+
}
16+
}

jackernel/kernel.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
"""
2+
Jac Kernel for Jupyter.
3+
4+
This module provides a Jupyter kernel for the Jac programming language. It allows you to execute Jac code
5+
in Jupyter notebooks and captures and displays the output.
6+
7+
Version: 1.0.0
8+
"""
9+
10+
import contextlib
11+
import os
12+
import os.path as op
13+
import tempfile
14+
from io import StringIO
15+
16+
from ipykernel.kernelapp import IPKernelApp
17+
from ipykernel.kernelbase import Kernel
18+
19+
from jaclang import jac_blue_import as jac_import
20+
from jackernel.syntax_hilighter import JacLexer
21+
22+
from pygments import lexers
23+
24+
# jac_lexer = lexers.load_lexer_from_file("syntax_hilighter.py", "JacLexer")
25+
26+
jac_lexer = JacLexer()
27+
28+
29+
def exec_jac(code: str) -> str:
30+
"""Compile, jac code, and execute and return the output."""
31+
with tempfile.TemporaryDirectory() as tmpdir:
32+
source_path = op.join(tmpdir, "temp.jac")
33+
34+
# Write the code to the jac file.
35+
with open(source_path, "w") as f:
36+
f.write(code)
37+
38+
try:
39+
jac_import(op.join(tmpdir, "temp"))
40+
# Import the jac file, this generates the __jac_gen__ folder at the same level as the jac file,
41+
# This folder contains the python file that we want to execute.
42+
script_path = op.join(tmpdir, "__jac_gen__/temp.py")
43+
44+
with open(script_path, "r") as script_file:
45+
script_code = script_file.read()
46+
stdout_capture = (
47+
StringIO()
48+
) # You need to import StringIO from io module
49+
50+
with contextlib.redirect_stdout(stdout_capture):
51+
exec(script_code)
52+
53+
captured_output = stdout_capture.getvalue()
54+
55+
except Exception as e:
56+
captured_output = "Exception: " + str(e)
57+
return captured_output
58+
59+
finally:
60+
pass
61+
62+
63+
class JacKernel(Kernel):
64+
"""Jac wrapper kernel."""
65+
66+
implementation = "jac"
67+
implementation_version = "0.0"
68+
language = "python" # For syntax hilighting
69+
language_version = "1.0"
70+
language_info = {
71+
"name": "python",
72+
"mimetype": "text/plain",
73+
"pygments_lexer": "jac_lexer",
74+
"file_extension": ".jac",
75+
}
76+
77+
banner = "Jac kernel for Jupyter (main), version 1.0.0a4\n\n"
78+
79+
def do_execute(
80+
self,
81+
code: str,
82+
silent: bool,
83+
store_history: bool = True,
84+
user_expressions: dict = None,
85+
allow_stdin: bool = False,
86+
stop_on_error: bool = False,
87+
) -> dict:
88+
"""Execute the code and return the result."""
89+
if not silent:
90+
try:
91+
output = exec_jac(code)
92+
stream_content = {"name": "stdout", "text": output}
93+
self.send_response(self.iopub_socket, "stream", stream_content)
94+
95+
except Exception as e:
96+
# Send the exception as an error message to the frontend.
97+
error_content = {
98+
"ename": type(e).__name__,
99+
"evalue": str(e),
100+
"traceback": [],
101+
}
102+
self.send_response(self.iopub_socket, "error", error_content)
103+
104+
return {
105+
"status": "error",
106+
"execution_count": self.execution_count,
107+
}
108+
109+
finally:
110+
pass
111+
112+
# Return the execution result.
113+
return {
114+
"status": "ok",
115+
"execution_count": self.execution_count,
116+
"payload": [],
117+
"user_expressions": {},
118+
}
119+
120+
121+
if __name__ == "__main__":
122+
IPKernelApp.launch_instance(kernel_class=JacKernel)

jackernel/resources/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
_ICON_PATH = (
2+
"https://www.jaseci.org/wp-content/uploads/2022/02/jaseki-logo-inverted-rgb.svg"
3+
)

0 commit comments

Comments
 (0)