Commit bb3dc3a2 authored by Klaus Wölfel's avatar Klaus Wölfel Committed by Ivan Tyagov

erp5_data_notebook: Add notebook conversion and execution

parent 1a66b21c
import nbformat
import ssl
import re
from base64 import b64encode, b64decode
from nbconvert import HTMLExporter
from nbconvert.preprocessors import Preprocessor, ExecutePreprocessor
from xmlrpclib import ServerProxy
from urllib import urlencode
from urllib2 import urlopen
class PyMarkdownPreprocessor(Preprocessor):
"""
:mod:`nbconvert` Preprocessor for the python-markdown nbextension.
This :class:`~nbconvert.preprocessors.Preprocessor` replaces kernel code in
markdown cells with the results stored in the cell metadata.
"""
def replace_variables(self, source, variables):
"""
Replace {{variablename}} with stored value
"""
try:
replaced = re.sub(
"{{(.*?)}}", lambda m: variables.get(m.group(1), ''), source)
except TypeError:
replaced = source
return replaced
def preprocess_cell(self, cell, resources, index):
"""
Preprocess cell
Parameters
----------
cell : NotebookNode cell
Notebook cell being processed
resources : dictionary
Additional resources used in the conversion process. Allows
preprocessors to pass variables into the Jinja engine.
cell_index : int
Index of the cell being processed (see base.py)
"""
if cell.cell_type == "markdown":
if hasattr(cell['metadata'], 'variables'):
variables = cell['metadata']['variables']
if len(variables) > 0:
cell.source = self.replace_variables(
cell.source, variables)
return cell, resources
def to_html(self):
notebook = nbformat.reads(self.getTextContent(), as_version=4)
resources = {}
notebook = \
ExecutePreprocessor(kernel_name="erp5").preprocess(notebook, resources)[0]
notebook = PyMarkdownPreprocessor().preprocess(notebook, resources)[0]
html_exporter = HTMLExporter()
html_exporter.template_file = 'full'
body, resources = html_exporter.from_notebook_node(notebook)
return body
def cloudoooConvertFile(self, data, source_mimetype, destination_mimetype, zip=False, refresh=False, conversion_kw=None):
url = 'https://softinst78992.host.vifib.net/erp5/ERP5Site_htmlToPdf'
data_dict = {'data' : b64encode(data)}
data_dict.update(**conversion_kw)
return urlopen(url=url, data=urlencode(data_dict)).read()
proxy = ServerProxy(self.getPortalObject().portal_preferences.getPreferredDocumentConversionServerUrl(), allow_none=True)
return b64decode(proxy.convertFile(b64encode(data), source_mimetype, destination_mimetype, zip, refresh, conversion_kw or {}))
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Extension Component" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_recorded_property_dict</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>default_reference</string> </key>
<value> <string>JupyterConvert</string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>extension.erp5.JupyterConvert</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Extension Component</string> </value>
</item>
<item>
<key> <string>sid</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>text_content_error_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple>
<string>W: 65, 75: Redefining built-in \'zip\' (redefined-builtin)</string>
<string>W: 70, 2: Unreachable code (unreachable)</string>
<string>W: 2, 0: Unused import ssl (unused-import)</string>
</tuple>
</value>
</item>
<item>
<key> <string>version</string> </key>
<value> <string>erp5</string> </value>
</item>
<item>
<key> <string>workflow_history</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary/>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary>
<item>
<key> <string>component_validation_workflow</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAQ=</string> </persistent>
</value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="4" aka="AAAAAAAAAAQ=">
<pickle>
<global name="WorkflowHistoryList" module="Products.ERP5Type.patches.WorkflowTool"/>
</pickle>
<pickle>
<tuple>
<none/>
<list>
<dictionary>
<item>
<key> <string>action</string> </key>
<value> <string>validate</string> </value>
</item>
<item>
<key> <string>validation_state</string> </key>
<value> <string>validated</string> </value>
</item>
</dictionary>
</list>
</tuple>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ExternalMethod" module="Products.ExternalMethod.ExternalMethod"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_function</string> </key>
<value> <string>to_html</string> </value>
</item>
<item>
<key> <string>_module</string> </key>
<value> <string>JupyterConvert</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>DataNotebook_toHtml</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
from base64 import b64encode
portal = context.getPortalObject()
#web_page = portal.web_page_module.newContent(
# text_content = text_content,
# temp_object=1,
# title="Bericht")
web_page = context.web_page_module["6"]
text_content = context.DataNotebook_toHtml()
web_page.setTextContent(text_content)
html_data = web_page.WebPage_exportAsSingleFile(format="html", allow_script=True)
pdf_file = portal.Base_cloudoooConvertFile(html_data, "html", "pdf",
conversion_kw=dict(
encoding="utf8",
margin_top=10,
margin_bottom=10,
margin_left=10,
margin_right=10,
header_spacing=0,
header_html_data=b64encode(""),
footer_html_data=b64encode(""),
print_media_type=True,
javascript_delay=9000,
page_size="A4",
zoom=1,
dpi="75"
)
)
REQUEST = context.REQUEST
if REQUEST is not None:
REQUEST.RESPONSE.setHeader("Content-Type", "application/pdf")
REQUEST.response.setHeader('Content-Length', len(pdf_file))
#REQUEST.response.setHeader('Content-Disposition',
# 'attachment;filename="%s.pdf"' % context.getTitle())
return pdf_file
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>DataNotebook_toPdf</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
extension.erp5.JupyterCompile extension.erp5.JupyterCompile
\ No newline at end of file extension.erp5.JupyterConvert
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment