databricks-ddbxutils 0.1.0__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,104 @@
1
+ Metadata-Version: 2.1
2
+ Name: databricks-ddbxutils
3
+ Version: 0.2.0
4
+ Summary: extends databricks dbutils
5
+ Author: Haneul Kim
6
+ Author-email: haneul.kim@data-dynamics.io
7
+ Requires-Python: >=3.11,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.11
10
+ Classifier: Programming Language :: Python :: 3.12
11
+ Requires-Dist: databricks-sdk (>=0.57.0,<0.58.0)
12
+ Requires-Dist: dotenv (>=0.9.9,<0.10.0)
13
+ Requires-Dist: jinja2 (>=3.1.6,<4.0.0)
14
+ Requires-Dist: python-dateutil (>=2.9.0.post0,<3.0.0)
15
+ Description-Content-Type: text/markdown
16
+
17
+ # databricks-ddbxutils
18
+
19
+ dbutils 로 부족한 부분을 확장한 ddbxutils
20
+
21
+ ## Feature
22
+
23
+ * [x] `dbutils.widgets` 에 jinja2 template 적용
24
+
25
+ ## setup
26
+
27
+ ```shell
28
+ cd <PROJECT_ROOT>
29
+ pip install poetry
30
+ ```
31
+
32
+ ## venv
33
+
34
+ ```shell
35
+ poetry shell
36
+ ```
37
+
38
+ ## Build
39
+
40
+ ```shell
41
+ poetry build
42
+ ```
43
+
44
+ ## Run
45
+
46
+ ### in databricks w/o init_script(= Serverless)
47
+
48
+ * Add Wheel
49
+ * wheel upload 용 Volume 생성 후 upload
50
+ * `/Volumes/<CATALOG>/<DATABASE>/<VOLUME_NAME>/ddbxutils-<VERSION>-py3-none-any.whl`
51
+ * notebook 의 우측 Environment 에서 Environment version 2로 지정 후 volume 에 upload 한 wheel file 추가 후 Apply
52
+ * Usage
53
+ ```python
54
+ # dbutils.widgets.text('rawdate', '2025-05-24', 'Raw Date')
55
+ # dbutils.widgets.text('next_day', '{{add_days(rawdate, "%Y-%m-%d", "", 1)}}', 'Next Day')
56
+ import ddbxutils
57
+ next_day = ddbxutils.widgets.get('next_day')
58
+ # next_day: 2025-05-25
59
+ ```
60
+
61
+ ### in databricks w/ init_script
62
+
63
+ * Add Wheel
64
+ * wheel upload 용 Volume 생성 후 upload
65
+ * `/Volumes/<CATALOG>/<DATABASE>/<VOLUME_NAME>/ddbxutils-<VERSION>-py3-none-any.whl`
66
+ * Libraries
67
+ * `/Volumes/<CATALOG>/<DATABASE>/<VOLUME_NAME>/ddbxutils-<VERSION>-py3-none-any.whl`
68
+ * `/Volumes/<CATALOG>/<DATABASE>/<VOLUME_NAME>/init_script_ddbxutils.sh`
69
+ ```shell
70
+ #! /bin/bash
71
+
72
+ STARTUP_SCRIPT=/tmp/pyspark_startup.py
73
+
74
+ cat >> ${STARTUP_SCRIPT} << EOF
75
+
76
+ prefix = 'PYTHONSTARTUP_ddbxutils'
77
+ print(f'{prefix} custom startup script loading...')
78
+ try:
79
+ import ddbxutils
80
+ print(f'{prefix} Custom modules [ddbxutils] are loaded.')
81
+ except Exception as e:
82
+ print(f'{prefix} e={e}')
83
+ print(f'{prefix} import ddbxutils failed')
84
+ EOF
85
+ ```
86
+ * Spark config
87
+ ```text
88
+ spark.executorEnv.PYTHONSTARTUP /tmp/pyspark_startup.py
89
+ ```
90
+ * Environment variables
91
+ ```shell
92
+ PYTHONSTARTUP=/tmp/pyspark_startup.py
93
+ ```
94
+ * Init scripts
95
+ ```text
96
+ /Volumes/<CATALOG>/<DATABASE>/<VOLUME_NAME>/init_script_ddbxutils.sh
97
+ ```
98
+ * Usage
99
+ ```python
100
+ # dbutils.widgets.text('rawdate', '2025-05-24', 'Raw Date')
101
+ # dbutils.widgets.text('next_day', '{{add_days(rawdate, "%Y-%m-%d", "", 1)}}', 'Next Day')
102
+ next_day = ddbxutils.widgets.get('next_day')
103
+ # next_day: 2025-05-25
104
+ ```
@@ -0,0 +1,88 @@
1
+ # databricks-ddbxutils
2
+
3
+ dbutils 로 부족한 부분을 확장한 ddbxutils
4
+
5
+ ## Feature
6
+
7
+ * [x] `dbutils.widgets` 에 jinja2 template 적용
8
+
9
+ ## setup
10
+
11
+ ```shell
12
+ cd <PROJECT_ROOT>
13
+ pip install poetry
14
+ ```
15
+
16
+ ## venv
17
+
18
+ ```shell
19
+ poetry shell
20
+ ```
21
+
22
+ ## Build
23
+
24
+ ```shell
25
+ poetry build
26
+ ```
27
+
28
+ ## Run
29
+
30
+ ### in databricks w/o init_script(= Serverless)
31
+
32
+ * Add Wheel
33
+ * wheel upload 용 Volume 생성 후 upload
34
+ * `/Volumes/<CATALOG>/<DATABASE>/<VOLUME_NAME>/ddbxutils-<VERSION>-py3-none-any.whl`
35
+ * notebook 의 우측 Environment 에서 Environment version 2로 지정 후 volume 에 upload 한 wheel file 추가 후 Apply
36
+ * Usage
37
+ ```python
38
+ # dbutils.widgets.text('rawdate', '2025-05-24', 'Raw Date')
39
+ # dbutils.widgets.text('next_day', '{{add_days(rawdate, "%Y-%m-%d", "", 1)}}', 'Next Day')
40
+ import ddbxutils
41
+ next_day = ddbxutils.widgets.get('next_day')
42
+ # next_day: 2025-05-25
43
+ ```
44
+
45
+ ### in databricks w/ init_script
46
+
47
+ * Add Wheel
48
+ * wheel upload 용 Volume 생성 후 upload
49
+ * `/Volumes/<CATALOG>/<DATABASE>/<VOLUME_NAME>/ddbxutils-<VERSION>-py3-none-any.whl`
50
+ * Libraries
51
+ * `/Volumes/<CATALOG>/<DATABASE>/<VOLUME_NAME>/ddbxutils-<VERSION>-py3-none-any.whl`
52
+ * `/Volumes/<CATALOG>/<DATABASE>/<VOLUME_NAME>/init_script_ddbxutils.sh`
53
+ ```shell
54
+ #! /bin/bash
55
+
56
+ STARTUP_SCRIPT=/tmp/pyspark_startup.py
57
+
58
+ cat >> ${STARTUP_SCRIPT} << EOF
59
+
60
+ prefix = 'PYTHONSTARTUP_ddbxutils'
61
+ print(f'{prefix} custom startup script loading...')
62
+ try:
63
+ import ddbxutils
64
+ print(f'{prefix} Custom modules [ddbxutils] are loaded.')
65
+ except Exception as e:
66
+ print(f'{prefix} e={e}')
67
+ print(f'{prefix} import ddbxutils failed')
68
+ EOF
69
+ ```
70
+ * Spark config
71
+ ```text
72
+ spark.executorEnv.PYTHONSTARTUP /tmp/pyspark_startup.py
73
+ ```
74
+ * Environment variables
75
+ ```shell
76
+ PYTHONSTARTUP=/tmp/pyspark_startup.py
77
+ ```
78
+ * Init scripts
79
+ ```text
80
+ /Volumes/<CATALOG>/<DATABASE>/<VOLUME_NAME>/init_script_ddbxutils.sh
81
+ ```
82
+ * Usage
83
+ ```python
84
+ # dbutils.widgets.text('rawdate', '2025-05-24', 'Raw Date')
85
+ # dbutils.widgets.text('next_day', '{{add_days(rawdate, "%Y-%m-%d", "", 1)}}', 'Next Day')
86
+ next_day = ddbxutils.widgets.get('next_day')
87
+ # next_day: 2025-05-25
88
+ ```
@@ -0,0 +1,4 @@
1
+ from . import widgets
2
+
3
+ # lazy evaluation 위해 () 로 감싸서 generator 를 return 하도록 변경
4
+ generator = (widgets.get_instance() for x in range(1))
@@ -0,0 +1,14 @@
1
+ # 직접 만든 ddbxutils 모듈 임포트
2
+ import ddbxutils
3
+
4
+ # 'next_day' 위젯의 기본값 가져오기
5
+ initial_value = ddbxutils.widgets.get('next_day')
6
+ print(f'초기 next_day 값: {initial_value}')
7
+
8
+ # 변경된 값 다시 가져오기
9
+ updated_value = ddbxutils.widgets.get('next_day')
10
+ print(f'업데이트된 next_day 값: {updated_value}')
11
+
12
+ # 존재하지 않는 위젯 가져오기
13
+ other_value = ddbxutils.widgets.get('another_widget')
14
+ print(f'another_widget 값: {other_value}')
@@ -0,0 +1,45 @@
1
+ from .core import WidgetImpl
2
+
3
+ _widget_impl_instance: WidgetImpl = None
4
+
5
+
6
+ def get_instance():
7
+ """
8
+ widgets 모듈을 초기화합니다.
9
+ 이 함수는 반드시 dbutils 객체와 함께 한 번 호출되어야 합니다.
10
+
11
+ :return: None
12
+ """
13
+ global _widget_impl_instance
14
+ if _widget_impl_instance is None:
15
+ _widget_impl_instance = WidgetImpl()
16
+ return _widget_impl_instance
17
+
18
+
19
+ def get(widget_name: str):
20
+ """
21
+ 초기화된 인스턴스에서 위젯 값을 가져옵니다.
22
+ init()이 호출되지 않았다면 예외를 발생시킵니다.
23
+
24
+ :param widget_name: widget key
25
+ :return: resolved widget value
26
+ """
27
+ widget_impl = get_instance()
28
+ # if _widget_impl_instance is None:
29
+ # raise RuntimeError('ddbxutils.widgets가 초기화되지 않았습니다. `ddbxutils.widgets.init(dbutils)`를 먼저 호출하세요.')
30
+ return widget_impl.get(widget_name)
31
+
32
+
33
+ def refresh():
34
+ """
35
+ 위젯 값을 새로 고칩니다.
36
+
37
+ :param dbutils: dbutils
38
+ :return: None
39
+ """
40
+ widget_impl = get_instance()
41
+ # if _widget_impl_instance is None:
42
+ # raise RuntimeError('ddbxutils.widgets가 초기화되지 않았습니다. `ddbxutils.widgets.init(dbutils)`를 먼저 호출하세요.')
43
+ # if dbutils is None:
44
+ # raise RuntimeError('dbutils is required.')
45
+ widget_impl.refresh()
@@ -1,6 +1,8 @@
1
- from ddbxutils.functions import add_days, add_datetime
1
+ from databricks.sdk import WorkspaceClient
2
2
  from jinja2 import Environment
