-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathdatabase_explorer.py
199 lines (157 loc) · 4.07 KB
/
database_explorer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# /// script
# requires-python = ">=3.9"
# dependencies = [
# "duckdb==1.1.1",
# "marimo",
# "polars==1.18.0",
# "pyarrow==18.1.0",
# ]
# ///
import marimo
__generated_with = "0.9.1"
app = marimo.App(width="full")
@app.cell
def __():
import os
import duckdb
import marimo as mo
return duckdb, mo, os
@app.cell(hide_code=True)
def __(mo):
mo.md(
r"""
# Database explorer
This notebook lets you explore the contents of a database. Start by providing a database URL.
"""
)
return
@app.cell
def __(mo, os):
database_url = mo.ui.text(
label="Database URL",
full_width=True,
value=os.environ.get("DATABASE_URL", ""),
)
database_url
return (database_url,)
@app.cell
def __(database_url):
import duckdb
if database_url.value:
duckdb.sql(
f"""
INSTALL postgres;
LOAD postgres;
DETACH DATABASE IF EXISTS my_db;
ATTACH DATABASE '{database_url.value}' AS my_db (TYPE postgres, READ_ONLY);
"""
)
return duckdb, my_db
@app.cell
def __(duckdb):
duckdb.sql("SHOW DATABASES").show()
return
@app.cell(hide_code=True)
def __(mo):
mo.md(r"""## Tables""")
return
@app.cell
def __(mo):
_df = mo.sql(
f"""
SHOW ALL TABLES;
"""
)
return
@app.cell(hide_code=True)
def __(mo):
mo.md(r"""## Other meta table functions""")
return
@app.cell
def __():
FUNCTIONS = [
"duckdb_columns()", # columns
"duckdb_constraints()", # constraints
"duckdb_databases()", # lists the databases that are accessible from within the current DuckDB process
"duckdb_dependencies()", # dependencies between objects
"duckdb_extensions()", # extensions
"duckdb_functions()", # functions
"duckdb_indexes()", # secondary indexes
"duckdb_keywords()", # DuckDB's keywords and reserved words
"duckdb_optimizers()", # the available optimization rules in the DuckDB instance
"duckdb_schemas()", # schemas
"duckdb_sequences()", # sequences
"duckdb_settings()", # settings
"duckdb_tables()", # base tables
"duckdb_types()", # data types
"duckdb_views()", # views
"duckdb_temporary_files()", # the temporary files DuckDB has written to disk, to offload data from memory
]
return (FUNCTIONS,)
@app.cell
def __(FUNCTIONS, mo):
function = mo.ui.dropdown(
label="Dropdown",
options=FUNCTIONS,
value=FUNCTIONS[0],
)
function
return (function,)
@app.cell
def __(_, function, mo):
_df = mo.sql(
f"""
SELECT * FROM {function.value} WHERE database_name == 'my_db'
"""
)
return
@app.cell(hide_code=True)
def __(mo):
mo.md(r"""## Interact with your tables""")
return
@app.cell
def __(duckdb, duckdb_tables, mo):
tables = duckdb.execute(
"""
SELECT table_name FROM duckdb_tables() WHERE internal = False;
"""
).df()
table_names = list(tables["table_name"])
mo.accordion({f"Found {len(table_names)} tables": table_names})
return table_names, tables
@app.cell
def __(mo, table_names):
mo.stop(not table_names)
table_select = mo.ui.dropdown(
label="Table",
options=table_names,
)
limit = mo.ui.slider(
label="Limit",
start=100,
stop=10_000,
step=10,
debounce=True,
show_value=True,
)
mo.hstack([table_select, limit]).left()
return limit, table_select
@app.cell
def __(mo, table_select):
mo.stop(not table_select.value)
table_select_value = table_select.value
return (table_select_value,)
@app.cell
def __(limit, mo, table_select_value):
selected_table = mo.sql(
f"""
select * from my_db.{table_select_value} LIMIT {limit.value};
"""
)
return (selected_table,)
@app.cell
def __(mo, selected_table):
mo.ui.data_explorer(selected_table)
return
if __name__ == "__main__":
app.run()