From b0e08601f0e3be569cf41636b408419aae41b932 Mon Sep 17 00:00:00 2001 From: James J Balamuta Date: Mon, 12 Feb 2024 19:45:38 -0800 Subject: [PATCH] Allow injection into the document --- .../pyodide/qpyodide-document-settings.js | 21 +++++++++++++++ _extensions/pyodide/qpyodide.lua | 26 +++++-------------- 2 files changed, 27 insertions(+), 20 deletions(-) create mode 100644 _extensions/pyodide/qpyodide-document-settings.js diff --git a/_extensions/pyodide/qpyodide-document-settings.js b/_extensions/pyodide/qpyodide-document-settings.js new file mode 100644 index 0000000..d32a652 --- /dev/null +++ b/_extensions/pyodide/qpyodide-document-settings.js @@ -0,0 +1,21 @@ +// Document level settings ---- + +// Determine if we need to install R packages +globalThis.qpyodideInstallPythonPackagesList = [{{INSTALLPYTHONPACKAGESLIST}}]; + +// Check to see if we have an empty array, if we do set to skip the installation. +globalThis.qpyodideSetupPythonPackages = !(qpyodideInstallPythonPackagesList.indexOf("") !== -1); + +// Display a startup message? +globalThis.qpyodideShowStartupMessage = {{SHOWSTARTUPMESSAGE}}; + +// Describe the webR settings that should be used +globalThis.qpyodideCustomizedPyodideOptions = { + "baseURL": "{{BASEURL}}", + "serviceWorkerUrl": "{{SERVICEWORKERURL}}", + "homedir": "{{HOMEDIR}}", + "channelType": "{{CHANNELTYPE}}" +}; + +// Store cell data +globalThis.qpyodideCellDetails = {{QPYODIDECELLDETAILS}}; \ No newline at end of file diff --git a/_extensions/pyodide/qpyodide.lua b/_extensions/pyodide/qpyodide.lua index 78fdc35..2526710 100644 --- a/_extensions/pyodide/qpyodide.lua +++ b/_extensions/pyodide/qpyodide.lua @@ -210,20 +210,6 @@ function readTemplateFile(template) return content end --- Obtain the initialization template file at pyodide-init.html -function initializationTemplateFile() - return readTemplateFile("pyodide-init.html") -end - - --- Obtain the editor template file at pyodide-context-interactive.html -function interactiveTemplateFile() - return readTemplateFile("pyodide-context-interactive.html") -end - --- Cache a copy of each public-facing templates to avoid multiple read/writes. --- interactive_template = interactiveTemplateFile() - -- Define a function that escape control sequence function escapeControlSequences(str) -- Perform a global replacement on the control sequence character @@ -243,16 +229,16 @@ function initializationPyodide() ["BASEURL"] = baseUrl, ["HOMEDIR"] = homeDir, ["INSTALLPYTHONPACKAGESLIST"] = installPythonPackagesList, + ["QPYODIDECELLDETAILS"] = quarto.json.encode(qPyodideCapturedCodeBlocks), } -- Make sure we perform a copy - --local initializationTemplate = initializationTemplateFile() + local initializationTemplate = readTemplateFile("qyodide-document-settings.js") -- Make the necessary substitutions - --local initializedPyodideConfiguration = substitute_in_file(initializationTemplate, substitutions) + local initializedPyodideConfiguration = substitute_in_file(initializationTemplate, substitutions) - --return initializedPyodideConfiguration - return "Placeholder" + return initializedPyodideConfiguration end -- Setup Pyodide's pre-requisites once per document. @@ -275,10 +261,10 @@ function ensurePyodideSetup() --quarto.doc.include_file("in-header", "pyodide-styling.html") -- Insert the Pyodide initialization routine - --quarto.doc.include_text("in-header", initializedConfigurationPyodide) + quarto.doc.include_text("in-header", initializedConfigurationPyodide) -- Insert the Monaco Editor initialization - --quarto.doc.include_file("before-body", "monaco-editor-init.html") + quarto.doc.include_file("before-body", "monaco-editor-init.html") end