|
| 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) |
0 commit comments