-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert initialization scripts to static imports from the kernel runt…
…ime. See #55.
- Loading branch information
1 parent
8e7b6b6
commit fe14b73
Showing
9 changed files
with
162 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/io/github/spencerpark/ijava/runtime/Display.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package io.github.spencerpark.ijava.runtime; | ||
|
||
import io.github.spencerpark.ijava.JavaKernel; | ||
import io.github.spencerpark.jupyter.kernel.display.DisplayData; | ||
|
||
import java.util.UUID; | ||
|
||
public class Display { | ||
public static DisplayData render(Object o) { | ||
JavaKernel kernel = Kernel.getKernelInstance(); | ||
|
||
if (kernel != null) { | ||
return kernel.getRenderer().render(o); | ||
} else { | ||
throw new RuntimeException("No IJava kernel running"); | ||
} | ||
} | ||
|
||
public static DisplayData render(Object o, String... as) { | ||
JavaKernel kernel = Kernel.getKernelInstance(); | ||
|
||
if (kernel != null) { | ||
return kernel.getRenderer().renderAs(o, as); | ||
} else { | ||
throw new RuntimeException("No IJava kernel running"); | ||
} | ||
} | ||
|
||
public static String display(Object o) { | ||
JavaKernel kernel = Kernel.getKernelInstance(); | ||
|
||
if (kernel != null) { | ||
DisplayData data = kernel.getRenderer().render(o); | ||
|
||
String id = data.getDisplayId(); | ||
if (id == null) { | ||
id = UUID.randomUUID().toString(); | ||
data.setDisplayId(id); | ||
} | ||
|
||
kernel.display(data); | ||
|
||
return id; | ||
} else { | ||
throw new RuntimeException("No IJava kernel running"); | ||
} | ||
} | ||
|
||
public static String display(Object o, String... as) { | ||
JavaKernel kernel = Kernel.getKernelInstance(); | ||
|
||
if (kernel != null) { | ||
DisplayData data = kernel.getRenderer().renderAs(o, as); | ||
|
||
String id = data.getDisplayId(); | ||
if (id == null) { | ||
id = UUID.randomUUID().toString(); | ||
data.setDisplayId(id); | ||
} | ||
|
||
kernel.display(data); | ||
|
||
return id; | ||
} else { | ||
throw new RuntimeException("No IJava kernel running"); | ||
} | ||
} | ||
|
||
public static void updateDisplay(String id, Object o) { | ||
JavaKernel kernel = Kernel.getKernelInstance(); | ||
|
||
if (kernel != null) { | ||
DisplayData data = kernel.getRenderer().render(o); | ||
kernel.getIO().display.updateDisplay(id, data); | ||
} else { | ||
throw new RuntimeException("No IJava kernel running"); | ||
} | ||
} | ||
|
||
public static void updateDisplay(String id, Object o, String... as) { | ||
JavaKernel kernel = Kernel.getKernelInstance(); | ||
|
||
if (kernel != null) { | ||
DisplayData data = kernel.getRenderer().renderAs(o, as); | ||
kernel.getIO().display.updateDisplay(id, data); | ||
} else { | ||
throw new RuntimeException("No IJava kernel running"); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/io/github/spencerpark/ijava/runtime/Kernel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.github.spencerpark.ijava.runtime; | ||
|
||
import io.github.spencerpark.ijava.IJava; | ||
import io.github.spencerpark.ijava.JavaKernel; | ||
|
||
public class Kernel { | ||
public static JavaKernel getKernelInstance() { | ||
return IJava.getKernelInstance(); | ||
} | ||
|
||
public static Object eval(String expr) throws Exception { | ||
JavaKernel kernel = getKernelInstance(); | ||
|
||
if (kernel != null) { | ||
return kernel.evalRaw(expr); | ||
} else { | ||
throw new RuntimeException("No IJava kernel running"); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/io/github/spencerpark/ijava/runtime/Magics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.github.spencerpark.ijava.runtime; | ||
|
||
import io.github.spencerpark.ijava.IJava; | ||
import io.github.spencerpark.ijava.JavaKernel; | ||
import io.github.spencerpark.jupyter.kernel.magic.registry.UndefinedMagicException; | ||
|
||
import java.util.List; | ||
|
||
public class Magics { | ||
public static <T> T lineMagic(String name, List<String> args) { | ||
JavaKernel kernel = IJava.getKernelInstance(); | ||
|
||
if (kernel != null) { | ||
try { | ||
return kernel.getMagics().applyLineMagic(name, args); | ||
} catch (UndefinedMagicException e) { | ||
throw e; | ||
} catch (Exception e) { | ||
throw new RuntimeException(String.format("Exception occurred while running line magic '%s': %s", name, e.getMessage()), e); | ||
} | ||
} else { | ||
throw new RuntimeException("No IJava kernel running"); | ||
} | ||
} | ||
|
||
public static <T> T cellMagic(String name, List<String> args, String body) { | ||
JavaKernel kernel = IJava.getKernelInstance(); | ||
|
||
if (kernel != null) { | ||
try { | ||
return kernel.getMagics().applyCellMagic(name, args, body); | ||
} catch (UndefinedMagicException e) { | ||
throw e; | ||
} catch (Exception e) { | ||
throw new RuntimeException(String.format("Exception occurred while running cell magic '%s': %s", name, e.getMessage()), e); | ||
} | ||
} else { | ||
throw new RuntimeException("No IJava kernel running"); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.