Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add execute external program magic. #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

frankfliu
Copy link
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
Copy link

neomatrix369 commented Nov 28, 2019

@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
Copy link

ThibTrip commented May 7, 2020

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
Copy link

ThibTrip commented May 7, 2020

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
Copy link

firasm commented Nov 11, 2020

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