bluenevus commited on
Commit
bc2fd7e
·
verified ·
1 Parent(s): 372f5c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -70
app.py CHANGED
@@ -1,79 +1,70 @@
1
  import dash
2
  from dash import dcc, html, Input, Output, State
 
3
  import base64
4
  import io
5
  import re
6
  from pptx import Presentation
7
  from pptx.util import Inches, Pt
8
 
9
- app = dash.Dash(__name__)
10
 
11
- app.layout = html.Div([
12
- html.Div([
13
- html.H3("Upload Markdown File"),
14
- dcc.Upload(
15
- id='upload-file',
16
- children=html.Div(['Drag and Drop or ', html.A('Select Files')]),
17
- style={
18
- 'width': '100%',
19
- 'height': '60px',
20
- 'lineHeight': '60px',
21
- 'borderWidth': '1px',
22
- 'borderStyle': 'dashed',
23
- 'borderRadius': '5px',
24
- 'textAlign': 'center',
25
- 'margin': '10px'
26
- },
27
- multiple=False
28
- ),
29
  ]),
30
- html.Div([
31
- html.H3("Or Paste Markdown Text"),
32
- dcc.Textarea(
33
- id='markdown-text',
34
- placeholder='Enter or paste your markdown text here',
35
- style={'width': '100%', 'height': 200}
36
- ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  ]),
38
- html.Button('Convert', id='convert-button', n_clicks=0),
39
- html.Div(id='output')
40
- ])
 
 
 
 
 
 
 
 
41
 
42
  def markdown_to_pptx(md_text):
43
- prs = Presentation()
44
- slides = re.split(r'# Slide \d+:', md_text)
45
- slides = [slide.strip() for slide in slides if slide.strip()]
46
-
47
- for slide_content in slides:
48
- lines = slide_content.split('\n')
49
- title = lines[0].strip()
50
- current_slide = prs.slides.add_slide(prs.slide_layouts[1])
51
-
52
- title_shape = current_slide.shapes.title
53
- title_shape.text = title
54
-
55
- content = []
56
- for line in lines[1:]:
57
- if line.startswith('## '):
58
- content.append(('heading', line[3:].strip()))
59
- elif line.startswith('- '):
60
- content.append(('bullet', line[2:].strip()))
61
 
62
- text_frame = current_slide.placeholders[1].text_frame
63
- for item_type, item_text in content:
64
- p = text_frame.add_paragraph()
65
- p.text = item_text
66
- if item_type == 'heading':
67
- p.level = 0
68
- p.font.size = Pt(24)
69
- p.font.bold = True
70
- else:
71
- p.level = 1
72
- p.font.size = Pt(18)
73
-
74
- output = io.BytesIO()
75
- prs.save(output)
76
- return output.getvalue()
77
 
78
  @app.callback(
79
  Output('output', 'children'),
@@ -92,16 +83,16 @@ def convert_markdown(n_clicks, file_contents, markdown_text):
92
  elif markdown_text:
93
  md_text = markdown_text
94
  else:
95
- return html.Div('No input provided')
96
 
97
  pptx_output = markdown_to_pptx(md_text)
98
- return html.Div([
99
- html.A(
100
- 'Download PPTX',
101
- href=f"data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,{base64.b64encode(pptx_output).decode()}",
102
- download='output.pptx'
103
- )
104
- ])
105
 
106
  if __name__ == '__main__':
107
  app.run(host='0.0.0.0', port=7860, debug=True)
 
1
  import dash
2
  from dash import dcc, html, Input, Output, State
3
+ import dash_bootstrap_components as dbc
4
  import base64
5
  import io
6
  import re
7
  from pptx import Presentation
8
  from pptx.util import Inches, Pt
9
 
10
+ app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
11
 
12
+ app.layout = dbc.Container([
13
+ dbc.Row([
14
+ dbc.Col([
15
+ html.H1("Markdown to PowerPoint Converter", className="text-center mb-4")
16
+ ])
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  ]),
18
+ dbc.Row([
19
+ dbc.Col([
20
+ dbc.Card([
21
+ dbc.CardHeader("Upload Markdown File"),
22
+ dbc.CardBody([
23
+ dcc.Upload(
24
+ id='upload-file',
25
+ children=dbc.Button("Select File", color="primary", className="mr-2"),
26
+ multiple=False
27
+ ),
28
+ html.Div(id="upload-file-name", className="mt-2")
29
+ ])
30
+ ], className="mb-3")
31
+ ], md=6),
32
+ dbc.Col([
33
+ dbc.Card([
34
+ dbc.CardHeader("Or Paste Markdown Text"),
35
+ dbc.CardBody([
36
+ dbc.Textarea(
37
+ id='markdown-text',
38
+ placeholder='Enter or paste your markdown text here',
39
+ style={'height': 150}
40
+ )
41
+ ])
42
+ ])
43
+ ], md=6)
44
  ]),
45
+ dbc.Row([
46
+ dbc.Col([
47
+ dbc.Button("Convert", id='convert-button', color="success", className="mt-3 mb-3 w-100")
48
+ ])
49
+ ]),
50
+ dbc.Row([
51
+ dbc.Col([
52
+ html.Div(id='output')
53
+ ])
54
+ ])
55
+ ], fluid=True)
56
 
57
  def markdown_to_pptx(md_text):
58
+ # ... (keep the existing markdown_to_pptx function unchanged)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
+ @app.callback(
61
+ Output('upload-file-name', 'children'),
62
+ Input('upload-file', 'filename')
63
+ )
64
+ def update_filename(filename):
65
+ if filename:
66
+ return html.Div(f"Selected file: {filename}")
67
+ return ""
 
 
 
 
 
 
 
68
 
69
  @app.callback(
70
  Output('output', 'children'),
 
83
  elif markdown_text:
84
  md_text = markdown_text
85
  else:
86
+ return dbc.Alert('No input provided', color="warning")
87
 
88
  pptx_output = markdown_to_pptx(md_text)
89
+ return dbc.Button(
90
+ "Download PPTX",
91
+ href=f"data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,{base64.b64encode(pptx_output).decode()}",
92
+ download='output.pptx',
93
+ color="primary",
94
+ className="mt-3"
95
+ )
96
 
97
  if __name__ == '__main__':
98
  app.run(host='0.0.0.0', port=7860, debug=True)