JayLacoma commited on
Commit
22834ea
·
verified ·
1 Parent(s): d5427dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -7
app.py CHANGED
@@ -112,9 +112,10 @@ questionnaire = {
112
  },
113
  }
114
 
 
 
115
  import gradio as gr
116
  import plotly.graph_objects as go
117
- import numpy as np
118
  from scipy.stats import percentileofscore
119
 
120
  # Define TRAIT_COLORS
@@ -257,7 +258,22 @@ def start_test():
257
  gr.update(visible=False),
258
  )
259
 
260
- # Next question
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  def next_question(response):
262
  state["responses"].append(int(response))
263
  state["current_question"] += 1
@@ -265,6 +281,11 @@ def next_question(response):
265
  if state["current_question"] >= len(questions):
266
  scores, z_scores, percentiles = compute_scores_and_percentiles(state["responses"])
267
  result_chart = create_chart(scores, z_scores, percentiles)
 
 
 
 
 
268
  return "Test Complete! Your results:", result_chart, gr.update(visible=False), gr.update(visible=True)
269
 
270
  question = questions[state["current_question"]][2]
@@ -278,22 +299,50 @@ def next_question(response):
278
  gr.update(visible=False),
279
  )
280
 
281
- # Create Gradio app
282
  def create_gradio_app():
283
  with gr.Blocks() as app:
284
  gr.Markdown("## Extended Big Five Personality Test")
285
 
 
286
  start_button = gr.Button("Start Test")
287
  question_text = gr.Textbox(label="Question", interactive=False, visible=True)
288
  button_group = gr.Radio([str(i) for i in range(1, 11)], label="Your Response (1-10)", visible=True)
289
  progress_gauge = gr.Plot()
290
  result_output = gr.Textbox(label="Results", visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
291
 
292
- start_button.click(start_test, outputs=[question_text, progress_gauge, button_group, result_output])
293
- button_group.change(next_question, inputs=button_group, outputs=[question_text, progress_gauge, button_group, result_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
294
 
295
  return app
296
 
297
- # Launch Gradio app
 
298
  app = create_gradio_app()
299
- app.launch()
 
 
 
112
  },
113
  }
114
 
115
+ import json
116
+ import numpy as np
117
  import gradio as gr
118
  import plotly.graph_objects as go
 
119
  from scipy.stats import percentileofscore
120
 
121
  # Define TRAIT_COLORS
 
258
  gr.update(visible=False),
259
  )
260
 
261
+
262
+
263
+
264
+
265
+ # Save Plotly chart as HTML
266
+ def save_plotly_html(chart, file_name="results_chart.html"):
267
+ chart.write_html(file_name)
268
+ print(f"Chart saved to {file_name}")
269
+
270
+ # Save test results as a JSON file
271
+ def save_results(responses, file_name="test_results.json"):
272
+ with open(file_name, "w") as f:
273
+ json.dump({"responses": responses}, f, indent=4)
274
+ print(f"Results saved to {file_name}")
275
+
276
+ # Modified next_question to save results and chart
277
  def next_question(response):
278
  state["responses"].append(int(response))
279
  state["current_question"] += 1
 
281
  if state["current_question"] >= len(questions):
282
  scores, z_scores, percentiles = compute_scores_and_percentiles(state["responses"])
283
  result_chart = create_chart(scores, z_scores, percentiles)
284
+
285
+ # Save results and chart
286
+ save_results(state["responses"], "test_results.json")
287
+ save_plotly_html(result_chart, "results_chart.html")
288
+
289
  return "Test Complete! Your results:", result_chart, gr.update(visible=False), gr.update(visible=True)
290
 
291
  question = questions[state["current_question"]][2]
 
299
  gr.update(visible=False),
300
  )
301
 
 
302
  def create_gradio_app():
303
  with gr.Blocks() as app:
304
  gr.Markdown("## Extended Big Five Personality Test")
305
 
306
+ # UI Elements
307
  start_button = gr.Button("Start Test")
308
  question_text = gr.Textbox(label="Question", interactive=False, visible=True)
309
  button_group = gr.Radio([str(i) for i in range(1, 11)], label="Your Response (1-10)", visible=True)
310
  progress_gauge = gr.Plot()
311
  result_output = gr.Textbox(label="Results", visible=True)
312
+ download_results = gr.File(label="Download Results JSON", visible=False)
313
+ download_chart = gr.File(label="Download Chart HTML", visible=True)
314
+
315
+ # Start Test Logic
316
+ start_button.click(
317
+ start_test,
318
+ outputs=[question_text, progress_gauge, button_group, result_output]
319
+ )
320
+
321
+ # Question Logic
322
+ def handle_next_question(response):
323
+ output = next_question(response)
324
 
325
+ # Check if the test is complete to enable downloads
326
+ if state["current_question"] >= len(questions):
327
+ return (*output, "test_results.json", "results_chart.html")
328
+ else:
329
+ return (*output, None, None)
330
+
331
+ # Respond to button changes and enable downloads after test completion
332
+ button_group.change(
333
+ handle_next_question,
334
+ inputs=button_group,
335
+ outputs=[
336
+ question_text, progress_gauge, button_group, result_output,
337
+ download_results, download_chart
338
+ ]
339
+ )
340
 
341
  return app
342
 
343
+
344
+ # Launch the Gradio app
345
  app = create_gradio_app()
346
+
347
+ # Launch the app locally
348
+ app.launch()