Exemple #1
0
        public bool StopJupyter(string id)
        {
            bool   result       = false;
            string ResourceName = $"{id}.ipynb";
            string ResourceNameWithoutExtension = id;
            string notebookDir  = DirectoryHelper.GetCodeDirectoryPath() + ResourceNameWithoutExtension;
            string ResourcePath = notebookDir + System.IO.Path.DirectorySeparatorChar + ResourceName;
            string Message      = "Error";

            System.Console.WriteLine("Resource Path " + ResourcePath);
            Dictionary <string, string> Result = new Dictionary <string, string>();
            //
            var obj = new
            {
                base_url     = "/",
                NotebookDir  = $"{notebookDir}",
                ResourcePath = $"{ResourcePath}"
            };

            //
            try
            {
                JupyterNotebook JNBTool             = this.jupyterClient.GetJupyterNotebookTool();
                ITask           JupyterNoteBookTask = JNBTool.FindTask(ResourcePath);
                if (JupyterNoteBookTask.IsEmpty())
                {
                    Message = "Error : There is no such task running";
                    // var inst = InstancePayload.Get();
                    // foreach(var item in inst)
                    // {
                    //     if(item.Id == id)
                    //     {
                    //         InstancePayload.Delete(id);
                    //         result = true;
                    //     }
                    // }
                }
                else
                {
                    JNBTool.StopTask(ResourcePath);
                    result = true;
                    InstancePayload.Delete(id);
                    Message = "Notebook successfully stop";
                }
            }
            catch (Exception ex)
            {
                Message = ex.Message;
            }
            // Result.Add("Result", Message);
            return(result);
        }
        public async Task <IActionResult> GetJupyterNotebookUrlAsync(string id)
        {
            string response = string.Empty;
            string _jUrl    = string.Empty;
            string message  = "Notebook is up and running successfully";
            //We needs to populate Current User from request
            string   CURRENT_USER    = string.Empty;
            string   ResourcePath    = Helpers.Common.FilePathHelper.GetFilePathById(id, codeResponse);
            FileInfo resourceInfo    = new System.IO.FileInfo(ResourcePath);
            string   notebookDir     = resourceInfo.Directory.ToString();
            string   notebookLinkURL = string.Empty;

            var obj = new
            {
                base_url     = "/",
                NotebookDir  = $"{notebookDir}",
                ResourcePath = $"{ResourcePath}"
            };

            try
            {
                var             portRegex = new Regex(@"(?<![^/]/[^/]*):\d+");//to remove port number
                JupyterNotebook JNBTool   = this.jupyterClient.GetJupyterNotebookTool();
                await System.Threading.Tasks.Task.FromResult(0);

                ITask JupyterNoteBookTask = JNBTool.FindTask(ResourcePath);
                if (JupyterNoteBookTask.IsEmpty())
                {
                    JNBTool.StartTaskAsync((int)TaskTypes.Start, ResourcePath, (JObject)JObject.FromObject(obj));
                }
                notebookLinkURL = JNBTool.GetResourceLink(ResourcePath);
                // notebookLinkURL = @"C:\myCode\Project\ZMOD\code\1_SVM\1_SVM.ipynb";
                //
                // JupyterNotebook nb = JupyterNotebook.Find(ResourcePath);
                var objJNBInst = new InstanceResponse()
                {
                    Id   = id,
                    Name = $"{id}.ipynb",
                    Type = "JNB"
                           // Properties = new List<InstanceProperty>(){ new InstanceProperty{ key="port", value=nb.Info.Port}}
                };
                InstancePayload.Create(objJNBInst);
                notebookLinkURL = notebookLinkURL.Replace(@"//", @"/");
                //
                return(Ok(new { user = CURRENT_USER, id = id, message = message, url = notebookLinkURL.Replace(notebookLinkURL.Substring(0, notebookLinkURL.IndexOf("jnb")), "") }));
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return(BadRequest(new { user = CURRENT_USER, id = id, message = message }));
            }
        }
