|
9 | 9 |
|
10 | 10 | logger = logging.getLogger(__name__)
|
11 | 11 | PLATFORM = "DATABRICKS" if "DB_HOME" in os.environ.keys() else "NON_DATABRICKS"
|
| 12 | + |
12 | 13 | """
|
13 | 14 | DB_HOME env variable has been chosen and that's because this variable is a special variable that will be available in DBR.
|
14 | 15 |
|
15 | 16 | This constant is to ensure the correct behaviour of the show and display methods are called based on the platform
|
16 | 17 | where the code is running from.
|
17 | 18 | """
|
18 | 19 |
|
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(): |
30 | 21 | """
|
31 | 22 | This method returns a boolean value signifying whether the environment is a notebook environment
|
32 | 23 | capable of rendering HTML or not.
|
@@ -63,38 +54,28 @@ def display_unavailable(df):
|
63 | 54 | logger.error("'display' method not available in this environment. Use 'show' method instead.")
|
64 | 55 |
|
65 | 56 |
|
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() |
75 | 58 |
|
76 | 59 |
|
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 |
85 | 70 |
|
86 |
| - display = display_improvised |
87 |
| - else: |
88 |
| - display = display_unavailable |
89 |
| -elif __isnotebookenv(): |
| 71 | +elif ENV_BOOLEAN: |
90 | 72 | def display_html_improvised(obj):
|
91 | 73 | if type(obj).__name__ == 'TSDF':
|
92 | 74 | display_html(obj.df)
|
93 | 75 | else:
|
94 | 76 | display_html(obj)
|
95 |
| - |
96 |
| - |
97 | 77 | display = display_html_improvised
|
| 78 | + |
98 | 79 | else:
|
99 | 80 | display = display_unavailable
|
100 | 81 |
|
|
0 commit comments