Is there an easy way to only compile an Jupyter No...
# datascience
p
Is there an easy way to only compile an Jupyter Notebook cell but not run it. For example, currently I use the following code snippet for regression testing of expected outcome vs real outcome of a notebook. Please notice the use of the
exec
method to run a cell.
Copy code
for (cell in notebook.cells.filterIsInstance<CodeCell>()) {
    val cellResult = exec(cell.source)
    val result = if (cellResult is MimeTypedResult) cellResult.entries.first().value else cellResult.toString()

    if (cell.outputs.isNotEmpty()) {
        val firstOutput = cell.outputs.first()
        if (firstOutput is ExecuteResult && firstOutput.data.isNotEmpty()) {
            val output = firstOutput.data.entries.first()
            assertEquals(output.value.removeUUID(), result.removeUUID())
        }
    }
}
But some cells take very long to run, so I would like to have the option to only compile the cell and get compilation errors, but not run (exec) the cell. Any hints how to achieve this?
a
You can always wrap it in a function.
p
Now I think about it, if I only compile (but not execute) a cell, would a variable defined in that cell still be “known” in the next cell or would this cause a compilation error? So would the following example throw a compilation exception if I only compile cells and don’t execute them?
cell 1: val x = 12
cell 2: println(x)
I think wrapping in a function doesn’t expose variable defined in earlier cells to later cells. I could wrap all cells in one function, but then I have to make sure the notebook doesn’t use multiple
val
statements for same variable.
i
It's not supposed to work this way. Sometimes cell executions may trigger some other executions and compilations, some code may be generated and evaluated based on the results of the executed cell etc. So skipping execution itself in this process may affect the "compile-time" state of the notebook - the set of its variables and their types. If you're running some notebooks in tests, maybe you could just lighten these notebooks somehow?
p
Thanks. Will look at some other ways to keep these how-to notebooks in sync with my library code base (perhaps generate these notebooks based on plain code that is known to compile).