3
3
 
4
+ from ddbxutils.functions import add_days, add_datetime
5
+
4
6
  environment = Environment()
5
7
  environment.globals['add_days'] = add_days
6
8
  environment.globals['add_datetime'] = add_datetime
@@ -10,14 +12,15 @@ class WidgetImpl:
10
12
  dbutils = None
11
13
  rendered_widget_values = None
12
14
 
13
- def __init__(self, dbutils):
14
- self.refresh(dbutils)
15
+ def __init__(self):
16
+ self.refresh()
15
17
 
16
- def refresh(self, dbutils):
18
+ def refresh(self):
17
19
  """
18
20
  위젯의 값을 설정하거나 추가합니다.
19
21
  """
20
- self.dbutils = dbutils
22
+ if self.dbutils is None:
23
+ self.dbutils = WorkspaceClient().dbutils
21
24
  widget_values = self.dbutils.widgets.getAll()
22
25
  self.rendered_widget_values = {key: environment.from_string(value).render(widget_values) for key, value in widget_values.items()}
23
26
 
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "databricks-ddbxutils"
3
- version = "0.1.0"
3
+ version = "0.2.0"
4
4
  description = "extends databricks dbutils"
5
5
  authors = ["Haneul Kim <haneul.kim@data-dynamics.io>"]
