Closed bryangingechen closed 4 years ago
Sure, this makes sense! Maybe we can have a compile.notebook
that takes in the notebook object and compiles it into a define
function. I'd rather the object be the input instead of a JSON string (so it's not confused with compile.module
).
I thought a bit more about this and there are actually two parts to this:
createModuleDefinition
. This should be straightforward to implement as soon as the other PRs are merged; the only question is picking a convenient / reusable API for compile.cell
; I'll experiment and see.define
function (like the api.observablehq.com/-/-.js?v=3
files). I don't yet see an easy way to do this without repeating much of createModuleDefintion
though. I'll think some more about this.any way (or hint) to make this work? this seems to me like a blocker if we want to be able to "import" local notebooks defined in this way
I was hoping to get back to this within a few weeks or so, but if you'd like to work on it yourself, the basic idea is just to imitate the output of the files that are generated by the https://api.observablehq.com/@:user/:notebook.js?v=3
endpoint. I couldn't tell you the exact format from memory, but roughly speaking those files begin with import
statements for all the referenced imports, then a define
function which creates a main module and then populates it with variable
objects for the code cells and module
objects for the imports.
For the variable
objects, we need to use the parser output to wrap the source code of each non-import cell and turn it into the source code for a variable definition (string to string). I think we may be able to reuse some of the logic in createRegularCellDefintion
that appears before we pass things to the Function
constructor.
Similarly, the parser output for import cells is used to create the module
objects and the import statements at the top of the file. This will just take some fiddling.
Anyways, let me know if you start working on this. I'd be happy to give more suggestions / tips.
I've started work on this. Hoping to PR something by the end of the week. 🤞
This is now live as of v0.5.0. Woohoo!
that is awesome, thank you!
I'm thinking of adapting the code I wrote in this notebook to the V3 runtime format using your compiler.
Would you welcome a PR that adds a function which takes as input a notebook JSON object (maybe also a JSON string) and outputs the define function? Something to worry about here is the fact that the notebook JSON format is not in any way officially supported so it could very well change without warning. However, something like this might still be useful for the folks who have been backing up their notebooks locally per this thread.
As I recall, I used the parser's
parseCell
function to implement the V1 version of this, so I might end up writing some kind ofcompile.cell
function as discussed in #3.