Exemple #3
0
        public void StartAllAllowedInstances()
        {
            JupyterNotebookTool = new JupyterNotebook("http://localhost", "/jnb", new int[] { 8888, 8890 });
            System.Console.WriteLine("Start Test : StartAllAllowedInstances");
            var obj = new
            {
                base_url     = "/",
                NotebookDir  = TestDir + System.IO.Path.DirectorySeparatorChar + "HelloWorld.ipynb",
                ResourcePath = JNBFirstFile
            };

            JupyterNotebookTool.StartTaskAsync((int)TaskTypes.Start, JNBFirstFile, (JObject)JObject.FromObject(obj));
            System.Threading.Thread.Sleep(2000);
            Assert.True(JupyterNotebookTool.IsPortBusyInRange(8888, 8888, 8890));

            var obj2 = new
            {
                base_url     = "/",
                NotebookDir  = TestDir + System.IO.Path.DirectorySeparatorChar + "HelloWorld2.ipynb",
                ResourcePath = JNBSecondFile
            };

            JupyterNotebookTool.StartTaskAsync((int)TaskTypes.Start, JNBSecondFile, (JObject)JObject.FromObject(obj2));
            System.Threading.Thread.Sleep(2000);
            Assert.True(JupyterNotebookTool.IsPortBusyInRange(8889, 8888, 8890));

            var obj3 = new
            {
                base_url     = "/",
                NotebookDir  = TestDir + System.IO.Path.DirectorySeparatorChar + "HelloWorld3.ipynb",
                ResourcePath = JNBThirdFile
            };

            JupyterNotebookTool.StartTaskAsync((int)TaskTypes.Start, JNBThirdFile, (JObject)JObject.FromObject(obj3));
            System.Threading.Thread.Sleep(2000);
            Assert.True(JupyterNotebookTool.IsPortBusyInRange(8890, 8888, 8890));
            System.Console.WriteLine("End Test : StartAllAllowedInstances");
        }
Exemple #4
0
        public JsonResult Stop()
        {
            string ResourceName = Request.Query["name"].ToString();
            string ResourceNameWithoutExtension = ResourceName.Substring(0, ResourceName.LastIndexOf("."));
            string notebookDir  = DirectoryHelper.GetCodeDirectoryPath() + ResourceNameWithoutExtension;
            string ResourcePath = notebookDir + System.IO.Path.DirectorySeparatorChar + ResourceName; //http://localhost:5000//uploads/TestUser/code/HelloClass2.ipynb
            string Message      = "Error";

            System.Console.WriteLine("Resource Path " + ResourcePath);
            Dictionary <string, string> Result = new Dictionary <string, string>();
            var obj = new
            {
                base_url     = "/",
                NotebookDir  = $"{notebookDir}",
                ResourcePath = $"{ResourcePath}"
            };

            try
            {
                JupyterNotebook JNBTool             = this.jupyterClient.GetJupyterNotebookTool();
                ITask           JupyterNoteBookTask = JNBTool.FindTask(ResourcePath);
                if (JupyterNoteBookTask.IsEmpty())
                {
                    Message = "Error : There is no such task running";
                }
                else
                {
                    JNBTool.StopTask(ResourcePath);
                    Message = "Notebook successfully stop";
                }
            }
            catch (Exception ex)
            {
                Message = ex.Message;
            }
            Result.Add("Result", Message);
            return(new JsonResult(Result));
        }
 public PyJupyterServiceClient(string HostURL, string RoutePrefix, int[] PortRangeInUse)
 {
     JupyterNotebookTool = new JupyterNotebook(HostURL, RoutePrefix, PortRangeInUse);
 }
 public PyJupyterServiceClient(string HostURL)
 {
     JupyterNotebookTool = new JupyterNotebook(HostURL);
 }