Skip to content
Snippets Groups Projects
Commit 1042e1cc authored by David Rouquet's avatar David Rouquet
Browse files

Add batch_unlizeToNotebook / add ipynb data / commented code to execute...

Add batch_unlizeToNotebook / add ipynb data / commented code to execute notebooks in conversion / rescale svg in notebooks
parent a3d52116
No related branches found
No related tags found
No related merge requests found
Pipeline #208 passed
File added
#!/bin/bash
for filepath in unl/*; do
echo "$filepath"
python unlizeToNotebook.py "$filepath" "$(basename "$filepath" .xml).ipynb"
done
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import nbformat as nbf import nbformat as nbf
from nbformat.v4 import new_code_cell from nbformat.v4 import new_code_cell
import nbconvert as nbc #from nbconvert.preprocessors import ExecutePreprocessor
import click import click
from lxml import etree, objectify from lxml import etree, objectify
from unlizeXml import remove_namespace, unlize, nestedBody2Str from unlizeXml import remove_namespace, unlize, nestedBody2Str
...@@ -32,6 +32,10 @@ def unlizeXmlNb(input, output, template, lang, dry_run): ...@@ -32,6 +32,10 @@ def unlizeXmlNb(input, output, template, lang, dry_run):
addCell(nb, unl) addCell(nb, unl)
else: else:
addCell(nb, getText(node, 'unl')) addCell(nb, getText(node, 'unl'))
# We execute the notebook
#resources = {}
#exec_nb, resources = ExecutePreprocessor(timeout=-1).preprocess(nb, resources)
# Save
with open(output, 'w') as f: with open(output, 'w') as f:
nbf.write(nb, f) nbf.write(nb, f)
......
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import tempfile import tempfile
import os import os
import re import re
import requests import requests
from subprocess import Popen, PIPE, STDOUT from subprocess import Popen, PIPE, STDOUT
from IPython.core.display import SVG from IPython.display import SVG, display, HTML
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def unl2dot(text, path): def unl2dot(text, path):
with tempfile.NamedTemporaryFile() as temp: with tempfile.NamedTemporaryFile() as temp:
out_name = os.path.basename(temp.name) out_name = os.path.basename(temp.name)
out_dir = os.path.dirname(temp.name) out_dir = os.path.dirname(temp.name)
with tempfile.NamedTemporaryFile(mode="w") as in_file: with tempfile.NamedTemporaryFile(mode="w") as in_file:
# Remove CRLF and flush output to avoid java errors # Remove CRLF and flush output to avoid java errors
in_file.write(text.replace("\r\n", "\n")) in_file.write(text.replace("\r\n", "\n"))
in_file.flush() in_file.flush()
# Run java parser # Run java parser
cmd = ['java', '-jar', path, cmd = ['java', '-jar', path,
'--input-file', in_file.name, '--input-file', in_file.name,
'--output-Dir', out_dir, '--output-file', out_name, '--output-Dir', out_dir, '--output-file', out_name,
'--output-type', 'dot'] '--output-type', 'dot']
with Popen(cmd, stdout=PIPE, stderr=STDOUT) as p: with Popen(cmd, stdout=PIPE, stderr=STDOUT) as p:
p.wait() p.wait()
p.stdout.flush() p.stdout.flush()
if p.returncode != 0: if p.returncode != 0:
print("Error in unl2rdf: \n\n"+p.stdout.read().decode()) print("Error in unl2rdf: \n\n"+p.stdout.read().decode())
print('UNL;') print('UNL;')
print(text) print(text)
# generate dot output # generate dot output
fname = '{}/{}.dot'.format(out_dir, out_name) fname = '{}/{}.dot'.format(out_dir, out_name)
cmd = ['dot', '-Tsvg', fname] cmd = ['dot', '-Tsvg', fname]
with Popen(cmd, stdout=PIPE, stderr=PIPE) as p: with Popen(cmd, stdout=PIPE, stderr=PIPE) as p:
p.wait() p.wait()
if p.returncode != 0: if p.returncode != 0:
print("Error creating svg: \n\n"+p.stderr.read().decode()) print("Error creating svg: \n\n"+p.stderr.read().decode())
print('UNL:') print('UNL:')
print(text) print(text)
try: try:
with open(fname) as f: with open(fname) as f:
print('DOT:') print('DOT:')
print(f.read()) print(f.read())
except FileNotFoundError: except FileNotFoundError:
pass pass
else: else:
svg = p.stdout.read().decode() svg = p.stdout.read().decode()
os.remove(fname) os.remove(fname)
return svg return svg
return "" return ""
def unl2dotWeb(unldata) : def unl2dotWeb(unldata) :
data={'unl': unldata, 'outputs':['dot', 'svg']} data={'unl': unldata, 'outputs':['dot', 'svg']}
try: try:
r = requests.post('https://unl.demo.tetras-libre.fr/unl2rdf', data=data) r = requests.post('https://unl.demo.tetras-libre.fr/unl2rdf', data=data)
except Exception as e: except Exception as e:
return 'Error calling https://unl.demo.tetras-libre.fr/unl2rdf : "{error}"'.format(error=e) return 'Error calling https://unl.demo.tetras-libre.fr/unl2rdf : "{error}"'.format(error=e)
html=r.text html=r.text
# On utilise une regex au lieu de parser le html car ce dernier est mal formé # On utilise une regex au lieu de parser le html car ce dernier est mal formé
regex = re.compile('<svg.*svg>',re.MULTILINE|re.DOTALL) regex = re.compile('<svg.*svg>',re.MULTILINE|re.DOTALL)
svg = regex.search(html).group() svg = regex.search(html).group()
return(svg) return(svg)
def svg_to_fixed_width_html_image(svg, width="100%"):
html_template='<img width="{}">{}</img>'
text = html_template.format(width, svg)
return HTML(text)
def displayUnl(unldata) : def displayUnl(unldata) :
# We generate protoSVG because whent there are several sentences, # We generate protoSVG because whent there are several sentences,
# a string composed of several concatenated SVG is produced (not a valid SVG). # a string composed of several concatenated SVG is produced (not a valid SVG).
# We must then split the string to obtain several valid SVG to display. # We must then split the string to obtain several valid SVG to display.
sep = "[/S]\n" sep = "[/S]\n"
unldataArray = [x+sep for x in unldata.split(sep)] unldataArray = [x+sep for x in unldata.split(sep)]
unldataArray.pop() unldataArray.pop()
for unl in unldataArray : for unl in unldataArray :
regex = re.compile('\{org:..\}\n(.*)\n{\/org\}',re.MULTILINE|re.DOTALL) regex = re.compile('\{org:..\}\n(.*)\n{\/org\}',re.MULTILINE|re.DOTALL)
text = regex.search(unl).group(1) text = regex.search(unl).group(1)
print("\n"+text+"\n") print("\n"+text+"\n")
# Keep one of the two lines below depending if you want to use a local jar or a webservice for unltools # Keep one of the two lines below depending if you want to use a local jar or a webservice for unltools
try: try:
#svg = unl2dotWeb(unl) #svg = unl2dotWeb(unl)
svg = unl2dot(unl, "unl2rdf-app-1.0-SNAPSHOT-jar-with-dependencies.jar") svg = unl2dot(unl, "unl2rdf-app-1.0-SNAPSHOT-jar-with-dependencies.jar")
display(SVG(svg)) display(svg_to_fixed_width_html_image(svg))
except Exception as e : except Exception as e :
print(e) print(e)
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment