Update main_app.py
Browse files- main_app.py +24 -1
main_app.py
CHANGED
@@ -419,8 +419,31 @@ 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 |
# Create a namespace to execute the code in
|
423 |
-
namespace = {}
|
424 |
# Execute the code
|
425 |
exec(code, namespace)
|
426 |
|
|
|
419 |
if function_editor.value:
|
420 |
# Get the edited code from the function editor
|
421 |
code = function_editor.value['editor']
|
422 |
+
|
423 |
+
### Safe Import pass - this bypasses the issue of exec(code,namespace) conflicting when a package isn't in this hugging face environment.
|
424 |
+
def safe_import(name, *args, **kwargs):
|
425 |
+
try:
|
426 |
+
return __import__(name, *args, **kwargs)
|
427 |
+
except ImportError:
|
428 |
+
# Return a dummy module
|
429 |
+
class DummyModule:
|
430 |
+
_instance = None
|
431 |
+
|
432 |
+
def __new__(cls):
|
433 |
+
if cls._instance is None:
|
434 |
+
cls._instance = super().__new__(cls)
|
435 |
+
return cls._instance
|
436 |
+
|
437 |
+
def __getattr__(self, name):
|
438 |
+
return self
|
439 |
+
|
440 |
+
def __call__(self, *args, **kwargs):
|
441 |
+
return self
|
442 |
+
|
443 |
+
return DummyModule()
|
444 |
+
|
445 |
# Create a namespace to execute the code in
|
446 |
+
namespace = {'__builtins__': __builtins__, '__import__': safe_import}
|
447 |
# Execute the code
|
448 |
exec(code, namespace)
|
449 |
|