Skip to content

Commit

Permalink
Added jupyter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
John2143 committed Aug 1, 2018
1 parent 7ac8902 commit 3231fe5
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 12 deletions.
78 changes: 70 additions & 8 deletions bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bundle.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion rollup.config.js
Expand Up @@ -9,7 +9,7 @@ let config = {
input: "-----",
external: [
...Object.keys(packagejson.dependencies),
"path", "fs", "chalk", "os",
"path", "fs", "chalk", "os", "child_process", "perf_hooks"
],
output: {
file: "----",
Expand Down
30 changes: 28 additions & 2 deletions src/cli.js
Expand Up @@ -12,7 +12,7 @@ import {version as packageVersion} from "../package.json";
import {configFile, configObject} from "./config.js";
import {writeFileSync} from "fs";

import {helpText, arg, param, usage, helpEntries} from "./decorators.js";
import {helpText, arg, param, usage, helpEntries, spawn} from "./decorators.js";

import * as configHelpers from "./config-create.js";

Expand Down Expand Up @@ -108,7 +108,6 @@ let presetsub = {

//standard diff
argv.command = argv.command || "diff";
const spawn = require("child_process").spawn;
await spawn(argv.command, [file, temp], {stdio: "inherit"});
},
async unknown(arg, args){
Expand All @@ -134,6 +133,26 @@ let rulesub = {
},
}

let jupytersub = {
async before(args){
this.input = args._.shift() || "main.ipynb";
this.output = args._.shift() || "main.py";
},
async $build(args){
let cmd = `jupyter nbconvert --to python ${this.input} --TagRemovePreprocessor.remove_cell_tags={\"remove_cell\"} --output ${this.output} --TemplateExporter.exclude_markdown=True --TemplateExporter.exclude_input_prompt=True --TemplateExporter.exclude_output_prompt=True`.split(" ");
log(chalk`Compiling GCR file {green ${this.input}} into {green ${this.output}} using jupyter...`);

try{
let {timestr} = await spawn(cmd[0], cmd.slice(1));
log(chalk`Complete in ~{green.bold ${timestr}}.`);
}catch(e){
if(e.code !== "ENOENT") throw e;
log(chalk`Cannot run the build command. Make sure that you have jupyter notebook installed.\n{green pip install jupyter}`);
return;
}
},
}

let supplysub = {
async before(args){
this.env = args.env;
Expand Down Expand Up @@ -235,6 +254,13 @@ let cli = {
}
},

@helpText("Rally tools jupyter interface. Requires jupyter to be installed.")
@usage("rally jupyter build [in] [out]")
@param("in/out", "input and output file for jupyter. By default main.ipyrb and main.py")
async jupyter(args){
return subCommand(jupytersub)(args);
},

//@helpText(`Print input args, for debugging`)
async printArgs(args){
log(args);
Expand Down
30 changes: 30 additions & 0 deletions src/decorators.js
Expand Up @@ -122,3 +122,33 @@ export function defineAssoc(classname, shortname, path){
},
});
}

import {spawn as cp_spawn} from "child_process";
import {performance} from "perf_hooks";

//Spawn promise decorator, based on https://gist.github.com/Stuk/6226938
export function spawn(options, ...args){
if(typeof options !== "object"){
args.unshift(options);
options = {};
}
//todo options
return new Promise((resolve, reject) => {
let start = performance.now();

let stdout = "";
let stderr = "";
let cp = cp_spawn(...args);

if(cp.stdout) cp.stdout.on("data", chunk => {stdout += chunk; write(chunk)});
if(cp.stderr) cp.stderr.on("data", chunk => {stderr += chunk; write(chunk)});

cp.on("error", reject);
cp.on("close", code => {
let end = performance.now();
let time = end - start;
let timestr = time > 1000 ? (time/100|0)/10 + "s" : (time|0) + "ms";
resolve({stdout, stderr, exitCode: code, time, timestr});
});
});
}

0 comments on commit 3231fe5

Please sign in to comment.