Exemple #1
0
 public void FindWhenThereIsNoInstanceRunning()
 {
     System.Console.WriteLine("Start Test : Find");
     Tasks.ITask JupyterNotebookTask = JupyterNotebookTool.FindTask("HelloWorld.ipynb");
     Assert.False(JupyterNotebookTask.IsAlive());
     Assert.True(JupyterNotebookTask.IsEmpty());
     System.Console.WriteLine("End Test : Find");
 }
        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 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);
        }
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));
        }