cassiebuhler commited on
Commit
08ae6d1
·
1 Parent(s): 5ffe768

chatbot example

Browse files
Files changed (1) hide show
  1. app/sql_example.ipynb +231 -0
app/sql_example.ipynb ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 13,
6
+ "id": "2a04e077-1297-406a-a257-fb0f98566036",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import os\n",
11
+ "import ibis\n",
12
+ "from ibis import _\n",
13
+ "import ibis.selectors as s\n",
14
+ "from cng.utils import *\n",
15
+ "from cng.h3 import *\n",
16
+ "from minio import Minio\n",
17
+ "import streamlit \n",
18
+ "from datetime import timedelta\n",
19
+ "import re\n",
20
+ "duckdb_install_h3()\n",
21
+ "\n",
22
+ "# con = ibis.duckdb.connect(\"duck.db\",extensions = [\"spatial\", \"h3\"])\n",
23
+ "con = ibis.duckdb.connect(extensions = [\"spatial\", \"h3\"])\n",
24
+ "set_secrets(con)\n",
25
+ "\n",
26
+ "# Get signed URLs to access license-controlled layers\n",
27
+ "key = st.secrets[\"MINIO_KEY\"]\n",
28
+ "secret = st.secrets[\"MINIO_SECRET\"]\n",
29
+ "client = Minio(\"minio.carlboettiger.info\", key, secret)\n",
30
+ "\n",
31
+ "mobi_z8 = con.read_parquet(\"https://minio.carlboettiger.info/public-mobi/hex/all-richness-h8.parquet\").select(\"h8\", \"Z\").rename(richness = \"Z\")\n",
32
+ "svi_z8 = con.read_parquet(\"https://minio.carlboettiger.info/public-social-vulnerability/2022/SVI2022_US_tract_h3_z8.parquet\").select(\"h8\", \"svi\").filter(_.svi > 0)\n",
33
+ "carbon_z8 = con.read_parquet(\"https://minio.carlboettiger.info/public-carbon/hex/us-tracts-vuln-total-carbon-2018-h8.parquet\").select('carbon','h8')\n",
34
+ "county_bounds = con.read_parquet(\"https://minio.carlboettiger.info/public-census/2024/county/2024_us_county.parquet\")\n",
35
+ "tpl_z8 = con.read_parquet(\"s3://shared-tpl/conservation_almanac/z8/tpl_h3_z8.parquet\")\n",
36
+ "landvote_z8 = con.read_parquet(\"s3://shared-tpl/landvote/z8/landvote_h3_z8.parquet\")"
37
+ ]
38
+ },
39
+ {
40
+ "cell_type": "code",
41
+ "execution_count": 15,
42
+ "id": "919e66ee-5e41-4e86-a9b9-867dc6bc3d87",
43
+ "metadata": {},
44
+ "outputs": [
45
+ {
46
+ "data": {
47
+ "application/vnd.jupyter.widget-view+json": {
48
+ "model_id": "31265c6efc164d5082af3ed5f855ef2e",
49
+ "version_major": 2,
50
+ "version_minor": 0
51
+ },
52
+ "text/plain": [
53
+ "FloatProgress(value=0.0, layout=Layout(width='auto'), style=ProgressStyle(bar_color='black'))"
54
+ ]
55
+ },
56
+ "metadata": {},
57
+ "output_type": "display_data"
58
+ }
59
+ ],
60
+ "source": [
61
+ "current_tables = con.list_tables()\n",
62
+ "\n",
63
+ "if \"conservation_almanac\" not in set(current_tables):\n",
64
+ " con.create_table(\"conservation_almanac\", tpl_z8)\n",
65
+ "\n",
66
+ "if \"landvote\" not in set(current_tables):\n",
67
+ " con.create_table(\"landvote\", landvote_z8)\n",
68
+ "\n",
69
+ "if \"carbon\" not in set(current_tables):\n",
70
+ " con.create_table(\"carbon\", carbon_z8)\n",
71
+ "\n",
72
+ "if \"mobi\" not in set(current_tables):\n",
73
+ " con.create_table(\"mobi\", mobi_z8)\n",
74
+ "\n",
75
+ "if \"svi\" not in set(current_tables):\n",
76
+ " con.create_table(\"svi\", svi_z8)\n",
77
+ "\n",
78
+ "conservation_almanac = con.table(\"conservation_almanac\")\n",
79
+ "landvote = con.table(\"landvote\")\n",
80
+ "carbon = con.table(\"carbon\")\n",
81
+ "mobi = con.table(\"mobi\")\n",
82
+ "svi = con.table(\"svi\")\n"
83
+ ]
84
+ },
85
+ {
86
+ "cell_type": "code",
87
+ "execution_count": 12,
88
+ "id": "78a7680e-a2ba-4e4a-ab90-c703ebda74b6",
89
+ "metadata": {},
90
+ "outputs": [
91
+ {
92
+ "data": {
93
+ "text/html": [
94
+ "<div>\n",
95
+ "<style scoped>\n",
96
+ " .dataframe tbody tr th:only-of-type {\n",
97
+ " vertical-align: middle;\n",
98
+ " }\n",
99
+ "\n",
100
+ " .dataframe tbody tr th {\n",
101
+ " vertical-align: top;\n",
102
+ " }\n",
103
+ "\n",
104
+ " .dataframe thead th {\n",
105
+ " text-align: right;\n",
106
+ " }\n",
107
+ "</style>\n",
108
+ "<table border=\"1\" class=\"dataframe\">\n",
109
+ " <thead>\n",
110
+ " <tr style=\"text-align: right;\">\n",
111
+ " <th></th>\n",
112
+ " <th>fid</th>\n",
113
+ " <th>mean_carbon</th>\n",
114
+ " </tr>\n",
115
+ " </thead>\n",
116
+ " <tbody>\n",
117
+ " <tr>\n",
118
+ " <th>0</th>\n",
119
+ " <td>12347</td>\n",
120
+ " <td>484.333333</td>\n",
121
+ " </tr>\n",
122
+ " <tr>\n",
123
+ " <th>1</th>\n",
124
+ " <td>21514</td>\n",
125
+ " <td>440.160714</td>\n",
126
+ " </tr>\n",
127
+ " <tr>\n",
128
+ " <th>2</th>\n",
129
+ " <td>14997</td>\n",
130
+ " <td>440.160714</td>\n",
131
+ " </tr>\n",
132
+ " <tr>\n",
133
+ " <th>3</th>\n",
134
+ " <td>29331</td>\n",
135
+ " <td>430.705128</td>\n",
136
+ " </tr>\n",
137
+ " <tr>\n",
138
+ " <th>4</th>\n",
139
+ " <td>29333</td>\n",
140
+ " <td>388.551515</td>\n",
141
+ " </tr>\n",
142
+ " <tr>\n",
143
+ " <th>5</th>\n",
144
+ " <td>29245</td>\n",
145
+ " <td>366.432727</td>\n",
146
+ " </tr>\n",
147
+ " <tr>\n",
148
+ " <th>6</th>\n",
149
+ " <td>25881</td>\n",
150
+ " <td>339.250000</td>\n",
151
+ " </tr>\n",
152
+ " <tr>\n",
153
+ " <th>7</th>\n",
154
+ " <td>24728</td>\n",
155
+ " <td>339.000000</td>\n",
156
+ " </tr>\n",
157
+ " <tr>\n",
158
+ " <th>8</th>\n",
159
+ " <td>24727</td>\n",
160
+ " <td>339.000000</td>\n",
161
+ " </tr>\n",
162
+ " <tr>\n",
163
+ " <th>9</th>\n",
164
+ " <td>29326</td>\n",
165
+ " <td>333.180000</td>\n",
166
+ " </tr>\n",
167
+ " </tbody>\n",
168
+ "</table>\n",
169
+ "</div>"
170
+ ],
171
+ "text/plain": [
172
+ " fid mean_carbon\n",
173
+ "0 12347 484.333333\n",
174
+ "1 21514 440.160714\n",
175
+ "2 14997 440.160714\n",
176
+ "3 29331 430.705128\n",
177
+ "4 29333 388.551515\n",
178
+ "5 29245 366.432727\n",
179
+ "6 25881 339.250000\n",
180
+ "7 24728 339.000000\n",
181
+ "8 24727 339.000000\n",
182
+ "9 29326 333.180000"
183
+ ]
184
+ },
185
+ "execution_count": 12,
186
+ "metadata": {},
187
+ "output_type": "execute_result"
188
+ }
189
+ ],
190
+ "source": [
191
+ "con.sql('''\n",
192
+ "SELECT fid, AVG(carbon) as mean_carbon\n",
193
+ " FROM conservation_almanac\n",
194
+ " LEFT JOIN carbon\n",
195
+ " USING (h8)\n",
196
+ " GROUP BY fid\n",
197
+ " ORDER BY mean_carbon DESC LIMIT 10;\n",
198
+ " ''').execute()"
199
+ ]
200
+ },
201
+ {
202
+ "cell_type": "code",
203
+ "execution_count": null,
204
+ "id": "93796006-8203-496d-8288-2c0156ea22bc",
205
+ "metadata": {},
206
+ "outputs": [],
207
+ "source": []
208
+ }
209
+ ],
210
+ "metadata": {
211
+ "kernelspec": {
212
+ "display_name": "Python 3 (ipykernel)",
213
+ "language": "python",
214
+ "name": "python3"
215
+ },
216
+ "language_info": {
217
+ "codemirror_mode": {
218
+ "name": "ipython",
219
+ "version": 3
220
+ },
221
+ "file_extension": ".py",
222
+ "mimetype": "text/x-python",
223
+ "name": "python",
224
+ "nbconvert_exporter": "python",
225
+ "pygments_lexer": "ipython3",
226
+ "version": "3.12.10"
227
+ }
228
+ },
229
+ "nbformat": 4,
230
+ "nbformat_minor": 5
231
+ }