summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTheodore Aptekarev <aptekarev@gmail.com>2021-10-03 17:48:19 +0300
committerTheodore Aptekarev <aptekarev@gmail.com>2021-10-03 17:48:19 +0300
commitfa70c0d7a8b4b15750ea53e03f44118ee3eac0a6 (patch)
treee51b300c4334e16ab483191e4253f651b4a5eba3
parent9ebfd2d6d9b102b9c8752ec6725a193fd4af4055 (diff)
Add wip prototype of the jupyter kernel module
-rw-r--r--jupyterlab/kernel/README.md38
-rw-r--r--jupyterlab/kernel/gamestonk/kernel.json14
-rw-r--r--jupyterlab/kernel/gamestonk_kernel/__init__.py0
-rw-r--r--jupyterlab/kernel/gamestonk_kernel/__main__.py7
-rw-r--r--jupyterlab/kernel/gamestonk_kernel/gamestonk_kernel.py87
-rw-r--r--jupyterlab/kernel/setup.py18
6 files changed, 164 insertions, 0 deletions
diff --git a/jupyterlab/kernel/README.md b/jupyterlab/kernel/README.md
new file mode 100644
index 00000000000..b8834ae063d
--- /dev/null
+++ b/jupyterlab/kernel/README.md
@@ -0,0 +1,38 @@
+# Gamestonk Jupter Kernel
+
+This python module installs a gamestonk kernel into a local python environment.
+The kerlen can be then used to work with Notebooks and Consoles in Jupyter Lab.
+
+## Installation
+
+0. Navigate to this folder
+
+ ```bash
+ cd jupyterlab/kernel
+ ```
+
+1. Install the kernel module with pip:
+
+ ```bash
+ pip install --upgrade .
+ ```
+
+2. Install the kernel into jupyter
+
+ ```bash
+ jupyter-kernelspec install gamestonk
+ ```
+
+## Usage
+
+Open Jupyter Lab and click the G button in the Notebook or the Console launcher.
+
+### Development notes
+
+1. You can install the kernel in development mode by using the `-e` flag
+
+ ```bash
+ pip install -e .
+ ```
+
+2. Nice tutorial on building simple python wrapper extension [here](https://github.com/ipython-books/cookbook-2nd-code/blob/master/chapter01_basic/06_kernel.ipynb)
diff --git a/jupyterlab/kernel/gamestonk/kernel.json b/jupyterlab/kernel/gamestonk/kernel.json
new file mode 100644
index 00000000000..6200ab654f8
--- /dev/null
+++ b/jupyterlab/kernel/gamestonk/kernel.json
@@ -0,0 +1,14 @@
+{
+ "argv":
+ [
+ "python",
+ "-m",
+ "gamestonk_kernel",
+ "--matplotlib=inline",
+ "-f",
+ "{connection_file}"
+ ],
+ "display_name": "Gamestonk",
+ "name": "Gamestonk",
+ "language": "python"
+}
diff --git a/jupyterlab/kernel/gamestonk_kernel/__init__.py b/jupyterlab/kernel/gamestonk_kernel/__init__.py
new file mode 100644
index 00000000000..e69de29bb2d
--- /dev/null
+++ b/jupyterlab/kernel/gamestonk_kernel/__init__.py
diff --git a/jupyterlab/kernel/gamestonk_kernel/__main__.py b/jupyterlab/kernel/gamestonk_kernel/__main__.py
new file mode 100644
index 00000000000..f20cc63c7cc
--- /dev/null
+++ b/jupyterlab/kernel/gamestonk_kernel/__main__.py
@@ -0,0 +1,7 @@
+"""Gamestonk Kernel Module Main magics file."""
+
+if __name__ == "__main__":
+ from ipykernel.kernelapp import IPKernelApp
+ from .gamestonk_kernel import GamestonkKernel
+
+ IPKernelApp.launch_instance(kernel_class=GamestonkKernel)
diff --git a/jupyterlab/kernel/gamestonk_kernel/gamestonk_kernel.py b/jupyterlab/kernel/gamestonk_kernel/gamestonk_kernel.py
new file mode 100644
index 00000000000..39a6b40ae5d
--- /dev/null
+++ b/jupyterlab/kernel/gamestonk_kernel/gamestonk_kernel.py
@@ -0,0 +1,87 @@
+"""Gamestonk Jupyter Kernel."""
+import io
+from contextlib import redirect_stdout
+import matplotlib
+from ipykernel.kernelbase import Kernel
+from terminal import TerminalController, bootup
+
+
+class GamestonkKernel(Kernel):
+ """The Gamestonk Kernel."""
+
+ implementation = "Gamestonk"
+ implementation_version = "0.1"
+ language = "gamestonk" # will be used for
+ # syntax highlighting
+ language_version = "1.0"
+ language_info = {"name": "gamestonk", "mimetype": "text/plain", "extension": ".gst"}
+ banner = "..."
+
+ def __init__(self, **kwargs) -> None:
+ """
+ Override the Kernel init function.
+
+ During initialization we instantiate the Terminal Controller and do some boot up
+ procesdures.
+
+ :param kwargs: Keywords arguments
+ :type kwargs: dict
+ """
+ super().__init__(**kwargs)
+ bootup()
+ self.t_controller = TerminalController()
+
+ f = io.StringIO()
+ with redirect_stdout(f):
+ self.t_controller.print_help()
+ self.banner = f.getvalue()
+
+ matplotlib.use("svg")
+
+ def do_execute(
+ self,
+ code: str,
+ silent: bool = False,
+ store_history: bool = True,
+ user_expressions: dict = None,
+ allow_stdin: bool = False,
+ ):
+ """Execute code in the kernel."""
+ if not silent:
+ f = io.StringIO()
+ with redirect_stdout(f):
+ # Process list of commands selected by user
+ try:
+ process_input = self.t_controller.switch(code)
+ # None - Keep loop
+ # True - Quit or Reset based on flag
+ # False - Keep loop and show help menu
+
+ if process_input is not None and not isinstance(
+ process_input, bool
+ ):
+ # Quit terminal
+ self.t_controller = process_input
+ if process_input is True and isinstance(process_input, bool):
+ self.t_controller = TerminalController()
+ self.t_controller.print_help()
+
+ except Exception as e:
+ self.send_response(
+ self.iopub_socket,
+ "stream",
+ {"name": "stdout", "text": f"{e}\n"},
+ )
+ self.send_response(
+ self.iopub_socket,
+ "stream",
+ {"name": "stdout", "text": f"{f.getvalue()}\n"},
+ )
+
+ # We return the execution results.
+ return {
+ "status": "ok",
+ "execution_count": self.execution_count,
+ "payload": [],
+ "user_expressions": {},
+ }
diff --git a/jupyterlab/kernel/setup.py b/jupyterlab/kernel/setup.py
new file mode 100644
index 00000000000..616c53cf19e
--- /dev/null
+++ b/jupyterlab/kernel/setup.py
@@ -0,0 +1,18 @@
+"""Setup script for the Gamestonk Jupyter Kernel."""
+from setuptools import setup
+
+setup(
+ name="gamestonk_kernel",
+ version="0.0.1",
+ description="Gamestonk Jupyter Kernel",
+ url="https://github.com/GamestonkTerminal/GamestonkTerminal",
+ author="piiq",
+ author_email="",
+ license="MIT",
+ packages=["gamestonk_kernel"],
+ install_requires=[
+ "matplotlib",
+ "numpy",
+ ],
+ classifiers=["Development Status :: 1 - Planning"],
+)