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
+1
View File
@@ -2,6 +2,7 @@ venv
.pytest_cache .pytest_cache
.idea .idea
.vscode .vscode
.ipynb_checkpoints
__pycache__ __pycache__
build build
_build _build
+2
View File
@@ -12,6 +12,8 @@ clean:
rm -rf docs/source/_build rm -rf docs/source/_build
rm -rf prof rm -rf prof
rm -rf tests/prof rm -rf tests/prof
rm -rf Untitled*.ipynb
find . -name '.pytest_cache' -exec rm -rf {} + find . -name '.pytest_cache' -exec rm -rf {} +
find . -name '__pycache__' -exec rm -rf {} + find . -name '__pycache__' -exec rm -rf {} +
find . -name 'debug.txt' -exec rm -rf {} + find . -name 'debug.txt' -exec rm -rf {} +
find . -name 'debug.txt' -exec rm -rf {} +
+1
View File
@@ -18,6 +18,7 @@ def main(argv):
loggers = set() loggers = set()
try: try:
print(f"main arguments {argv=}")
opts, args = getopt.getopt(argv, "hdl:i", ["help", "debug", "logger=", "interactive"]) opts, args = getopt.getopt(argv, "hdl:i", ["help", "debug", "logger=", "interactive"])
for o, a in opts: for o, a in opts:
+3
View File
@@ -18,3 +18,6 @@ pytest-profiling==1.7.0
pytz==2019.3 pytz==2019.3
six==1.13.0 six==1.13.0
wcwidth==0.1.7 wcwidth==0.1.7
ipykernel~=5.3.4
setuptools~=41.6.0
+2
View File
@@ -115,6 +115,7 @@ class Sheerka(Concept):
self.last_executions = [] self.last_executions = []
self.last_return_values = [] self.last_return_values = []
self.execution_count = 0
@property @property
def resolved_concepts_by_first_keyword(self): def resolved_concepts_by_first_keyword(self):
@@ -436,6 +437,7 @@ class Sheerka(Concept):
# with open(CONCEPTS_FILE, "a") as f: # with open(CONCEPTS_FILE, "a") as f:
# f.write(text + "\n") # f.write(text + "\n")
self.execution_count += 1
self._last_execution = execution_context self._last_execution = execution_context
if len(self.last_executions) == self.MAX_EXECUTION_HISTORY: if len(self.last_executions) == self.MAX_EXECUTION_HISTORY:
del self.last_executions[0] 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): class BnfParser(BaseParser):
""" """
Parser used to transform litteral into ParsingExpression Parser used to transform literal into ParsingExpression
example : example :
a | b, c -> Sequence(OrderedChoice(a, b) ,c) a | b, c -> Sequence(OrderedChoice(a, b) ,c)