BenjiELCA commited on
Commit
ee93af8
1 Parent(s): 33c579f

add the vizifile downloading

Browse files
modules/htlm_webpage.py CHANGED
@@ -1,7 +1,7 @@
1
  import streamlit as st
2
  import streamlit.components.v1 as components
3
 
4
- def display_bpmn_xml(bpmn_xml, is_mobile=False, screen_width=300):
5
 
6
  if is_mobile:
7
  html_template = f"""
@@ -29,7 +29,7 @@ def display_bpmn_xml(bpmn_xml, is_mobile=False, screen_width=300):
29
  justify-content: flex-start;
30
  gap: 10px;
31
  }}
32
- #save-button, #download-button {{
33
  background-color: #4CAF50;
34
  color: white;
35
  border: none;
@@ -45,6 +45,9 @@ def display_bpmn_xml(bpmn_xml, is_mobile=False, screen_width=300):
45
  #download-button {{
46
  background-color: #008CBA;
47
  }}
 
 
 
48
  #canvas-container {{
49
  flex: 1;
50
  position: relative;
@@ -65,6 +68,7 @@ def display_bpmn_xml(bpmn_xml, is_mobile=False, screen_width=300):
65
  <div id="button-container">
66
  <button id="save-button">Save as BPMN</button>
67
  <button id="download-button">Save as XML</button>
 
68
  </div>
69
  <div id="canvas-container">
70
  <div id="canvas"></div>
@@ -118,8 +122,24 @@ def display_bpmn_xml(bpmn_xml, is_mobile=False, screen_width=300):
118
  }}
119
  }}
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  document.getElementById('save-button').addEventListener('click', saveDiagram);
122
  document.getElementById('download-button').addEventListener('click', downloadXML);
 
123
 
124
  // Ensure the canvas is focused to capture keyboard events
125
  document.getElementById('canvas').focus();
@@ -171,7 +191,7 @@ def display_bpmn_xml(bpmn_xml, is_mobile=False, screen_width=300):
171
  justify-content: flex-start;
172
  gap: 10px;
173
  }}
174
- #save-button, #download-button, #download-png-button {{
175
  background-color: #4CAF50;
176
  color: white;
177
  border: none;
@@ -187,6 +207,9 @@ def display_bpmn_xml(bpmn_xml, is_mobile=False, screen_width=300):
187
  #download-button {{
188
  background-color: #008CBA;
189
  }}
 
 
 
190
  #canvas-container {{
191
  flex: 1;
192
  position: relative;
@@ -207,7 +230,7 @@ def display_bpmn_xml(bpmn_xml, is_mobile=False, screen_width=300):
207
  <div id="button-container">
208
  <button id="save-button">Save as BPMN</button>
209
  <button id="download-button">Save as XML</button>
210
- <button id="download-png-button">Save as PNG</button>
211
  </div>
212
  <div id="canvas-container">
213
  <div id="canvas"></div>
@@ -261,24 +284,24 @@ def display_bpmn_xml(bpmn_xml, is_mobile=False, screen_width=300):
261
  }}
262
  }}
263
 
264
- async function downloadPNG() {{
265
  try {{
266
- const canvas = document.querySelector('#canvas canvas');
267
- const pngUrl = canvas.toDataURL('image/png');
268
- const link = document.createElement('a');
269
- link.href = pngUrl;
270
- link.download = 'diagram.png';
271
- document.body.appendChild(link);
272
- link.click();
273
- document.body.removeChild(link);
274
  }} catch (err) {{
275
- console.error('Error downloading PNG', err);
276
  }}
277
  }}
278
 
279
  document.getElementById('save-button').addEventListener('click', saveDiagram);
280
  document.getElementById('download-button').addEventListener('click', downloadXML);
281
- document.getElementById('download-png-button').addEventListener('click', downloadPNG);
282
 
283
  document.getElementById('canvas').focus();
284
 
@@ -299,3 +322,4 @@ def display_bpmn_xml(bpmn_xml, is_mobile=False, screen_width=300):
299
  """
300
 
301
  components.html(html_template, height=1000, width=int(9/10*screen_width))
 
 
1
  import streamlit as st
2
  import streamlit.components.v1 as components
3
 
4
+ def display_bpmn_xml(bpmn_xml, vizi_file, is_mobile=False, screen_width=300):
5
 
6
  if is_mobile:
7
  html_template = f"""
 
29
  justify-content: flex-start;
30
  gap: 10px;
31
  }}
32
+ #save-button, #download-button, #download-vizi-button {{
33
  background-color: #4CAF50;
34
  color: white;
35
  border: none;
 
45
  #download-button {{
46
  background-color: #008CBA;
47
  }}
48
+ #download-vizi-button {{
49
+ background-color: #FFA500;
50
+ }}
51
  #canvas-container {{
52
  flex: 1;
53
  position: relative;
 
68
  <div id="button-container">
69
  <button id="save-button">Save as BPMN</button>
70
  <button id="download-button">Save as XML</button>
71
+ <button id="download-vizi-button">Save as Vizi</button>
72
  </div>
73
  <div id="canvas-container">
74
  <div id="canvas"></div>
 
122
  }}
123
  }}
124
 
125
+ async function downloadVizi() {{
126
+ try {{
127
+ const blob = new Blob([`{vizi_file}`], {{ type: 'text/plain' }});
128
+ const url = URL.createObjectURL(blob);
129
+ const a = document.createElement('a');
130
+ a.href = url;
131
+ a.download = 'vizi_file.pmw';
132
+ document.body.appendChild(a);
133
+ a.click();
134
+ document.body.removeChild(a);
135
+ }} catch (err) {{
136
+ console.error('Error downloading Vizi file', err);
137
+ }}
138
+ }}
139
+
140
  document.getElementById('save-button').addEventListener('click', saveDiagram);
141
  document.getElementById('download-button').addEventListener('click', downloadXML);
142
+ document.getElementById('download-vizi-button').addEventListener('click', downloadVizi);
143
 
144
  // Ensure the canvas is focused to capture keyboard events
145
  document.getElementById('canvas').focus();
 
191
  justify-content: flex-start;
192
  gap: 10px;
193
  }}
194
+ #save-button, #download-button, #download-vizi-button {{
195
  background-color: #4CAF50;
196
  color: white;
197
  border: none;
 
207
  #download-button {{
208
  background-color: #008CBA;
209
  }}
210
+ #download-vizi-button {{
211
+ background-color: #FFA500;
212
+ }}
213
  #canvas-container {{
214
  flex: 1;
215
  position: relative;
 
230
  <div id="button-container">
231
  <button id="save-button">Save as BPMN</button>
232
  <button id="download-button">Save as XML</button>
233
+ <button id="download-vizi-button">Save as Vizi</button>
234
  </div>
235
  <div id="canvas-container">
236
  <div id="canvas"></div>
 
284
  }}
285
  }}
286
 
287
+ async function downloadVizi() {{
288
  try {{
289
+ const blob = new Blob([`{vizi_file}`], {{ type: 'text/plain' }});
290
+ const url = URL.createObjectURL(blob);
291
+ const a = document.createElement('a');
292
+ a.href = url;
293
+ a.download = 'vizi_file.pmw';
294
+ document.body.appendChild(a);
295
+ a.click();
296
+ document.body.removeChild(a);
297
  }} catch (err) {{
298
+ console.error('Error downloading Vizi file', err);
299
  }}
300
  }}
301
 
302
  document.getElementById('save-button').addEventListener('click', saveDiagram);
303
  document.getElementById('download-button').addEventListener('click', downloadXML);
304
+ document.getElementById('download-vizi-button').addEventListener('click', downloadVizi);
305
 
306
  document.getElementById('canvas').focus();
307
 
 
322
  """
323
 
324
  components.html(html_template, height=1000, width=int(9/10*screen_width))
325
+
modules/streamlit_utils.py CHANGED
@@ -25,6 +25,8 @@ from streamlit_cropper import st_cropper
25
  from streamlit_image_select import image_select
26
  from streamlit_js_eval import streamlit_js_eval
27
 
 
 
28
 
29
 
30
 
@@ -418,7 +420,9 @@ def display_bpmn_modeler(is_mobile, screen_width):
418
  st.session_state.prediction.copy(), st.session_state.text_mapping,
419
  st.session_state.size_scale, st.session_state.scale
420
  )
421
- display_bpmn_xml(st.session_state.bpmn_xml, is_mobile=is_mobile, screen_width=int(4/5 * screen_width))
 
 
422
 
423
  def modeler_options(is_mobile):
424
  if not is_mobile:
 
25
  from streamlit_image_select import image_select
26
  from streamlit_js_eval import streamlit_js_eval
27
 
28
+ from modules.toWizard import create_wizard_file
29
+
30
 
31
 
32
 
 
420
  st.session_state.prediction.copy(), st.session_state.text_mapping,
421
  st.session_state.size_scale, st.session_state.scale
422
  )
423
+ st.session_state.vizi_file = create_wizard_file(st.session_state.prediction.copy(), st.session_state.text_mapping)
424
+ print(st.session_state.vizi_file)
425
+ display_bpmn_xml(st.session_state.bpmn_xml, st.session_state.vizi_file, is_mobile=is_mobile, screen_width=int(4/5 * screen_width))
426
 
427
  def modeler_options(is_mobile):
428
  if not is_mobile:
toWizard.py → modules/toWizard.py RENAMED
@@ -1,5 +1,6 @@
1
  import xml.etree.ElementTree as ET
2
- from utils import class_dict
 
3
 
4
  def rescale(scale, boxes):
5
  for i in range(len(boxes)):
@@ -43,3 +44,103 @@ def create_BPMN_id(data):
43
  enum_pool += 1
44
 
45
  return data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import xml.etree.ElementTree as ET
2
+ from modules.utils import class_dict
3
+ from xml.dom import minidom
4
 
5
  def rescale(scale, boxes):
6
  for i in range(len(boxes)):
 
