Skip to content
Snippets Groups Projects
Commit 343b37af authored by dgelessus's avatar dgelessus
Browse files

Use jupyter instead of python3 -m jupyter to install the kernel spec

The standard Jupyter command is always called "jupyter" regardless of
Python version, so this way the default is more likely to work. If the
user's Jupyter command is different, they will more likely know its
name than what the underlying Python interpreter is.
parent ea020329
Branches
No related tags found
No related merge requests found
...@@ -76,6 +76,6 @@ task createKernelSpec(type: JavaExec) { ...@@ -76,6 +76,6 @@ task createKernelSpec(type: JavaExec) {
task installKernelSpec(type: Exec) { task installKernelSpec(type: Exec) {
dependsOn = [createKernelSpec] dependsOn = [createKernelSpec]
executable = project.hasProperty("pythonInterpreter") ? project.pythonInterpreter : "python3" executable = project.hasProperty("jupyterCommand") ? project.jupyterCommand : "jupyter"
args = ["-m", "jupyter", "kernelspec", "install", "--sys-prefix", "--name=prob2", KERNEL_SPEC_OUTPUT_PATH.toString()] args = ["kernelspec", "install", "--sys-prefix", "--name=prob2", KERNEL_SPEC_OUTPUT_PATH.toString()]
} }
...@@ -124,11 +124,11 @@ public final class Main { ...@@ -124,11 +124,11 @@ public final class Main {
System.out.println("Kernel spec created"); System.out.println("Kernel spec created");
} }
private static void installKernelSpec(final Path pythonInterpreter, final Path kernelSpecDir) { private static void installKernelSpec(final String jupyterCommand, final Path kernelSpecDir) {
System.out.println("Installing kernel spec..."); System.out.println("Installing kernel spec...");
System.out.println("Python interpreter: " + pythonInterpreter); System.out.println("Jupyter command: " + jupyterCommand);
System.out.println("Kernel spec directory: " + kernelSpecDir); System.out.println("Kernel spec directory: " + kernelSpecDir);
final ProcessBuilder pb = new ProcessBuilder(pythonInterpreter.toString(), "-m", "jupyter", "kernelspec", "install", "--sys-prefix", "--name=prob2", kernelSpecDir.toString()); final ProcessBuilder pb = new ProcessBuilder(jupyterCommand, "kernelspec", "install", "--sys-prefix", "--name=prob2", kernelSpecDir.toString());
pb.redirectInput(ProcessBuilder.Redirect.INHERIT); pb.redirectInput(ProcessBuilder.Redirect.INHERIT);
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT); pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT); pb.redirectError(ProcessBuilder.Redirect.INHERIT);
...@@ -156,14 +156,14 @@ public final class Main { ...@@ -156,14 +156,14 @@ public final class Main {
System.out.println("Kernel spec installed"); System.out.println("Kernel spec installed");
} }
private static void install(final Path pythonInterpreter) { private static void install(final String jupyterCommand) {
final Path jarPath = getJarPath(); final Path jarPath = getJarPath();
final Path destPath = getDestPath(jarPath); final Path destPath = getDestPath(jarPath);
copyJar(jarPath, destPath); copyJar(jarPath, destPath);
try { try {
final Path kernelSpecDir = Files.createTempDirectory("prob2kernelspec"); final Path kernelSpecDir = Files.createTempDirectory("prob2kernelspec");
createKernelSpec(destPath, kernelSpecDir); createKernelSpec(destPath, kernelSpecDir);
installKernelSpec(pythonInterpreter, kernelSpecDir); installKernelSpec(jupyterCommand, kernelSpecDir);
try (final Stream<Path> contents = Files.list(kernelSpecDir)) { try (final Stream<Path> contents = Files.list(kernelSpecDir)) {
contents.forEach(path -> { contents.forEach(path -> {
try { try {
...@@ -199,10 +199,10 @@ public final class Main { ...@@ -199,10 +199,10 @@ public final class Main {
public static void main(final String[] args) throws IOException, InvalidKeyException, NoSuchAlgorithmException { public static void main(final String[] args) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
if (args.length < 1 || args.length == 1 && "--help".equals(args[0])) { if (args.length < 1 || args.length == 1 && "--help".equals(args[0])) {
System.err.println("Usage: java -jar prob2-jupyter-kernel-all.jar [--help | install PYTHONINTERPRETER | createKernelSpec KERNELSPECDIR | run CONNECTIONFILE]"); System.err.println("Usage: java -jar prob2-jupyter-kernel-all.jar [--help | install [JUPYTER] | createKernelSpec KERNELSPECDIR | run CONNECTIONFILE]");
System.err.println("--help: Prints this information."); System.err.println("--help: Prints this information.");
System.err.println("install: Copies the kernel into the ProB home directory, and installs the kernel in the given Python interpreter."); System.err.println("install: Copies the kernel into the ProB home directory, and installs the kernel in Jupyter.");
System.err.println("\tIf you're not sure which Python interpreter is used by your Jupyter installation, open a Python notebook and run \"import sys; print(sys.executable)\"."); System.err.println("\tBy default the command \"jupyter\" is used to install the kernel. If the kernel should be installed in a different Jupyter installation, a different Jupyter command can be passed as an argument to the install command.");
System.err.println("createKernelSpec: Creates a Jupyter kernel spec for this jar file at the given location."); System.err.println("createKernelSpec: Creates a Jupyter kernel spec for this jar file at the given location.");
System.err.println("\tThis option is for advanced users or developers, who don't want the jar file to be copied, or who want to install the kernel spec manually."); System.err.println("\tThis option is for advanced users or developers, who don't want the jar file to be copied, or who want to install the kernel spec manually.");
System.err.println("run: Runs the kernel using the given connection file."); System.err.println("run: Runs the kernel using the given connection file.");
...@@ -211,12 +211,12 @@ public final class Main { ...@@ -211,12 +211,12 @@ public final class Main {
} }
switch (args[0]) { switch (args[0]) {
case "install": case "install":
if (args.length != 2) { if (args.length > 2) {
System.err.println("install expects exactly one argument, not " + (args.length-1)); System.err.println("install expects at most one argument, not " + (args.length-1));
System.err.println("Use --help for more info."); System.err.println("Use --help for more info.");
throw die(2); throw die(2);
} }
install(Paths.get(args[1])); install(args.length > 1 ? args[1] : "jupyter");
break; break;
case "createKernelSpec": case "createKernelSpec":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment