Skip to content

Add execute external program magic. - #78

Open
frankfliu wants to merge 1 commit into
SpencerPark:masterfrom
frankfliu:exec
Open

Add execute external program magic.#78
frankfliu wants to merge 1 commit into
SpencerPark:masterfrom
frankfliu:exec

Conversation

@frankfliu

Copy link
Copy Markdown
Contributor

Add a magic that allow user to execute external program in jupyter notebook.

Example:
%system ls -la

@frankfliu frankfliu mentioned this pull request Nov 21, 2019
@neomatrix369

neomatrix369 commented Nov 28, 2019

Copy link
Copy Markdown

@frankfliu great work, and someone has used it to create their own artifact and used it in a notebook, see https://twitter.com/theNeomatrix369/status/1199963064679968769

@ThibTrip

ThibTrip commented May 7, 2020

Copy link
Copy Markdown

Hello! I was wondering why this has not been merged.

I tried the code in src/main/java/io/github/spencerpark/ijava/magics/ExecMagics.java from commit 42ea416. It works but the process hangs instead of terminating.

I tried the code below with jdk-14 in Windows 10 with IJava. It converts a jupyter notebook called "learning java.ipynb" in the current directory into a jshell script.

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Scanner;

public class ExecMagics {
    public void exec(List<String> args) throws Exception {
        ProcessBuilder pb = new ProcessBuilder();
        pb.command(args);
        pb.redirectErrorStream(true);
        Process process = pb.start();

        try (Scanner scanner = new Scanner(process.getInputStream(), StandardCharsets.UTF_8)) {
            while (scanner.hasNext()) {
                System.out.println(scanner.nextLine());
            }
        }
    }
}

ExecMagics ex = new ExecMagics();
List<String> cmd = new ArrayList<String>();
cmd.add("cmd");
cmd.add("/k");
cmd.add("jupyter-nbconvert --to script");
cmd.add("./learning_java.ipynb");
ex.exec(cmd);
[NbConvertApp] Converting notebook ./learning_java.ipynb to script
[NbConvertApp] Writing 8396 bytes to .\learning_java.jshell

Note: I am a total newbie in java 🙈

@ThibTrip

ThibTrip commented May 7, 2020

Copy link
Copy Markdown

I am an idiot... I was using the wrong command option. If I use cmd.add("/c") instead of cmd.add("/k") it works (see https://stackoverflow.com/a/515380/10551772)! Still, would be nice to see this merged 👍 !

@firasm

firasm commented Nov 11, 2020

Copy link
Copy Markdown

Agreed with above - would be nice if this was merged in to master (main). are there any blockers?

franceme added a commit to franceme/IJava that referenced this pull request Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants