Update main_app.py
Browse files- main_app.py +28 -59
main_app.py
CHANGED
@@ -419,65 +419,34 @@ def _(function_editor, mo, os):
|
|
419 |
if function_editor.value:
|
420 |
# Get the edited code from the function editor
|
421 |
code = function_editor.value['editor']
|
|
|
|
|
422 |
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
def __getattr__(attr):
|
437 |
-
return dummy
|
438 |
-
dummy.__getattr__ = __getattr__
|
439 |
-
|
440 |
-
# Register in sys.modules to handle nested imports
|
441 |
-
sys.modules[name] = dummy
|
442 |
-
|
443 |
-
# Also register parent modules
|
444 |
-
if '.' in name:
|
445 |
-
parts = name.split('.')
|
446 |
-
for i in range(1, len(parts)):
|
447 |
-
parent_name = '.'.join(parts[:i])
|
448 |
-
if parent_name not in sys.modules:
|
449 |
-
parent = types.ModuleType(parent_name)
|
450 |
-
parent.__getattr__ = lambda attr: dummy if attr == parts[i] else parent
|
451 |
-
sys.modules[parent_name] = parent
|
452 |
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
function_name = name
|
465 |
-
break
|
466 |
-
|
467 |
-
if function_name:
|
468 |
-
# Instantiate the deployable function
|
469 |
-
deployable_function = namespace[function_name]
|
470 |
-
# Now deployable_function contains the score function
|
471 |
-
mo.md(f"Created deployable function from '{function_name}'")
|
472 |
-
# Create the directory if it doesn't exist
|
473 |
-
save_dir = "/tmp/notebook_functions"
|
474 |
-
os.makedirs(save_dir, exist_ok=True)
|
475 |
-
# Save the function code to a file
|
476 |
-
file_path = os.path.join(save_dir, f"{function_name}.py")
|
477 |
-
with open(file_path, "w") as f:
|
478 |
-
f.write(code)
|
479 |
-
else:
|
480 |
-
mo.md("No function found in the editor code")
|
481 |
return (
|
482 |
code,
|
483 |
deployable_function,
|
@@ -806,7 +775,7 @@ def _(
|
|
806 |
# If JSON parsing fails, try Python literal evaluation as fallback
|
807 |
function_meta[deployment_client.repository.FunctionMetaNames.SAMPLE_SCORING_INPUT] = ast.literal_eval(sample_input_editor.value)
|
808 |
|
809 |
-
def upload_function(function_meta, use_function_object=
|
810 |
"""
|
811 |
Uploads a Python function to watsonx.ai as a deployable asset.
|
812 |
Parameters:
|
@@ -873,7 +842,7 @@ def _(
|
|
873 |
|
874 |
upload_button = mo.ui.button(
|
875 |
label="Upload Function",
|
876 |
-
on_click=lambda _: upload_function(function_meta, use_function_object=
|
877 |
kind="success",
|
878 |
tooltip="Click to upload function to watsonx.ai"
|
879 |
)
|
|
|
419 |
if function_editor.value:
|
420 |
# Get the edited code from the function editor
|
421 |
code = function_editor.value['editor']
|
422 |
+
|
423 |
+
# Extract function name using AST without executing the code
|
424 |
|
425 |
+
try:
|
426 |
+
# Parse the code to find function definitions
|
427 |
+
parsed_ast = ast.parse(code)
|
428 |
+
function_name = None
|
429 |
+
for node in parsed_ast.body:
|
430 |
+
if isinstance(node, ast.FunctionDef):
|
431 |
+
function_name = node.name
|
432 |
+
break
|
433 |
+
|
434 |
+
if function_name:
|
435 |
+
# Set deployable_function to None since we're not executing the code
|
436 |
+
deployable_function = None
|
437 |
+
mo.md(f"Found function: '{function_name}' (using file path approach)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
438 |
|
439 |
+
# Create the directory if it doesn't exist
|
440 |
+
save_dir = "/tmp/notebook_functions"
|
441 |
+
os.makedirs(save_dir, exist_ok=True)
|
442 |
+
# Save the function code to a file
|
443 |
+
file_path = os.path.join(save_dir, f"{function_name}.py")
|
444 |
+
with open(file_path, "w") as f:
|
445 |
+
f.write(code)
|
446 |
+
else:
|
447 |
+
mo.md("No function found in the editor code")
|
448 |
+
except SyntaxError as e:
|
449 |
+
mo.md(f"Syntax error in function code: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
450 |
return (
|
451 |
code,
|
452 |
deployable_function,
|
|
|
775 |
# If JSON parsing fails, try Python literal evaluation as fallback
|
776 |
function_meta[deployment_client.repository.FunctionMetaNames.SAMPLE_SCORING_INPUT] = ast.literal_eval(sample_input_editor.value)
|
777 |
|
778 |
+
def upload_function(function_meta, use_function_object=False):
|
779 |
"""
|
780 |
Uploads a Python function to watsonx.ai as a deployable asset.
|
781 |
Parameters:
|
|
|
842 |
|
843 |
upload_button = mo.ui.button(
|
844 |
label="Upload Function",
|
845 |
+
on_click=lambda _: upload_function(function_meta, use_function_object=False),
|
846 |
kind="success",
|
847 |
tooltip="Click to upload function to watsonx.ai"
|
848 |
)
|