Jupyter supports over 50 languages (IPython kernels for other languages -for-other-languages)). It's easy to adapt to new languages. (Reference: Making kernels for Jupyter) Here, as an example, let's create a new Jupyter kernel called KeyValue.
Create something that simply manages key / value pairs.
Command | Description |
---|---|
? | Display key list |
Key | Shows the value corresponding to the key |
Key value | Set the value corresponding to the key |
Just put the two files in place.
Create the following JSON file. See Kernel specs for the location. (If you don't have "python3" in "argv", change it to "python".)
keyvalue/kernel.json
"argv": [
"python", "-m", "keyvaluekernel", "-f", "{connection_file}"
],
"display_name": "KeyValue"
}
Create the following python file under the python installation destination.
keyvaluekernel.py
from ipykernel.kernelbase import Kernel
class KeyValue(Kernel):
implementation = 'KeyValue'
implementation_version = '0.1'
language = 'no-op'
language_version = '0.1'
language_info = {'name': 'KeyValue', 'mimetype': 'text/plain'}
banner = 'Dictionry of Key, Value'
_d = {}
def do_execute(self, code, silent, store_history=True,
user_expressions=None, allow_stdin=False):
s = code.strip()
if not silent:
if s.startswith('?'):
c = {'name': 'stdout', 'text': ' '.join(KeyValue._d.keys())}
else:
ss = s.split(maxsplit=1)
if len(ss) == 1:
if s in KeyValue._d:
c = {'name': 'stdout', 'text': KeyValue._d[s]}
else:
c = {'name': 'stderr', 'text': 'Not found'}
else:
KeyValue._d[ss[0]] = ss[1]
c = {'name': 'stdout', 'text': ss[1]}
self.send_response(self.iopub_socket, 'stream', c)
return {'status': 'ok',
'execution_count': self.execution_count,
'payload': [],
'user_expressions': {},
}
def do_complete(self, code, cursor_pos):
s = code[:cursor_pos]
return {'status': 'ok', 'matches': [k for k in KeyValue._d if k.startswith(s)],
'cursor_start': cursor_pos, 'cursor_end': -1, 'metadata': {}}
if __name__ == '__main__':
from ipykernel.kernelapp import IPKernelApp
IPKernelApp.launch_instance(kernel_class=KeyValue)
Alternatively, place it in any location and specify the location in PYTHONPATH
of the ʻenv` key of the JSON file.
keyvalue/kernel.json
{
"argv": [
"python", "-m", "keyvaluekernel", "-f", "{connection_file}"
],
"display_name": "KeyValue",
"env": {
"PYTHONPATH": "/path/to/your/modules"
}
}
Calculates and returns the result of executing with do_execute. do_complete returns what to complete when the tab is pressed.
OS | Example of creation location |
---|---|
Windows | C:\Anaconda3\Lib\site-packages |
Ubuntu | /usr/local/lib/python3.4/dist-packages |
--Start with "jupyter notebook" and select [Key Value] from [New]. -Enter "key1 value1" and execute (Shift + Enter). --If you enter "key1" and execute it, "value1" will be displayed. -Enter "key2 value2" and execute. -If you execute "?", A list of keys will appear. -Enter up to "k" and press [Tab] to get candidates.
I have prepared a docker image (tsutomu7 / keyvalue) for easy confirmation. You can check as follows.
ubuntu
docker run -it --rm tsutomu7/keyvalue
After executing the above, open "http: // host IP address: 8888" (for example, "http://172.17.0.2:8888").
Recommended Posts