This is an automated email from the ASF dual-hosted git repository.

dimuthuupe pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata.git


The following commit(s) were added to refs/heads/master by this push:
     new c50760df01 Showing image data in cell output
c50760df01 is described below

commit c50760df01e900a68f4fd83d7d11b7cc6e897326
Author: Dimuthu Wannipurage <[email protected]>
AuthorDate: Sat Jul 20 00:25:06 2024 -0400

    Showing image data in cell output
---
 modules/agent-framework/airavata-agent/agent.go    |  47 ++++++-
 .../airavata-agent/jupyter/kernel.py               |   9 +-
 .../airavata-agent/jupyter/magic.ipynb             | 145 +++++++++++++--------
 3 files changed, 139 insertions(+), 62 deletions(-)

diff --git a/modules/agent-framework/airavata-agent/agent.go 
b/modules/agent-framework/airavata-agent/agent.go
index 86696adb28..75088578b8 100644
--- a/modules/agent-framework/airavata-agent/agent.go
+++ b/modules/agent-framework/airavata-agent/agent.go
@@ -12,6 +12,7 @@ import (
        "io/ioutil"
        "encoding/json"
        "bytes"
+       "bufio"
        protos "airavata-agent/protos"
 
        "golang.org/x/crypto/ssh"
@@ -51,12 +52,52 @@ func main() {
        go func() {
                log.Printf("Starting jupyter kernel")
                cmd := exec.Command("python", "/opt/jupyter/kernel.py")
-               _, err := cmd.Output()
+               //cmd := exec.Command("jupyter/venv/bin/python", 
"jupyter/kernel.py")
+               stdout, err := cmd.StdoutPipe()
+
+               if err != nil {
+                       fmt.Println("Error creating StdoutPipe:", err)
+                       return
+               }
+
+               // Get stderr pipe
+               stderr, err := cmd.StderrPipe()
                if err != nil {
-                       log.Printf(err.Error())
-                       close(kernelChannel)
+                       fmt.Println("Error creating StderrPipe:", err)
                        return
                }
+
+               // Start the command
+               if err := cmd.Start(); err != nil {
+                       fmt.Println("Error starting command:", err)
+                       return
+               }
+
+               // Create channels to read from stdout and stderr
+               stdoutScanner := bufio.NewScanner(stdout)
+               stderrScanner := bufio.NewScanner(stderr)
+
+               // Stream stdout
+               go func() {
+                       for stdoutScanner.Scan() {
+                               fmt.Printf("stdout: %s\n", stdoutScanner.Text())
+                       }
+               }()
+
+               // Stream stderr
+               go func() {
+                       for stderrScanner.Scan() {
+                               fmt.Printf("stderr: %s\n", stderrScanner.Text())
+                       }
+               }()
+
+               // Wait for the command to finish
+               if err := cmd.Wait(); err != nil {
+                       fmt.Println("Error waiting for command:", err)
+                       return
+               }
+
+               fmt.Println("Command finished")
        }()
 
        go func() {
diff --git a/modules/agent-framework/airavata-agent/jupyter/kernel.py 
b/modules/agent-framework/airavata-agent/jupyter/kernel.py
index c3f9f19569..5188105732 100644
--- a/modules/agent-framework/airavata-agent/jupyter/kernel.py
+++ b/modules/agent-framework/airavata-agent/jupyter/kernel.py
@@ -20,7 +20,7 @@ def start_kernel():
     global kernel_running
 
     if kernel_running:
-        return
+        return "Kernel already running"
     # Create a new kernel manager
     km = KernelManager(kernel_name='python3')
     km.start_kernel()
@@ -53,6 +53,9 @@ def execute():
     while True:
         try:
             msg = kc.get_iopub_msg(timeout=1)
+            print("------------------")
+            print(msg)
+            print("-================-")
             content = msg["content"]
             parent_header = msg["parent_header"]
 
@@ -62,6 +65,8 @@ def execute():
             if msg["msg_type"] == "stream" and content["name"] == "stdout":
                 print(content["text"])
                 content_text = content_text + content["text"]
+            if msg["msg_type"] == "display_data":
+                return jsonify({'display': content}), 200
             if msg["msg_type"] == "error":
                 return jsonify({'error': content}), 200
             if msg["msg_type"] == "status" and execution_noticed:
@@ -83,7 +88,7 @@ def stop():
     global kernel_running
 
     if not kernel_running:
-        return
+        return "Kernel is not running to shut down"
     
     kc.stop_channels()
     km.shutdown_kernel()
diff --git a/modules/agent-framework/airavata-agent/jupyter/magic.ipynb 
b/modules/agent-framework/airavata-agent/jupyter/magic.ipynb
index 5f80e5782c..c667375f30 100644
--- a/modules/agent-framework/airavata-agent/jupyter/magic.ipynb
+++ b/modules/agent-framework/airavata-agent/jupyter/magic.ipynb
@@ -2,7 +2,7 @@
  "cells": [
   {
    "cell_type": "code",
-   "execution_count": 40,
+   "execution_count": 1,
    "id": "e520a5ea-847a-4237-a0a8-e52deb0b2c0d",
    "metadata": {},
    "outputs": [],
@@ -18,20 +18,23 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 41,
+   "execution_count": 53,
    "id": "c5c591cb-7f0f-47ab-a9fa-4700161439c2",
    "metadata": {},
    "outputs": [],
    "source": [
+    "from IPython.display import display, Image\n",
+    "import base64\n",
+    "\n",
     "@register_cell_magic\n",
     "def run_remote(line, cell):\n",
-    "    url = 'http://localhost:18880/api/v1/agent/executejupyterrequest'\n",
+    "    url = 
'http://scigap02.sciencegateways.iu.edu:18880/api/v1/agent/executejupyterrequest'\n",
     "\n",
     "    data = {\n",
     "        \"sessionId\": \"session1\",\n",
     "        \"keepAlive\": True,\n",
     "        \"code\": cell,\n",
-    "        \"agentId\": \"agent3\"\n",
+    "        \"agentId\": \"agent1991\"\n",
     "    }\n",
     "\n",
     "    json_data = json.dumps(data)    \n",
@@ -43,7 +46,7 @@
     "        print(\"Cell execution failed. Error: \" + error)\n",
     "    if execution_id:\n",
     "        while True:\n",
-    "            url = 
\"http://localhost:18880/api/v1/agent/executejupyterresponse/\"; + 
execution_id\n",
+    "            url = 
\"http://scigap02.sciencegateways.iu.edu:18880/api/v1/agent/executejupyterresponse/\";
 + execution_id\n",
     "            response = requests.get(url, headers={'Accept': 
'application/json'})\n",
     "            json_response = response.json()\n",
     "            #print(json_response)\n",
@@ -56,12 +59,35 @@
     "                    print(result['error']['ename'])\n",
     "                    print(result['error']['evalue'])\n",
     "                    print(result['error']['traceback'])\n",
+    "                elif 'display' in result:\n",
+    "                    display_obj = result['display']\n",
+    "                    if 'data' in display_obj:\n",
+    "                        data_obj = display_obj['data']\n",
+    "                        if 'image/png' in data_obj:\n",
+    "                            image_data = data_obj['image/png']\n",
+    "                            
display(Image(data=base64.b64decode(image_data), format='png'))\n",
     "                break\n",
     "            time.sleep(1)\n",
     "\n",
     "@register_line_magic\n",
     "def init_remote(line):\n",
     "    print(f\"Your input: {line}\")\n",
+    "    pairs = line.split()\n",
+    "\n",
+    "    # Initialize variable to store the cluster value\n",
+    "    cluster_value = None\n",
+    "    \n",
+    "    # Iterate through the pairs to find the cluster value\n",
+    "    for pair in pairs:\n",
+    "        if pair.startswith(\"cluster=\"):\n",
+    "            cluster_value = pair.split(\"=\")[1]\n",
+    "            break\n",
+    "    \n",
+    "    # Print the cluster value\n",
+    "    if cluster_value:\n",
+    "        print(f\"Cluster value: {cluster_value}\")\n",
+    "\n",
+    "    \n",
     "\n",
     "@register_line_magic\n",
     "def terminate_remote(line):\n",
@@ -70,109 +96,114 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
-   "id": "5c43ee08-39fc-4e44-a3f6-9f7ba372d201",
+   "execution_count": 51,
+   "id": "9e969c05-fbf9-4eca-9b83-d1c62429a13b",
    "metadata": {},
    "outputs": [
     {
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "Collecting airavata-jupyter-magic==0.5\n",
-      "  Obtaining dependency information for airavata-jupyter-magic==0.5 from 
https://files.pythonhosted.org/packages/12/1b/f7214be1a74520d1af8f7810ade38b0346f74b8ccf998eb0586a77b4e636/airavata_jupyter_magic-0.5-py3-none-any.whl.metadata\n";,
-      "  Downloading airavata_jupyter_magic-0.5-py3-none-any.whl.metadata (994 
bytes)\n",
-      "Downloading airavata_jupyter_magic-0.5-py3-none-any.whl (7.6 kB)\n",
-      "Installing collected packages: airavata-jupyter-magic\n",
-      "  Attempting uninstall: airavata-jupyter-magic\n",
-      "    Found existing installation: airavata-jupyter-magic 0.4\n",
-      "    Uninstalling airavata-jupyter-magic-0.4:\n",
-      "      Successfully uninstalled airavata-jupyter-magic-0.4\n",
-      "Successfully installed airavata-jupyter-magic-0.5\n",
-      "\n",
-      
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m
 A new release of pip is available: 
\u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> 
\u001b[0m\u001b[32;49m24.1.2\u001b[0m\n",
-      
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m
 To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
+      "Your input: cluster=jetstream cpu=2 memory=2GB\n",
+      "Cluster value: jetstream\n"
      ]
     }
    ],
    "source": [
-    "! pip install airavata-jupyter-magic==0.5"
+    "%init_remote cluster=jetstream cpu=2 memory=2GB queue=shared walltime=60"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
-   "id": "4c04f9ef-161c-4eb5-bd60-78a68ef614ac",
+   "execution_count": 59,
+   "id": "4e60d84a-2a81-4b04-9dcb-dea48c4228b8",
    "metadata": {},
    "outputs": [
     {
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "The airavata_jupyter_magic module is not an IPython extension.\n"
-     ]
-    }
-   ],
-   "source": [
-    "%load_ext airavata_jupyter_magic"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 2,
-   "id": "9e969c05-fbf9-4eca-9b83-d1c62429a13b",
-   "metadata": {},
-   "outputs": [
-    {
-     "name": "stderr",
-     "output_type": "stream",
-     "text": [
-      "UsageError: Line magic function `%init_remote` not found.\n"
+      "10\n",
+      "\n"
      ]
     }
    ],
    "source": [
-    "%init_remote cluster=jetstream cpu=2 memory=2GB"
+    "%%run_remote\n",
+    "# Your code here\n",
+    "a = 10\n",
+    "print(a)"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 48,
-   "id": "4e60d84a-2a81-4b04-9dcb-dea48c4228b8",
+   "execution_count": 60,
+   "id": "ebd549f1-3a5d-47e3-876d-beb91d971ac2",
    "metadata": {},
    "outputs": [
     {
-     "name": "stdout",
-     "output_type": "stream",
-     "text": [
-      "70\n",
-      "\n"
-     ]
+     "data": {
+      "image/png": 
"iVBORw0KGgoAAAANSUhEUgAAAjIAAAHHCAYAAACle7JuAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/TGe4hAAAACXBIWXMAAA9hAAAPYQGoP6dpAABKOElEQVR4nO3dd3QU5eLG8e+mB1JIIIQASQgdUuhwKYoFqSKIYENFr165gAgiIFhBRVRQUEFsgHoVC9VCUVCKAgoIpNBL6IFQk5CQtju/P6L5ESCQhCSzmzyfc3KOOzuz+7wOZB/efXfWYhiGgYiIiIgDcjI7gIiIiEhRqciIiIiIw1KREREREYelIiMiIiIOS0VGREREHJaKjIiIiDgsFRkRERFxWCoyIiIi4rBUZERERMRhqciIlAO1atXi4YcfNuW5x40bh8ViKdXnPHDgABaLhU8//bRUn7cwPv30UywWCwcO
 [...]
+      "text/plain": [
+       "<IPython.core.display.Image object>"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
     }
    ],
    "source": [
     "%%run_remote\n",
-    "# Your code here\n",
-    "a = 10 + a\n",
-    "print(a)"
+    "import matplotlib.pyplot as plt\n",
+    "\n",
+    "# Sample data\n",
+    "x = [1, 2, 3, 4, 5]\n",
+    "y = [2, 3, 5, 7, 11]\n",
+    "\n",
+    "# Create the plot\n",
+    "plt.plot(x, y)\n",
+    "\n",
+    "# Add a title and labels\n",
+    "plt.title('Simple Line Plot')\n",
+    "plt.xlabel('X Axis')\n",
+    "plt.ylabel('Y Axis')\n",
+    "\n",
+    "# Show the plot\n",
+    "plt.show()"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 35,
-   "id": "ebd549f1-3a5d-47e3-876d-beb91d971ac2",
+   "execution_count": 57,
+   "id": "a946bee5-384b-4883-9753-e5d2bf162d39",
    "metadata": {},
    "outputs": [
     {
      "name": "stdout",
      "output_type": "stream",
      "text": [
+      "Requirement already satisfied: matplotlib in 
/usr/local/lib/python3.12/site-packages (3.9.1)\n",
+      "Requirement already satisfied: contourpy>=1.0.1 in 
/usr/local/lib/python3.12/site-packages (from matplotlib) (1.2.1)\n",
+      "Requirement already satisfied: cycler>=0.10 in 
/usr/local/lib/python3.12/site-packages (from matplotlib) (0.12.1)\n",
+      "Requirement already satisfied: fonttools>=4.22.0 in 
/usr/local/lib/python3.12/site-packages (from matplotlib) (4.53.1)\n",
+      "Requirement already satisfied: kiwisolver>=1.3.1 in 
/usr/local/lib/python3.12/site-packages (from matplotlib) (1.4.5)\n",
+      "Requirement already satisfied: numpy>=1.23 in 
/usr/local/lib/python3.12/site-packages (from matplotlib) (2.0.0)\n",
+      "Requirement already satisfied: packaging>=20.0 in 
/usr/local/lib/python3.12/site-packages (from matplotlib) (24.1)\n",
+      "Requirement already satisfied: pillow>=8 in 
/usr/local/lib/python3.12/site-packages (from matplotlib) (10.4.0)\n",
+      "Requirement already satisfied: pyparsing>=2.3.1 in 
/usr/local/lib/python3.12/site-packages (from matplotlib) (3.1.2)\n",
+      "Requirement already satisfied: python-dateutil>=2.7 in 
/usr/local/lib/python3.12/site-packages (from matplotlib) (2.9.0.post0)\n",
+      "Requirement already satisfied: six>=1.5 in 
/usr/local/lib/python3.12/site-packages (from python-dateutil>=2.7->matplotlib) 
(1.16.0)\n",
+      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken 
permissions and conflicting behaviour with the system package manager. It is 
recommended to use a virtual environment instead: 
https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n";,
+      "\u001b[0m\n",
+      
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m
 A new release of pip is available: 
\u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> 
\u001b[0m\u001b[32;49m24.1.2\u001b[0m\n",
+      
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m
 To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
       "\n"
      ]
     }
    ],
    "source": [
     "%%run_remote\n",
-    "b = 20"
+    "!pip3 install matplotlib\n"
    ]
   },
   {

Reply via email to