Skip to content

Commit 095fd97

Browse files
streamling by removing ipython and namespace checks
1 parent 2a3e195 commit 095fd97

File tree

2 files changed

+16
-43
lines changed

2 files changed

+16
-43
lines changed

python/tempo/__init__.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
1-
from tempo.utils import __not_dlt_runtime
2-
31
from tempo.tsdf import TSDF
4-
if __not_dlt_runtime():
5-
from tempo.utils import display
6-
7-
'''
8-
Conditonal import of display so that it doesn't gets imported in runtimes where display is not required.
9-
For example in DATABRICKS Delta Live Tables Runtimes.
10-
'''
2+
from tempo.utils import display

python/tempo/utils.py

+15-34
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,15 @@
99

1010
logger = logging.getLogger(__name__)
1111
PLATFORM = "DATABRICKS" if "DB_HOME" in os.environ.keys() else "NON_DATABRICKS"
12+
1213
"""
1314
DB_HOME env variable has been chosen and that's because this variable is a special variable that will be available in DBR.
1415
1516
This constant is to ensure the correct behaviour of the show and display methods are called based on the platform
1617
where the code is running from.
1718
"""
1819

19-
20-
def __not_dlt_runtime():
21-
if type(get_ipython()) != type(None):
22-
user_namespace_keys = list(get_ipython().user_ns.keys())
23-
check_bool = (('create_dlt_table_fn' not in user_namespace_keys) and ('dlt_sql_fn' not in user_namespace_keys))
24-
return check_bool
25-
else:
26-
return False
27-
28-
29-
def __isnotebookenv():
20+
def __is_capable_of_html_rendering():
3021
"""
3122
This method returns a boolean value signifying whether the environment is a notebook environment
3223
capable of rendering HTML or not.
@@ -63,38 +54,28 @@ def display_unavailable(df):
6354
logger.error("'display' method not available in this environment. Use 'show' method instead.")
6455

6556

66-
ENV_BOOLEAN = __isnotebookenv()
67-
68-
if PLATFORM == "DATABRICKS":
69-
70-
# This below check is for ensuring compatibility with Databricks DLT runtimes
71-
# This if logic ensures that the custom user's namespace of DLT runtimes
72-
# which doesn't have PythonShell's Display object in the namespace doesn't result in an error.
73-
if __not_dlt_runtime():
74-
method = get_ipython().user_ns['display']
57+
ENV_BOOLEAN = __is_capable_of_html_rendering()
7558

7659

77-
# Under 'display' key in user_ns the original databricks display method is present
78-
# to know more refer: /databricks/python_shell/scripts/db_ipykernel_launcher.py
79-
def display_improvised(obj):
80-
if type(obj).__name__ == 'TSDF':
81-
method(obj.df)
82-
else:
83-
method(obj)
84-
60+
if (PLATFORM == "DATABRICKS") and (type(get_ipython()) != type(None)) and ('display' in get_ipython().user_ns.keys()):
61+
method = get_ipython().user_ns['display']
62+
# Under 'display' key in user_ns the original databricks display method is present
63+
# to know more refer: /databricks/python_shell/scripts/db_ipykernel_launcher.py
64+
def display_improvised(obj):
65+
if type(obj).__name__ == 'TSDF':
66+
method(obj.df)
67+
else:
68+
method(obj)
69+
display = display_improvised
8570

86-
display = display_improvised
87-
else:
88-
display = display_unavailable
89-
elif __isnotebookenv():
71+
elif ENV_BOOLEAN:
9072
def display_html_improvised(obj):
9173
if type(obj).__name__ == 'TSDF':
9274
display_html(obj.df)
9375
else:
9476
display_html(obj)
95-
96-
9777
display = display_html_improvised
78+
9879
else:
9980
display = display_unavailable
10081

0 commit comments

Comments
 (0)