6
6
  readme = "README.md"
@@ -1,56 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: databricks-ddbxutils
3
- Version: 0.1.0
4
- Summary: extends databricks dbutils
5
- Author: Haneul Kim
6
- Author-email: haneul.kim@data-dynamics.io
7
- Requires-Python: >=3.11,<4.0
8
- Classifier: Programming Language :: Python :: 3
9
- Classifier: Programming Language :: Python :: 3.11
10
- Classifier: Programming Language :: Python :: 3.12
11
- Requires-Dist: databricks-sdk (>=0.57.0,<0.58.0)
12
- Requires-Dist: dotenv (>=0.9.9,<0.10.0)
13
- Requires-Dist: jinja2 (>=3.1.6,<4.0.0)
14
- Requires-Dist: python-dateutil (>=2.9.0.post0,<3.0.0)
15
- Description-Content-Type: text/markdown
16
-
17
- # databricks-ddbxutils
18
-
19
- dbutils 로 부족한 부분을 확장한 ddbxutils
20
-
21
- ## Feature
22
-
23
- * [x] `dbutils.widgets` 에 jinja2 template 적용
24
-
25
- ## setup
26
-
27
- ```shell
28
- cd <PROJECT_ROOT>
29
- pip install poetry
30
- ```
31
-
32
- ## venv
33
-
34
- ```shell
35
- poetry shell
36
- ```
37
-
38
- ## Build
39
-
40
- ```shell
41
- poetry build
42
- ```
43
-
44
- ## Run
45
-
46
- ### in databricks w/o init_script
47
-
48
- * Add Wheel
49
- * wheel upload 용 Volume 생성 후 upload
50
- * `/Volumes/<CATALOG>/<DATABASE>/<VOLUME_NAME>/ddbxutils-<VERSION>-py3-none-any.whl`
51
- * notebook 의 우측 Environment 에서 Environment version 2로 지정 후 volume 에 upload 한 wheel file 추가 후 Apply
52
-
53
- ### in databricks w/ init_script
54
-
55
- [//]: # (TODO)
56
-
@@ -1,39 +0,0 @@
1
- # databricks-ddbxutils
2
-
3
- dbutils 로 부족한 부분을 확장한 ddbxutils
4
-
5
- ## Feature
6
-
7
- * [x] `dbutils.widgets` 에 jinja2 template 적용
8
-
9
- ## setup
10
-
11
- ```shell
12
- cd <PROJECT_ROOT>
13
- pip install poetry
14
- ```
15
-
16
- ## venv
17
-
18
- ```shell
19
- poetry shell
20
- ```
21
-
22
- ## Build
23
-
24
- ```shell
25
- poetry build
26
- ```
27
-
28
- ## Run
29
-
30
- ### in databricks w/o init_script
31
-
32
- * Add Wheel
33
- * wheel upload 용 Volume 생성 후 upload
34
- * `/Volumes/<CATALOG>/<DATABASE>/<VOLUME_NAME>/ddbxutils-<VERSION>-py3-none-any.whl`
35
- * notebook 의 우측 Environment 에서 Environment version 2로 지정 후 volume 에 upload 한 wheel file 추가 후 Apply
36
-
37
- ### in databricks w/ init_script
38
-
39
- [//]: # (TODO)
@@ -1,6 +0,0 @@
1
- from databricks.sdk import WorkspaceClient
2
-
3
- from . import widgets
4
-
5
- w = WorkspaceClient()
6
- widgets.init(w.dbutils)
@@ -1,14 +0,0 @@
1
- # 직접 만든 ddbxutils 모듈 임포트
2
- import ddbxutils
3
-
4
- # 'next_day' 위젯의 기본값 가져오기
5
- initial_value = ddbxutils.widgets.get("next_day")
6
- print(f"초기 'next_day' 값: {initial_value}")
7
-
8
- # 변경된 값 다시 가져오기
9
- updated_value = ddbxutils.widgets.get("next_day")
10
- print(f"업데이트된 'next_day' 값: {updated_value}")
11
-
12
- # 존재하지 않는 위젯 가져오기
13
- other_value = ddbxutils.widgets.get("another_widget")
14
- print(f"'another_widget' 값: {other_value}")
@@ -1,42 +0,0 @@
1
- from .core import WidgetImpl
2
-
3
- _widget_impl_instance: WidgetImpl = None
4
-
5
-
6
- def init(dbutils):
7
- """
8
- widgets 모듈을 초기화합니다.
9
- 이 함수는 반드시 dbutils 객체와 함께 한 번 호출되어야 합니다.
10
-
11
- :param dbutils: dbutils
12
- :return: None
13
- """
14
- global _widget_impl_instance
15
- _widget_impl_instance = WidgetImpl(dbutils)
16
-
17
-
18
- def get(widget_name: str):
19
- """
20
- 초기화된 인스턴스에서 위젯 값을 가져옵니다.
21
- init()이 호출되지 않았다면 예외를 발생시킵니다.
22
-
23
- :param widget_name: widget key
24
- :return: resolved widget value
25
- """
26
- if _widget_impl_instance is None:
27
- raise RuntimeError('ddbxutils.widgets가 초기화되지 않았습니다. `ddbxutils.widgets.init(dbutils)`를 먼저 호출하세요.')
28
- return _widget_impl_instance.get(widget_name)
29
-
30
-
31
- def refresh(dbutils):
32
- """
33
- 위젯 값을 새로 고칩니다.
34
-
35
- :param dbutils: dbutils
36
- :return: None
37
- """
38
- if _widget_impl_instance is None:
39
- raise RuntimeError('ddbxutils.widgets가 초기화되지 않았습니다. `ddbxutils.widgets.init(dbutils)`를 먼저 호출하세요.')
40
- if dbutils is None:
41
- raise RuntimeError('dbutils is required.')
42
- _widget_impl_instance.refresh(dbutils)