44
  enum_pool += 1
45
 
46
  return data
47
+
48
+ def check_end(val):
49
+ if val[1] is None:
50
+ return True
51
+ return False
52
+
53
+ def connect(data, text_mapping, i):
54
+ target_idx = data['links'][i][1]
55
+ current_id = data['BPMN_id'][i]
56
+ next_idx = data['links'][target_idx][1]
57
+ next_id = data['BPMN_id'][next_idx]
58
+ next_text = text_mapping[next_id]
59
+ current_text = text_mapping[current_id]
60
+
61
+ return current_text, next_text
62
+
63
+ def check_start(val):
64
+ if val[0] is None:
65
+ return True
66
+ return False
67
+
68
+
69
+
70
+ def create_wizard_file(data, text_mapping):
71
+ root = ET.Element('methodAndStyleWizard')
72
+
73
+ modelName = ET.SubElement(root, 'modelName')
74
+ modelName.text = 'My Diagram'
75
+
76
+ author = ET.SubElement(root, 'author')
77
+ author.text = 'Benjamin'
78
+
79
+ # Add pools to the collaboration element
80
+ for idx, (pool_index, keep_elements) in enumerate(data['pool_dict'].items()):
81
+ pool_id = f'participant_{idx+1}'
82
+ pool = ET.SubElement(root, 'processName')
83
+ pool.text = text_mapping[pool_index]
84
+
85
+ processDescription = ET.SubElement(root, 'processDescription')
86
+
87
+
88
+ for idx, Bpmn_id in enumerate(data['BPMN_id']):
89
+ # Start Event
90
+ element_type = Bpmn_id.split('_')[0]
91
+ if element_type == 'message':
92
+ eventType = 'Message'
93
+ elif element_type == 'event':
94
+ eventType = 'None'
95
+ if idx >= len(data['links']):
96
+ continue
97
+ if check_start(data['links'][idx]) and (element_type=='event' or element_type=='message'):
98
+ startEvent = ET.SubElement(root, 'startEvent', attrib={'name': text_mapping[Bpmn_id], 'eventType': eventType})
99
+
100
+ requestMessage = ET.SubElement(root, 'requestMessage')
101
+ requester = ET.SubElement(root, 'requester')
102
+
103
+ endEvents = ET.SubElement(root, 'endStates')
104
+ for idx, Bpmn_id in enumerate(data['BPMN_id']):
105
+ # End States
106
+ if idx >= len(data['links']):
107
+ continue
108
+ if check_end(data['links'][idx]) and Bpmn_id.split('_')[0] == 'event':
109
+ if text_mapping[Bpmn_id] == '':
110
+ text_mapping[Bpmn_id] = '(unnamed)'
111
+ ET.SubElement(endEvents, 'endState', attrib={'name': text_mapping[Bpmn_id], 'eventType': 'None', 'isRegular': 'False'})
112
+
113
+
114
+
115
+ activities = ET.SubElement(root, 'activities')
116
+
117
+ for idx, activity_name in enumerate(data['BPMN_id']):
118
+ if activity_name.startswith('task'):
119
+ target_text = ' FINISH'
120
+ activity = ET.SubElement(activities, 'activity', attrib={'name': text_mapping.get(activity_name, activity_name), 'performer': ''})
121
+ endStates = ET.SubElement(activity, 'endStates')
122
+ current_text, next_text = connect(data, text_mapping, idx)
123
+ ET.SubElement(endStates, 'endState', attrib={'name': next_text, 'isRegular': 'True'})
124
+ ET.SubElement(activity, 'subActivities')
125
+ ET.SubElement(activity, 'subActivityFlows')
126
+ ET.SubElement(activity, 'messageFlows')
127
+
128
+ activityFlows = ET.SubElement(root, 'activityFlows')
129
+ i=0
130
+ for i, link in enumerate(data['links']):
131
+ if link[0] is None and link[1] is not None and (data['BPMN_id'][i].split('_')[0] == 'event' or data['BPMN_id'][i].split('_')[0] == 'message'):
132
+ current_text, next_text = connect(data, text_mapping, i)
133
+ ET.SubElement(activityFlows, 'activityFlow', attrib={'startEvent': current_text, 'endState': '---', 'target': next_text, 'isMerging': 'False', 'isPredefined': 'True'})
134
+ i+=1
135
+ if link[0] is not None and link[1] is not None and data['BPMN_id'][i].split('_')[0] == 'task':
136
+ current_text, next_text = connect(data, text_mapping, i)
137
+ ET.SubElement(activityFlows, 'activityFlow', attrib={'activity': current_text, 'endState': '---', 'target': next_text, 'isMerging': 'False', 'isPredefined': 'True'})
138
+ i+=1
139
+
140
+ ET.SubElement(root, 'participants')
141
+
142
+ # Pretty print the XML
143
+ xml_str = ET.tostring(root, encoding='utf-8', method='xml')
144
+ pretty_xml_str = minidom.parseString(xml_str).toprettyxml(indent=" ")
145
+
146
+ return pretty_xml_str