owenkaplinsky commited on
Commit
6f26de3
·
1 Parent(s): 3e990c1

Change file name; show output names & type

Browse files
project/src/generators/python.js CHANGED
@@ -86,9 +86,9 @@ forBlock['create_mcp'] = function (block, generator) {
86
 
87
  // Create the main function definition
88
  if (typedInputs.length > 0) {
89
- code += `def create_mcp(${typedInputs.join(', ')}):\n out_amt = ${returnValues.length}\n\n${body}${returnStatement}\n`;
90
  } else {
91
- code += `def create_mcp():\n out_amt = ${returnValues.length}\n\n${body || ''}${returnStatement}`;
92
  }
93
 
94
  // Map Python types to Gradio components for inputs
 
86
 
87
  // Create the main function definition
88
  if (typedInputs.length > 0) {
89
+ code += `def create_mcp(${typedInputs.join(', ')}):\n out_amt = ${returnValues.length}\n out_names = ${JSON.stringify(block.outputNames_ || [])}\n out_types = ${JSON.stringify(block.outputTypes_ || [])}\n\n${body}${returnStatement}\n`;
90
  } else {
91
+ code += `def create_mcp():\n out_amt = ${returnValues.length}\n out_names = ${JSON.stringify(block.outputNames_ || [])}\n out_types = ${JSON.stringify(block.outputTypes_ || [])}\n\n${body || ''}${returnStatement}`;
92
  }
93
 
94
  // Map Python types to Gradio components for inputs
project/src/index.js CHANGED
@@ -138,7 +138,7 @@ downloadCodeButton.addEventListener("click", () => {
138
  return;
139
  }
140
 
141
- var filename = "mcp_generated.py";
142
  var element = document.createElement('a');
143
 
144
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(code));
 
138
  return;
139
  }
140
 
141
+ var filename = "app.py";
142
  var element = document.createElement('a');
143
 
144
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(code));
project/test.py CHANGED
@@ -2,8 +2,8 @@ from fastapi import FastAPI, Request
2
  from fastapi.middleware.cors import CORSMiddleware
3
  import gradio as gr
4
  import uvicorn
5
- import re
6
  import os
 
7
 
8
  app = FastAPI()
9
 
@@ -141,17 +141,22 @@ def execute_blockly_logic(user_inputs):
141
 
142
  anno = params[i].annotation
143
  try:
144
- # Convert using the declared type hint
145
  if anno == int:
146
  typed_args.append(int(arg))
147
  elif anno == float:
148
  typed_args.append(float(arg))
149
  elif anno == bool:
150
  typed_args.append(str(arg).lower() in ("true", "1"))
 
 
 
 
 
 
 
151
  elif anno == str or anno == inspect._empty:
152
  typed_args.append(str(arg))
153
  else:
154
- # Unknown or complex type — leave as-is
155
  typed_args.append(arg)
156
  except Exception:
157
  # If conversion fails, pass the raw input
@@ -223,11 +228,30 @@ def build_interface():
223
  out_amt_match = re.search(r'out_amt\s*=\s*(\d+)', latest_blockly_code)
224
  out_amt = int(out_amt_match.group(1)) if out_amt_match else 0
225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  # Update visibility + clear output fields
227
  output_updates = []
228
  for i, field in enumerate(output_fields):
229
  if i < out_amt:
230
- output_updates.append(gr.update(visible=True, label=f"Output {i+1}", value=""))
231
  else:
232
  output_updates.append(gr.update(visible=False, value=""))
233
 
@@ -236,9 +260,13 @@ def build_interface():
236
  for i, field in enumerate(input_fields):
237
  if i < len(params):
238
  param = params[i]
 
 
 
 
239
  updates.append(gr.update(
240
  visible=True,
241
- label=f"{param['name']} ({param['type']})",
242
  value=""
243
  ))
244
  else:
 
2
  from fastapi.middleware.cors import CORSMiddleware
3
  import gradio as gr
4
  import uvicorn
 
5
  import os
6
+ import ast
7
 
8
  app = FastAPI()
9
 
 
141
 
142
  anno = params[i].annotation
143
  try:
 
144
  if anno == int:
145
  typed_args.append(int(arg))
146
  elif anno == float:
147
  typed_args.append(float(arg))
148
  elif anno == bool:
149
  typed_args.append(str(arg).lower() in ("true", "1"))
150
+ elif anno == list:
151
+ try:
152
+ # Convert string like '["a", "b", "c"]' into an actual list
153
+ typed_args.append(ast.literal_eval(arg))
154
+ except Exception:
155
+ # If parsing fails, wrap it as a single-item list
156
+ typed_args.append([arg])
157
  elif anno == str or anno == inspect._empty:
158
  typed_args.append(str(arg))
159
  else:
 
160
  typed_args.append(arg)
161
  except Exception:
162
  # If conversion fails, pass the raw input
 
228
  out_amt_match = re.search(r'out_amt\s*=\s*(\d+)', latest_blockly_code)
229
  out_amt = int(out_amt_match.group(1)) if out_amt_match else 0
230
 
231
+ out_names_match = re.search(r'out_names\s*=\s*(\[.*?\])', latest_blockly_code, re.DOTALL)
232
+ if out_names_match:
233
+ try:
234
+ out_names = ast.literal_eval(out_names_match.group(1))
235
+ except Exception:
236
+ out_names = []
237
+ else:
238
+ out_names = []
239
+
240
+ out_types_match = re.search(r'out_types\s*=\s*(\[.*?\])', latest_blockly_code, re.DOTALL)
241
+ if out_types_match:
242
+ try:
243
+ out_types = ast.literal_eval(out_types_match.group(1))
244
+ except Exception:
245
+ out_types = []
246
+ else:
247
+ out_types = []
248
+ out_types = ["str" if t == "string" else "int" if t == "integer" else t for t in out_types]
249
+
250
  # Update visibility + clear output fields
251
  output_updates = []
252
  for i, field in enumerate(output_fields):
253
  if i < out_amt:
254
+ output_updates.append(gr.update(visible=True, label=f"{out_names[i]} ({out_types[i]})", value=""))
255
  else:
256
  output_updates.append(gr.update(visible=False, value=""))
257
 
 
260
  for i, field in enumerate(input_fields):
261
  if i < len(params):
262
  param = params[i]
263
+ note = ""
264
+ if param["type"] == "list":
265
+ note = "- (use like: [\"a\", \"b\", ...])"
266
+
267
  updates.append(gr.update(
268
  visible=True,
269
+ label=f"{param['name']} ({param['type']}) {note}",
270
  value=""
271
  ))
272
  else: