Skip to content

Commit

Permalink
username is not hardcode to get jupyter kernelspec path
Browse files Browse the repository at this point in the history
  • Loading branch information
prasunanand committed Nov 24, 2024
1 parent 86f6091 commit e973add
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
7 changes: 1 addition & 6 deletions kernelspec/kernelspec_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,25 +182,20 @@ func findKernelSpecs() map[string]string {

func getKernelDirs() []string {
dirs := core.Zasper.JupyterPath
ipythonDir := getIpythonDir()
dirs = append(dirs, ipythonDir)
kernel_dirs := []string{}
for _, v := range dirs {
kernel_dirs = append(kernel_dirs, filepath.Join(v, "kernels"))
}
return kernel_dirs
}

func getIpythonDir() string {
return "/home/prasun/.ipython"
}

func listKernelsIn(kernelDir string) map[string]string {
dir, err := os.Open(kernelDir)
if err != nil {
log.Println("No kernels found in", kernelDir)
return nil
}
log.Println("kernels found in", kernelDir)
files, err := dir.Readdir(0)
if err != nil {
fmt.Println(err)
Expand Down
2 changes: 1 addition & 1 deletion ui/src/ide/editor/notebook/NotebookEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { w3cwebsocket as W3CWebSocket } from 'websocket'
import { BaseApiUrl } from '../../config'
import { data } from '@remix-run/router/dist/utils'

const debugMode = true
const debugMode = false

interface CodeMirrorRef {
editor: {
Expand Down
51 changes: 47 additions & 4 deletions utils/paths.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package utils

import "os"
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
)

func GetHomeDir() string {
dir, _ := os.Getwd()
Expand All @@ -19,13 +25,50 @@ func GetJupyterRuntimeDir() string {
return ""
}

// getPythonVersion tries to retrieve the installed Python version (e.g., "3.9")
func getPythonVersion() (string, error) {
// Run `python3 --version` to get the Python version
cmd := exec.Command("python3", "--version")
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("could not get python version: %v", err)
}

// Parse the output, e.g., "Python 3.9.7"
versionOutput := string(output)
re := regexp.MustCompile(`Python (\d+\.\d+)`)
matches := re.FindStringSubmatch(versionOutput)
if len(matches) < 2 {
return "", fmt.Errorf("unable to parse Python version")
}

return matches[1], nil
}

// GetJupyterPath returns a list of possible Jupyter paths, making the function user-agnostic
func GetJupyterPath() []string {
// Get the home directory from the environment variable
homeDir := os.Getenv("HOME")
if homeDir == "" {
fmt.Println("HOME environment variable is not set")
}

// Get Python version (e.g., "3.9")
pythonVersion, err := getPythonVersion()
if err != nil {
return nil
}

// Initialize an empty slice to hold the paths
paths := []string{}
paths = append(paths, "/home/prasun/.local/jupyter")

// Add various possible paths, using the home directory dynamically
paths = append(paths, filepath.Join(homeDir, ".local", "jupyter"))
paths = append(paths, "/usr/local/share/jupyter")
paths = append(paths, "/home/prasun/.local/share/jupyter/") // todo : check
paths = append(paths, "/Users/prasunanand/Library/Python/3.9/share/jupyter")
paths = append(paths, filepath.Join(homeDir, ".local", "share", "jupyter"))
paths = append(paths, filepath.Join(homeDir, "Library", "Python", pythonVersion, "share", "jupyter"))

// Return the list of paths
return paths
}

Expand Down

0 comments on commit e973add

Please sign in to comment.