YFDashboard / src /dbt_data_client.py
Jon Solow
Pass in commands to get df rather than getting db conn
bf943a6
raw
history blame
792 Bytes
import duckdb
import os
import subprocess
import streamlit as st
DBT_DIR = os.path.join(os.path.dirname(__file__), "dbt_yfdash")
DUCKDB_PATH = os.path.join(DBT_DIR, "dev.duckdb")
def get_db_conn():
return duckdb.connect(DUCKDB_PATH)
def run_dbt_build():
process = subprocess.Popen(
args=["dbt", "build"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=DBT_DIR,
)
stdout, _ = process.communicate()
st.text("\n".join(stdout.decode().split("\n")[1:][:-1]))
def execute_db_command_df(db_command: str):
with get_db_conn() as db_conn:
return db_conn.sql(db_command).df()
def get_current_tables() -> list[str]:
current_tables_df = execute_db_command_df("SHOW TABLES")
return current_tables_df["name"].tolist()