Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolves #4 write execution result in cache directory #7

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
.cache/
34 changes: 34 additions & 0 deletions spec/dynamic-notebook.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* global describe it */
const fsp = require('node:fs/promises')
const ospath = require('node:path')
const chai = require('chai')
const expect = chai.expect

Expand Down Expand Up @@ -88,6 +90,38 @@ NameError: name 'invalid_python' is not defined</pre>
</div>
</details>`)
})
it('should write execution result when :dynamic-blocks-cache-result: is defined', async () => {
const input = `
:dynamic-blocks:
:dynamic-blocks-cache-result:

[%dynamic,python]
----
print('hello')
----
`
const registry = asciidoctor.Extensions.create()
dynamicNotebookExt.register(registry)
const doc = asciidoctor.load(input, {extension_registry: registry})
const html = doc.convert()
expect(html).to.equal(`<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-python" data-lang="python">print('hello')</code></pre>
</div>
</div>
<details>
<summary class="title">Results</summary>
<div class="content">
<div class="literalblock dynamic-py-result">
<div class="content">
<pre>hello</pre>
</div>
</div>
</div>
</details>`)
const executionResultData = await fsp.readFile(ospath.join(__dirname, '..', '.cache', 'e73b48e8e00d36304ea7204a0683c814-0.json'), 'utf8')
expect(executionResultData).to.equal(`{"success":true,"stderr":"","stdout":"hello\\n","id":"e73b48e8e00d36304ea7204a0683c814-0","code":"print('hello')"}`)
})
it('should output more than one Plotly charts', () => {
const input = `
:dynamic-blocks:
Expand Down
18 changes: 17 additions & 1 deletion src/dynamic-notebook-processor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* global Opal */
const child_process = require('node:child_process')
const fs = require('node:fs')
const ospath = require('node:path')

const conumRx = /\s*<i class="conum" data-value="[0-9]+"><\/i><b>[^>]+<\/b>/g
const figShowRx = /fig.show\(\)/g
Expand All @@ -14,6 +16,7 @@ const ipythonTemplate = (pyCodes) => {
from IPython.utils.capture import capture_output
import json
import sys
import hashlib

shell = InteractiveShell()
results = []
Expand All @@ -24,10 +27,13 @@ ${pyCodes.map((pyCode, index) => {
return `
with capture_output() as io${index}:
r${index} = shell.run_cell(${pyCode})
md5sum = hashlib.md5(${pyCode}.encode('utf8')).hexdigest()
results.append({
'success': r${index}.success,
'stderr': io${index}.stderr,
'stdout': io${index}.stdout
'stdout': io${index}.stdout,
'id': f"{md5sum}-${index}",
'code': ${pyCode}
})

`
Expand Down Expand Up @@ -101,6 +107,16 @@ module.exports.register = function register(registry) {
const exampleBlock = self.createExampleBlock(block, '', attrs, {'content_model': 'compound'})
exampleBlock.setTitle('Results')
const result = response[index]
let cacheResultDir = doc.getAttribute('dynamic-blocks-cache-result')
if (cacheResultDir !== undefined) {
if (cacheResultDir === '') {
cacheResultDir = '.cache'
}
if (!fs.existsSync(cacheResultDir)) {
fs.mkdirSync(cacheResultDir, { recursive: true })
}
fs.writeFileSync(ospath.join(cacheResultDir, `${result.id}.json`), JSON.stringify(result), 'utf8')
}
let source = result.stdout.toString('utf8')
if (result.success === false && block.hasAttribute("fail-on-error")) {
throw new ExecutionError(result.stderr.toString('utf8') + " " + result.stdout.toString('utf8'))
Expand Down