barunsaha commited on
Commit
94d93d2
1 Parent(s): e55d16a

Use named tempfile to save the presentation

Browse files
Files changed (2) hide show
  1. app.py +13 -8
  2. pptx_helper.py +10 -4
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import json5
3
  import logging
4
  import shutil
@@ -7,6 +8,7 @@ import streamlit as st
7
  import streamlit.runtime.scriptrunner as st_sr
8
  from typing import List, Tuple
9
  import metaphor_python as metaphor
 
10
 
11
  import llm_helper
12
  import pptx_helper
@@ -248,23 +250,26 @@ def generate_slide_deck(json_str: str, pptx_template: str, progress_bar) -> List
248
  progress_text = 'Creating the slide deck...give it a moment'
249
  progress_bar.progress(75, text=progress_text)
250
 
251
- # Get a unique name for the file to save -- use the session ID
252
- ctx = st_sr.get_script_run_ctx()
253
- session_id = ctx.session_id
254
- timestamp = time.time()
255
- output_file_name = f'{session_id}_{timestamp}.pptx'
 
 
 
256
 
257
  logging.info('Creating PPTX file...')
258
  all_headers = pptx_helper.generate_powerpoint_presentation(
259
  json_str,
260
  as_yaml=False,
261
  slides_template=pptx_template,
262
- output_file_name=output_file_name
263
  )
264
  progress_bar.progress(100, text='Done!')
265
 
266
- with open(output_file_name, 'rb') as f:
267
- st.download_button('Download PPTX file', f, file_name=output_file_name)
268
 
269
  return all_headers
270
 
 
1
  import os
2
+ import pathlib
3
  import json5
4
  import logging
5
  import shutil
 
8
  import streamlit.runtime.scriptrunner as st_sr
9
  from typing import List, Tuple
10
  import metaphor_python as metaphor
11
+ import tempfile
12
 
13
  import llm_helper
14
  import pptx_helper
 
250
  progress_text = 'Creating the slide deck...give it a moment'
251
  progress_bar.progress(75, text=progress_text)
252
 
253
+ # # Get a unique name for the file to save -- use the session ID
254
+ # ctx = st_sr.get_script_run_ctx()
255
+ # session_id = ctx.session_id
256
+ # timestamp = time.time()
257
+ # output_file_name = f'{session_id}_{timestamp}.pptx'
258
+
259
+ temp = tempfile.NamedTemporaryFile(delete=False, suffix='.pptx')
260
+ path = pathlib.Path(temp.name)
261
 
262
  logging.info('Creating PPTX file...')
263
  all_headers = pptx_helper.generate_powerpoint_presentation(
264
  json_str,
265
  as_yaml=False,
266
  slides_template=pptx_template,
267
+ output_file_path=path
268
  )
269
  progress_bar.progress(100, text='Done!')
270
 
271
+ with open(path, 'rb') as f:
272
+ st.download_button('Download PPTX file', f, file_name='Presentation.pptx')
273
 
274
  return all_headers
275
 
pptx_helper.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  from typing import List, Tuple
2
  import json5
3
  import logging
@@ -50,7 +52,7 @@ def generate_powerpoint_presentation(
50
  structured_data: str,
51
  as_yaml: bool,
52
  slides_template: str,
53
- output_file_name: str
54
  ) -> List:
55
  """
56
  Create and save a PowerPoint presentation file containing the contents in JSON or YAML format.
@@ -58,7 +60,7 @@ def generate_powerpoint_presentation(
58
  :param structured_data: The presentation contents as "JSON" (may contain trailing commas) or YAML
59
  :param as_yaml: True if the input data is in YAML format; False if it is in JSON format
60
  :param slides_template: The PPTX template to use
61
- :param output_file_name: The name of the PPTX file to save as
62
  :return A list of presentation title and slides headers
63
  """
64
 
@@ -120,7 +122,7 @@ def generate_powerpoint_presentation(
120
  title = slide.shapes.title
121
  title.text = 'Thank you!'
122
 
123
- presentation.save(output_file_name)
124
 
125
  return all_headers
126
 
@@ -235,9 +237,13 @@ if __name__ == '__main__':
235
  }
236
  ]
237
  }'''
 
 
 
 
238
  generate_powerpoint_presentation(
239
  json5.loads(json_data),
240
  as_yaml=False,
241
- output_file_name='test.pptx',
242
  slides_template='Blank'
243
  )
 
1
+ import pathlib
2
+ import tempfile
3
  from typing import List, Tuple
4
  import json5
5
  import logging
 
52
  structured_data: str,
53
  as_yaml: bool,
54
  slides_template: str,
55
+ output_file_path: pathlib.Path
56
  ) -> List:
57
  """
58
  Create and save a PowerPoint presentation file containing the contents in JSON or YAML format.
 
60
  :param structured_data: The presentation contents as "JSON" (may contain trailing commas) or YAML
61
  :param as_yaml: True if the input data is in YAML format; False if it is in JSON format
62
  :param slides_template: The PPTX template to use
63
+ :param output_file_path: The path of the PPTX file to save as
64
  :return A list of presentation title and slides headers
65
  """
66
 
 
122
  title = slide.shapes.title
123
  title.text = 'Thank you!'
124
 
125
+ presentation.save(output_file_path)
126
 
127
  return all_headers
128
 
 
237
  }
238
  ]
239
  }'''
240
+
241
+ temp = tempfile.NamedTemporaryFile(delete=False, suffix='.pptx')
242
+ path = pathlib.Path(temp.name)
243
+
244
  generate_powerpoint_presentation(
245
  json5.loads(json_data),
246
  as_yaml=False,
247
+ output_file_path=path,
248
  slides_template='Blank'
249
  )