neuralworm commited on
Commit
287228a
·
1 Parent(s): 084e807
Files changed (1) hide show
  1. app.py +35 -17
app.py CHANGED
@@ -9,6 +9,9 @@ from datetime import datetime, timedelta
9
  import json
10
  import inflect
11
 
 
 
 
12
  # --- Helper Functions ---
13
  def calculate_gematria_sum(text):
14
  """Calculates the gematria sum of a given text."""
@@ -166,13 +169,33 @@ def perform_calculation(start_date, end_date, include_words, min_results):
166
 
167
  def download_json(json_data, start_date, end_date):
168
  """
169
- Nimmt das Dictionary aus json_data (von gr.JSON), verwandelt es in einen JSON-String
170
- und gibt es zum Downloaden als .json zurück.
 
 
171
  """
 
172
  filename = f"gematria_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}.json"
173
- # Hier wandeln wir das Dictionary in einen String um
 
174
  json_string = json.dumps(json_data, indent=4, ensure_ascii=False)
175
- return gr.File.update(value=json_string, filename=filename)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
 
177
  # --- Main Gradio App ---
178
  with gr.Blocks() as app:
@@ -181,21 +204,16 @@ with gr.Blocks() as app:
181
  end_date = Calendar(type="datetime", label="End Date")
182
 
183
  with gr.Row():
184
- include_date_words = gr.Checkbox(
185
- value=True, label="Include Date-Words in JSON"
186
- )
187
- filter_results = gr.Checkbox(
188
- value=False, label="Filter to sums with at least"
189
- )
190
- min_results_input = gr.Number(
191
- value=2, label="results", interactive=True, precision=0
192
- )
193
 
194
  calculate_btn = gr.Button("Calculate Gematria for Date Range")
195
- json_output = gr.JSON(label="JSON Output") # Präsentation als JSON
196
  download_btn = gr.Button("Download JSON")
197
  json_file = gr.File(label="Downloaded JSON")
198
 
 
199
  # Damit wir den Wert von filter_results berücksichtigen können:
200
  def handle_calculate(start_val, end_val, include_val, filter_val, min_val):
201
  if not filter_val: # Checkbox nicht gesetzt
@@ -215,13 +233,13 @@ with gr.Blocks() as app:
215
  api_name="calculate_gematria"
216
  )
217
 
 
218
  download_btn.click(
219
- download_json,
220
  inputs=[json_output, start_date, end_date],
221
- outputs=[json_file]
222
  )
223
 
224
- # Aktiviert/Deaktiviert die Eingabe für min_results
225
  filter_results.change(
226
  lambda checked: gr.update(interactive=checked),
227
  inputs=filter_results,
 
9
  import json
10
  import inflect
11
 
12
+ import os
13
+ import tempfile
14
+
15
  # --- Helper Functions ---
16
  def calculate_gematria_sum(text):
17
  """Calculates the gematria sum of a given text."""
 
169
 
170
  def download_json(json_data, start_date, end_date):
171
  """
172
+ Nimmt das JSON-Dict (aus gr.JSON) und legt es als Datei ab,
173
+ deren Pfad wir zurückgeben.
174
+ Ältere Gradio-Versionen erwarten hier typischerweise einen String
175
+ (also File-Pfad), keinen Tuple oder dict.
176
  """
177
+ # Einen Dateinamen konstruieren – der taucht nur intern auf:
178
  filename = f"gematria_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}.json"
179
+
180
+ # Dictionary -> JSON-String
181
  json_string = json.dumps(json_data, indent=4, ensure_ascii=False)
182
+
183
+ # Temporäre Datei erstellen
184
+ # delete=False => Datei bleibt bestehen, bis wir sie manuell entfernen wollen
185
+ temp = tempfile.NamedTemporaryFile(suffix=".json", delete=False)
186
+ temp_path = temp.name # Pfad merken
187
+
188
+ try:
189
+ # Schreiben als Bytes
190
+ temp.write(json_string.encode("utf-8"))
191
+ temp.flush()
192
+ finally:
193
+ # Datei-Handle schließen
194
+ temp.close()
195
+
196
+ # Jetzt returnen wir den Pfad
197
+ # => Gradio wird damit umgehen können und bietet Download an
198
+ return temp_path
199
 
200
  # --- Main Gradio App ---
201
  with gr.Blocks() as app:
 
204
  end_date = Calendar(type="datetime", label="End Date")
205
 
206
  with gr.Row():
207
+ include_date_words = gr.Checkbox(value=True, label="Include Date-Words in JSON")
208
+ filter_results = gr.Checkbox(value=False, label="Filter to sums with at least")
209
+ min_results_input = gr.Number(value=2, label="results", interactive=True, precision=0)
 
 
 
 
 
 
210
 
211
  calculate_btn = gr.Button("Calculate Gematria for Date Range")
212
+ json_output = gr.JSON(label="JSON Output")
213
  download_btn = gr.Button("Download JSON")
214
  json_file = gr.File(label="Downloaded JSON")
215
 
216
+
217
  # Damit wir den Wert von filter_results berücksichtigen können:
218
  def handle_calculate(start_val, end_val, include_val, filter_val, min_val):
219
  if not filter_val: # Checkbox nicht gesetzt
 
233
  api_name="calculate_gematria"
234
  )
235
 
236
+ # Hier der "fix" im Download-Button
237
  download_btn.click(
238
+ download_json, # unsere neue Funktion
239
  inputs=[json_output, start_date, end_date],
240
+ outputs=[json_file] # gr.File-Element
241
  )
242
 
 
243
  filter_results.change(
244
  lambda checked: gr.update(interactive=checked),
245
  inputs=filter_results,