Added basic Jupyter Note support

This commit is contained in:
2020-09-24 20:16:04 +02:00
parent eeeed0f110
commit 8fefce1628
10 changed files with 84 additions and 1 deletions
+2
View File
@@ -115,6 +115,7 @@ class Sheerka(Concept):
self.last_executions = []
self.last_return_values = []
self.execution_count = 0
@property
def resolved_concepts_by_first_keyword(self):
@@ -436,6 +437,7 @@ class Sheerka(Concept):
# with open(CONCEPTS_FILE, "a") as f:
# f.write(text + "\n")
self.execution_count += 1
self._last_execution = execution_context
if len(self.last_executions) == self.MAX_EXECUTION_HISTORY:
del self.last_executions[0]
+53
View File
@@ -0,0 +1,53 @@
from core.sheerka.Sheerka import Sheerka
from ipykernel.ipkernel import IPythonKernel
from ipykernel.kernelapp import IPKernelApp
from sheerkapickle import encode
class SheerkaKernel(IPythonKernel):
implementation = 'Sheerka'
implementation_version = '0.1'
language = 'sheerka'
language_version = '0.1'
language_info = {
'name': 'Any text',
'mimetype': 'text/plain',
'file_extension': '.txt',
}
banner = "Sheerka kernel"
sheerka = Sheerka()
sheerka.initialize()
def __init__(self, **kwargs):
print(f"SheerkaKernel args: {kwargs}")
super().__init__(**kwargs)
def do_execute(self, code, silent, store_history=True, user_expressions=None, allow_stdin=False):
result = self.sheerka.evaluate_user_input(code)
if not silent:
display_data_content = {
'data': {
'text/plain': str(result),
'application/json': encode(self.sheerka, result)
},
'metadata': {
'application/json': {'expanded': True}
},
'execution_count': self.sheerka.execution_count,
}
stream_content = {'name': 'stdout', 'text': display_data_content}
# self.send_response(self.iopub_socket, "stream", stream_content)
self.send_response(self.iopub_socket, "execute_result", display_data_content)
return {'status': 'ok',
# The base class increments the execution count
'execution_count': self.sheerka.execution_count,
'payload': [],
'user_expressions': {}}
if __name__ == '__main__':
IPKernelApp.launch_instance(kernel_class=SheerkaKernel)
View File
+18
View File
@@ -0,0 +1,18 @@
To make Jupyter notebook recognize Sheerka,
the file kernel.json must be copied where the kernels are declared
* First, open the kernel.json file and make sure that the path to SheerkaKernel is correct.
* The copy it to the correct location
* You can use the command 'jupyter kernelspec install </path/to/kernel>'
* or simply copy it using cp ;-)
Valid locations for kernel.json are
| | Unix | Windows |
|----|------| ---------------|
|System | /usr/share/jupyter/kernels<br> /usr/local/share/jupyter/kernels | %PROGRAMDATA%\jupyter\kernels |
|Env |{sys.prefix}/share/jupyter/kernels | |
| User | ~/.local/share/jupyter/kernels (Linux)<br> ~/Library/Jupyter/kernels (Mac) | %APPDATA%\jupyter\kernels |
from https://jupyter-client.readthedocs.io/en/latest/kernels.html#kernel-specs
+3
View File
@@ -0,0 +1,3 @@
{"argv":["python", "/home/kodjo/Dev/Sheerka/src/jupyter/SheerkaKernel.py", "-f", "{connection_file}"],
"display_name":"Sheerka"
}
+1 -1
View File
@@ -16,7 +16,7 @@ class UnexpectedEndOfFileError(ErrorNode):
class BnfParser(BaseParser):
"""
Parser used to transform litteral into ParsingExpression
Parser used to transform literal into ParsingExpression
example :
a | b, c -> Sequence(OrderedChoice(a, b) ,c)