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

Téléverser un nouveau fichier

parent 5fb93ea6
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:160ab8ab-091e-4b00-b63a-c3746c71c540 tags:
``` python
import panel as pn
import param
from glob import glob
import base64
import re
import requests
from subprocess import Popen, PIPE, STDOUT
import html
from cairosvg import svg2png
pn.extension(comms='ipywidgets')
```
%% Output
%% Cell type:code id:ca43f1f2-42ef-4355-a2e2-e27351a51b96 tags:
``` python
storeBase = 'store/CCTP-SRSA-IP-20210831/'
def unl2dotWeb(unldata) :
data={'unl': unldata, 'outputs':['dot', 'svg', 'rdf']}
try:
r = requests.post('https://unl.demo.tetras-libre.fr/unl2rdf', data=data)
except Exception as e:
return 'Error calling https://unl.demo.tetras-libre.fr/unl2rdf : "{error}"'.format(error=e)
html=r.text
# On utilise une regex au lieu de parser le html car ce dernier est mal formé
regexSvg = re.compile('<svg.*svg>',re.MULTILINE|re.DOTALL)
regexRdf = re.compile("<code id='rdf' class='collapse show'>(.*?)</code>",re.MULTILINE|re.DOTALL)
try :
svg = regexSvg.search(html).group()
rdf = regexRdf.search(html).group(1)
except Exception as e :
svg = ''
rdf = ''
print(e)
return(svg, rdf)
def writeUnlFiles(unlStr, storePrefix):
srsaRef = selectDir.value
with open(storePrefix+'.unl','w') as unlFile:
unlFile.write(unlStr)
unlFile.close()
# Send UNL code to https://unl.demo.tetras-libre.fr/unl2rdf to get SVG and RDF
svg, rdf = unl2dotWeb(unlStr)
rdf = html.unescape(rdf)
with open(storePrefix+'.svg','w') as svgFile:
svgFile.write(svg)
svgFile.close()
with open(storePrefix+'.ttl','w') as rdfFile:
rdfFile.write(rdf)
rdfFile.close()
# Convert svg to png and write to a file
svg2png(bytestring=svg, write_to=storePrefix+'.png')
```
%% Cell type:code id:c65dbb6f-480e-429e-b3b9-569759ec3b67 tags:
``` python
pathList = glob(storeBase+'*')
dirList = sorted([x.split('/')[-1] for x in pathList])
saveButtonClicks = 0
selectDir = pn.widgets.Select(name='Exigence : ', options=dirList, width = 300)
dir_selector = dict(directory=selectDir)#, save=saveButton)
def main_pane(directory):
pane_width = 1200
saveButtonClicks = 0
saveButton = pn.widgets.Button(name='Enregistrer', button_type='success', width = 100)
saveButtonDic = dict(button=saveButton)
saveCommentButton = pn.widgets.Button(name='Enregistrer', button_type='success', width = 100)
path = storeBase+directory+'/current/'
pathOrig = storeBase+directory+'/orig/'
svgPath = path+directory+'.svg'
pngPath = path+directory+'.png'
unlPath = path+directory+'.unl'
rdfPath = path+directory+'.ttl'
commentPath = path+directory+'.comments'
with open(commentPath) as commentFile:
commentStr = commentFile.read()
commentFile.close()
with open(unlPath) as unlFile:
unlStr = unlFile.read()
unlFile.close()
svgPathOrig = pathOrig+directory+'.svg'
pngPathOrig = pathOrig+directory+'.png'
unlPathOrig = pathOrig+directory+'.unl'
rdfPathOrig = pathOrig+directory+'.ttl'
with open(unlPathOrig) as unlFileOrig:
unlStrOrig = unlFileOrig.read()
unlFileOrig.close()
unlHtmlOrig = unlStrOrig.replace("\n","<br/>")
if unlStrOrig == unlStr:
modIndicator = ''
else:
modIndicator = ' <u>modifié</u>'
regexFr = re.compile("{org:fr}\n(.*?)\n{/org}",re.MULTILINE|re.DOTALL)
try:
frStr = regexFr.search(unlStr).group(1)
except AttributeError:
frStr = ''
regexEn = re.compile("{en}\n(.*?)\n{/en}",re.MULTILINE|re.DOTALL)
try:
enStr = regexEn.search(unlStr).group(1)
except AttributeError:
enStr = ''
unlOrig_html = pn.pane.HTML(unlHtmlOrig)
unl_input = pn.widgets.input.TextAreaInput(height=400)
unl_input.value = unlStr
comment_input = pn.widgets.input.TextAreaInput(height=300)
comment_input.value = commentStr
downloadSvg = pn.widgets.FileDownload(sizing_mode='stretch_width', file=svgPath, embed=True, name='Télécharger le graphe en SVG :')
downloadPng = pn.widgets.FileDownload(sizing_mode='stretch_width', file=pngPath, embed=True, name='Télécharger le graphe en PNG :')
downloadRdf = pn.widgets.FileDownload(sizing_mode='stretch_width', file=rdfPath, embed=True, name='Télécharger le code UNL-RDF :')
downloadUnl = pn.widgets.FileDownload(sizing_mode='stretch_width', file=unlPath, embed=True, name='Télécharger le code UNL :')
def compute_unl_graph_pane(button):
global saveButtonClicks
if saveButtonClicks != 0:
writeUnlFiles(unl_input.value, storeBase+selectDir.value+'/current/'+selectDir.value)
pane = pn.pane.PNG(pngPath, width = pane_width)
saveButtonClicks += 1
return(pane)
unl_graph_pane = pn.interact(lambda button : compute_unl_graph_pane(button), **saveButtonDic)
pane = pn.Column(
pn.pane.HTML(directory),
pn.pane.HTML('FR : '+frStr),
pn.pane.HTML('EN : '+enStr),
unl_graph_pane[1],
pn.Card(pn.Column(saveCommentButton, comment_input, width = pane_width),
header='**Commentaires** *(cliquer pour afficher)*',
collapsed=True, width = pane_width),
pn.Card(pn.Column(saveButton, unl_input, width = pane_width),
header='**Code UNL**'+modIndicator+' *(cliquer pour afficher)*',
collapsed=True, width = pane_width),
pn.Card(pn.Column(
unlOrig_html,
pn.Card(pn.pane.PNG(pngPath, width = pane_width-20), header = "**Graphe d'origine**", width=pane_width-10)
),
header="**Code UNL d'origine** *(cliquer pour afficher)*",
collapsed=True, width = pane_width),
pn.WidgetBox(
pn.Row(downloadPng, downloadSvg),
pn.Row(downloadUnl, downloadRdf),
width=pane_width,
),
width=pane_width,
)
def saveComments(event):
with open(commentPath, 'w') as commentFile:
commentFile.write(comment_input.value)
commentFile.close()
saveCommentButton.on_click(saveComments)
return(pane)
pane = pn.interact(lambda directory : main_pane(directory), **dir_selector)
pn.Row(
pn.layout.HSpacer(),
pn.Column(
selectDir,
pane[1],
),
pn.layout.HSpacer(),
)
```
%% Output
BokehModel(combine_events=True, render_bundle={'docs_json': {'5655cbcb-db50-45d6-a5e1-0dbb63ee7c9d': {'defs': …
%% Cell type:code id:5d4ec56e-d0bb-44c8-975b-49d409b6b160 tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment