Soumik555 commited on
Commit
8cfab71
·
1 Parent(s): 3e98853

timestamp issue

Browse files
Files changed (1) hide show
  1. python_code_executor_service.py +54 -20
python_code_executor_service.py CHANGED
@@ -217,31 +217,65 @@ class PythonExecutor:
217
  raise Exception(f"Failed to save plot: {e}")
218
 
219
  def _format_result(self, result: Any) -> str:
220
- """Format the result for display"""
221
- if isinstance(result, pd.DataFrame):
222
- # Limit DataFrame display to avoid huge outputs
223
- if len(result) > 100:
224
- limited_result = result.head(100)
225
- return f"DataFrame (first 100 of {len(result)} rows):\n{limited_result.to_string()}"
226
- return result.to_string()
227
- elif isinstance(result, pd.Series):
228
- if len(result) > 100:
229
- limited_result = result.head(100)
230
- return f"Series (first 100 of {len(result)} values):\n{limited_result.to_string()}"
231
- return result.to_string()
232
- elif isinstance(result, (dict, list)):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  # Limit large collections
234
  if isinstance(result, dict) and len(result) > 50:
235
  limited = {k: result[k] for k in list(result.keys())[:50]}
236
- return json.dumps(limited, indent=2) + f"\n... (showing first 50 of {len(result)} items)"
237
  elif isinstance(result, list) and len(result) > 50:
238
  limited = result[:50]
239
- return json.dumps(limited, indent=2) + f"\n... (showing first 50 of {len(result)} items)"
240
- return json.dumps(result, indent=2, default=str)
241
- elif hasattr(result, '__str__'):
242
- return str(result)
243
- else:
244
- return repr(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
245
 
246
  def _get_result_variables(self, result_var: Union[str, List[str]]) -> Dict[str, Any]:
247
  """Get result variables from execution locals"""
 
217
  raise Exception(f"Failed to save plot: {e}")
218
 
219
  def _format_result(self, result: Any) -> str:
220
+ """Format the result for display"""
221
+ if isinstance(result, pd.DataFrame):
222
+ # Limit DataFrame display to avoid huge outputs
223
+ if len(result) > 100:
224
+ limited_result = result.head(100)
225
+ return f"DataFrame (first 100 of {len(result)} rows):\n{limited_result.to_string()}"
226
+ return result.to_string()
227
+ elif isinstance(result, pd.Series):
228
+ if len(result) > 100:
229
+ limited_result = result.head(100)
230
+ return f"Series (first 100 of {len(result)} values):\n{limited_result.to_string()}"
231
+ return result.to_string()
232
+ elif isinstance(result, (dict, list)):
233
+ # Custom JSON encoder to handle special types
234
+ def json_serializer(obj):
235
+ """Handle special types that aren't JSON serializable"""
236
+ if isinstance(obj, (pd.Timestamp, datetime)):
237
+ return obj.isoformat()
238
+ elif isinstance(obj, (np.integer, np.int64, np.int32)):
239
+ return int(obj)
240
+ elif isinstance(obj, (np.floating, np.float64, np.float32)):
241
+ return float(obj)
242
+ elif isinstance(obj, np.ndarray):
243
+ return obj.tolist()
244
+ elif isinstance(obj, pd.Series):
245
+ return obj.to_dict()
246
+ elif isinstance(obj, pd.DataFrame):
247
+ return obj.to_dict('records')
248
+ elif hasattr(obj, '__dict__'):
249
+ return str(obj)
250
+ else:
251
+ return str(obj)
252
+
253
+ try:
254
  # Limit large collections
255
  if isinstance(result, dict) and len(result) > 50:
256
  limited = {k: result[k] for k in list(result.keys())[:50]}
257
+ return json.dumps(limited, indent=2, default=json_serializer) + f"\n... (showing first 50 of {len(result)} items)"
258
  elif isinstance(result, list) and len(result) > 50:
259
  limited = result[:50]
260
+ return json.dumps(limited, indent=2, default=json_serializer) + f"\n... (showing first 50 of {len(result)} items)"
261
+ return json.dumps(result, indent=2, default=json_serializer)
262
+ except Exception as e:
263
+ # Fallback to string representation if JSON serialization fails
264
+ return f"Result (JSON serialization failed: {str(e)}):\n{str(result)}"
265
+ elif isinstance(result, (pd.Timestamp, datetime)):
266
+ return result.isoformat()
267
+ elif isinstance(result, (np.integer, np.int64, np.int32)):
268
+ return str(int(result))
269
+ elif isinstance(result, (np.floating, np.float64, np.float32)):
270
+ return str(float(result))
271
+ elif isinstance(result, np.ndarray):
272
+ if result.size > 100:
273
+ return f"Array (first 100 of {result.size} elements):\n{result.flatten()[:100]}"
274
+ return str(result)
275
+ elif hasattr(result, '__str__'):
276
+ return str(result)
277
+ else:
278
+ return repr(result)
279
 
280
  def _get_result_variables(self, result_var: Union[str, List[str]]) -> Dict[str, Any]:
281
  """Get result variables from execution locals"""