Deepa Shalini commited on
Commit
889dfc6
·
1 Parent(s): 159a221

download fig as html feature to chartbot-common

Browse files
pages/ewf_chartbot.py CHANGED
@@ -28,7 +28,9 @@ layout, df = chartbot_dataset_layout.chartbot_common(
28
  "ewf-textarea",
29
  "ewf-submit-button",
30
  "ewf-chartbot-output",
31
- "ewf-python-content-output"
 
 
32
  )
33
 
34
  @callback(
@@ -44,6 +46,8 @@ def update_prompt1_in_textarea(n_clicks1, n_clicks2):
44
  @callback(
45
  Output("ewf-chartbot-output", "children"),
46
  Output("ewf-python-content-output", "children"),
 
 
47
  Input("ewf-submit-button", "n_clicks"),
48
  State("ewf-textarea", "value"),
49
  prevent_initial_call=True
@@ -54,4 +58,13 @@ def create_graph(_, user_prompt):
54
 
55
  result_output = prompt.get_response(user_prompt, data_top5_csv_string, DATA_FILE_PATH)
56
 
57
- return helpers.display_response(result_output, DATA_FILE_PATH)
 
 
 
 
 
 
 
 
 
 
28
  "ewf-textarea",
29
  "ewf-submit-button",
30
  "ewf-chartbot-output",
31
+ "ewf-python-content-output",
32
+ "ewf-download-html",
33
+ "ewf-html-buffer"
34
  )
35
 
36
  @callback(
 
46
  @callback(
47
  Output("ewf-chartbot-output", "children"),
48
  Output("ewf-python-content-output", "children"),
49
+ Output("ewf-download-html", "style"),
50
+ Output("ewf-html-buffer", "data"),
51
  Input("ewf-submit-button", "n_clicks"),
52
  State("ewf-textarea", "value"),
53
  prevent_initial_call=True
 
58
 
59
  result_output = prompt.get_response(user_prompt, data_top5_csv_string, DATA_FILE_PATH)
60
 
61
+ return helpers.display_response(result_output, DATA_FILE_PATH)
62
+
63
+ @callback(
64
+ Output("ewf-download-html", "href"),
65
+ Input("ewf-html-buffer", "data")
66
+ )
67
+ def download_html(encoded):
68
+ if encoded:
69
+ return f"data:text/html;base64,{encoded}"
70
+ return None
utils/chartbot_dataset_layout.py CHANGED
@@ -14,7 +14,9 @@ def chartbot_common(
14
  prompt_textarea_id: str,
15
  submit_button_id: str,
16
  chartbot_output_id: str,
17
- python_content_id: str
 
 
18
  ) -> tuple:
19
  df = pd.read_csv(csv_file_path)
20
 
@@ -24,6 +26,8 @@ def chartbot_common(
24
  starter_prompts_list.append(components.button_with_prompt(key, value))
25
 
26
  layout = html.Div([
 
 
27
  dmc.Title(page_title, id="dataset", order=1, style={'fontFamily': 'Helvetica'}),
28
 
29
  html.Br(),
@@ -42,13 +46,19 @@ def chartbot_common(
42
  html.Br(),
43
 
44
  dmc.Group([
45
- dmc.Textarea(placeholder="Type the prompt here ...", id=prompt_textarea_id, size="lg", w=1470),
46
  dmc.Button("Submit", id=submit_button_id, color="#E71316", className="float-end")
47
  ]),
48
 
49
  dbc.Spinner([
50
  dmc.Text(id="chart", style={"scrollMarginTop": "60px"}),
51
  html.Div(id=chartbot_output_id),
 
 
 
 
 
 
52
  dmc.Text(id="python-code", style={"scrollMarginTop": "80px"}),
53
  dcc.Markdown(id=python_content_id)
54
  ], color="#E71316")
 
14
  prompt_textarea_id: str,
15
  submit_button_id: str,
16
  chartbot_output_id: str,
17
+ python_content_id: str,
18
+ download_html_button_id: str,
19
+ html_buffer_id: str
20
  ) -> tuple:
21
  df = pd.read_csv(csv_file_path)
22
 
 
26
  starter_prompts_list.append(components.button_with_prompt(key, value))
27
 
28
  layout = html.Div([
29
+ dcc.Store(id=html_buffer_id),
30
+
31
  dmc.Title(page_title, id="dataset", order=1, style={'fontFamily': 'Helvetica'}),
32
 
33
  html.Br(),
 
46
  html.Br(),
47
 
48
  dmc.Group([
49
+ dmc.Textarea(placeholder="Type the prompt here ...", id=prompt_textarea_id, size="lg", w=1170),
50
  dmc.Button("Submit", id=submit_button_id, color="#E71316", className="float-end")
51
  ]),
52
 
53
  dbc.Spinner([
54
  dmc.Text(id="chart", style={"scrollMarginTop": "60px"}),
55
  html.Div(id=chartbot_output_id),
56
+ html.A(
57
+ dmc.Button("Download as HTML", color="#54545C", className="float-end"),
58
+ id=download_html_button_id,
59
+ download="plotly_graph.html",
60
+ style={"display": "none"}
61
+ ),
62
  dmc.Text(id="python-code", style={"scrollMarginTop": "80px"}),
63
  dcc.Markdown(id=python_content_id)
64
  ], color="#E71316")
utils/helpers.py CHANGED
@@ -6,6 +6,7 @@ from dash import html, dcc
6
  import io
7
  import re
8
  import base64
 
9
 
10
  # libraries to help with the Dash app, layout, and callbacks
11
  import dash_ag_grid as dag
@@ -46,10 +47,17 @@ def display_response(response, file_name):
46
  cleaned_code = re.sub(r'(?m)^\s*fig\.show\(\)\s*$', '', code_block)
47
  fig = get_fig_from_code(cleaned_code, file_name)
48
 
49
- return dcc.Graph(figure=fig), response
 
 
 
 
 
 
 
50
 
51
  else:
52
- return "", response
53
 
54
  # Function to parse the contents of the uploaded file
55
  def parse_contents(contents, filename):
 
6
  import io
7
  import re
8
  import base64
9
+ from base64 import b64encode
10
 
11
  # libraries to help with the Dash app, layout, and callbacks
12
  import dash_ag_grid as dag
 
47
  cleaned_code = re.sub(r'(?m)^\s*fig\.show\(\)\s*$', '', code_block)
48
  fig = get_fig_from_code(cleaned_code, file_name)
49
 
50
+ buffer = io.StringIO()
51
+ fig.write_html(buffer)
52
+ html_bytes = buffer.getvalue().encode()
53
+ encoded = b64encode(html_bytes).decode()
54
+ #print(fig)
55
+ #print(type(fig))
56
+
57
+ return dcc.Graph(figure=fig), response, {"display": "block"}, encoded
58
 
59
  else:
60
+ return "", response, {"display": "none"}, None
61
 
62
  # Function to parse the contents of the uploaded file
63
  def parse_contents(contents, filename):