Skip to content

Commit 3c7b14e

Browse files
committed
update version
1 parent 2731397 commit 3c7b14e

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

.env.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
DISABLE_DISPLAY_KEYS=false # if true, the display keys will not be shown in the frontend
66
EXEC_PYTHON_IN_SUBPROCESS=false # if true, the python code will be executed in a subprocess to avoid crashing the main app, but it will increase the time of response
77

8+
LOCAL_DB_DIR= # the directory to store the local database, if not provided, the app will use the temp directory
9+
810
# External atabase connection settings
911
# check https://duckdb.org/docs/stable/extensions/mysql.html
1012
# and https://duckdb.org/docs/stable/extensions/postgres.html

py-src/data_formulator/db_manager.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
from dotenv import load_dotenv
1010

1111
class DuckDBManager:
12-
def __init__(self, external_db_connections: Dict[str, Dict[str, Any]]):
12+
def __init__(self, external_db_connections: Dict[str, Dict[str, Any]], local_db_dir: str):
1313
# Store session db file paths
1414
self._db_files: Dict[str, str] = {}
1515

1616
# External db connections and tracking of installed extensions
1717
self._external_db_connections: Dict[str, Dict[str, Any]] = external_db_connections
1818
self._installed_extensions: Dict[str, List[str]] = {}
19+
self._local_db_dir: str = local_db_dir
1920

2021
@contextmanager
2122
def connection(self, session_id: str) -> ContextManager[duckdb.DuckDBPyConnection]:
@@ -33,7 +34,10 @@ def get_connection(self, session_id: str) -> duckdb.DuckDBPyConnection:
3334
"""Internal method to get or create a DuckDB connection for a session"""
3435
# Get or create the db file path for this session
3536
if session_id not in self._db_files or self._db_files[session_id] is None:
36-
db_file = os.path.join(tempfile.gettempdir(), f"df_{session_id}.duckdb")
37+
db_dir = self._local_db_dir if self._local_db_dir else tempfile.gettempdir()
38+
if not os.path.exists(db_dir):
39+
db_dir = tempfile.gettempdir()
40+
db_file = os.path.join(db_dir, f"df_{session_id}.duckdb")
3741
print(f"=== Creating new db file: {db_file}")
3842
self._db_files[session_id] = db_file
3943
# Initialize extension tracking for this file
@@ -73,12 +77,15 @@ def get_connection(self, session_id: str) -> duckdb.DuckDBPyConnection:
7377
env = load_dotenv()
7478

7579
# Initialize the DB manager
76-
db_manager = DuckDBManager({
77-
"db_name": os.getenv('DB_NAME'),
78-
"db_type": os.getenv('DB_TYPE'),
79-
"host": os.getenv('DB_HOST'),
80-
"port": os.getenv('DB_PORT'),
81-
"database": os.getenv('DB_DATABASE'),
82-
"user": os.getenv('DB_USER'),
83-
"password": os.getenv('DB_PASSWORD')
84-
} if os.getenv('USE_EXTERNAL_DB') == 'true' else None)
80+
db_manager = DuckDBManager(
81+
external_db_connections={
82+
"db_name": os.getenv('DB_NAME'),
83+
"db_type": os.getenv('DB_TYPE'),
84+
"host": os.getenv('DB_HOST'),
85+
"port": os.getenv('DB_PORT'),
86+
"database": os.getenv('DB_DATABASE'),
87+
"user": os.getenv('DB_USER'),
88+
"password": os.getenv('DB_PASSWORD')
89+
} if os.getenv('USE_EXTERNAL_DB') == 'true' else None,
90+
local_db_dir=os.getenv('LOCAL_DB_DIR')
91+
)

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "data_formulator"
7-
version = "0.1.7"
7+
version = "0.2"
88

99
requires-python = ">=3.9"
1010
authors = [
@@ -34,7 +34,8 @@ dependencies = [
3434
"azure-keyvault-secrets",
3535
"python-dotenv",
3636
"vega_datasets",
37-
"litellm"
37+
"litellm",
38+
"duckdb"
3839
]
3940

4041
[project.urls]

0 commit comments

Comments
 (0)