1

I just discovered Beaker Notebook. I love the concept, and am desperately keen to use it for work. To do so, I need to be sure I can share my code in other formats.

Question

Say I write pure Python in a Beaker notebook:

  1. Can I save it as a .py file as I can in iPython Notebook/Jupyter?
  2. Could I do the same if I wrote a pure R Beaker notebook?
  3. If I wrote a mixed (polyglot) notebook with Python and R, can I save this to e.g. Python, with R code present but commented out?
  4. Lets say none of the above are possible. Looking at the Beaker Notebook file as a text file, it seems to be saved in JSON. I can even find the cells that correspond to e.g. Python, R. It doesn't look like it would be too challenging to write a python script that does 1-3 above. Am I missing something?

Thanks!

PS - there's no Beaker notebook tag!? bad sign...

rawr
  • 20,481
  • 4
  • 44
  • 78
user3279453
  • 403
  • 1
  • 3
  • 12

1 Answers1

1

It's really not that hard to replicate the basics of the export:

#' Save a beaker notebook cell type to a file
#' 
#' @param notebook path to the notebook file
#' @param output path to the output file (NOTE: this file will be overwritten)
#' @param cell_type which cells to export
save_bkr <- function(notebook="notebook.bkr", 
                     output="saved.py", 
                     cell_type="IPython") {

  nb <- jsonlite::fromJSON(notebook)

  tmp <- subset(nb$cells, evaluator == cell_type)$input

  if (length(tmp) != 0) {
    unlink(output)
    purrr::walk(tidyr::unnest(tmp, body), cat, file=output, append=TRUE, sep="\n")
  } else {
    message("No cells found matching cell type")
  }

}

I have no idea what Jupyter does with the "magic" stuff (gosh how can notebook folks take that seriously with a name like "magic").

This can be enhanced greatly, but it gets you the basics of what you asked.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205