简体   繁体   中英

How do I save a Beaker notebook as straight python/r/...?

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 eg 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 eg 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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM