|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import altair as alt\n", |
| 10 | + "import duckdb" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 2, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "c = duckdb.connect(\"dbt.duckdb\", read_only=True)" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "code", |
| 24 | + "execution_count": 3, |
| 25 | + "metadata": {}, |
| 26 | + "outputs": [ |
| 27 | + { |
| 28 | + "data": { |
| 29 | + "text/html": [ |
| 30 | + "\n", |
| 31 | + "<style>\n", |
| 32 | + " #altair-viz-a072d4b80d6348bfba9549f394250756.vega-embed {\n", |
| 33 | + " width: 100%;\n", |
| 34 | + " display: flex;\n", |
| 35 | + " }\n", |
| 36 | + "\n", |
| 37 | + " #altair-viz-a072d4b80d6348bfba9549f394250756.vega-embed details,\n", |
| 38 | + " #altair-viz-a072d4b80d6348bfba9549f394250756.vega-embed details summary {\n", |
| 39 | + " position: relative;\n", |
| 40 | + " }\n", |
| 41 | + "</style>\n", |
| 42 | + "<div id=\"altair-viz-a072d4b80d6348bfba9549f394250756\"></div>\n", |
| 43 | + "<script type=\"text/javascript\">\n", |
| 44 | + " var VEGA_DEBUG = (typeof VEGA_DEBUG == \"undefined\") ? {} : VEGA_DEBUG;\n", |
| 45 | + " (function(spec, embedOpt){\n", |
| 46 | + " let outputDiv = document.currentScript.previousElementSibling;\n", |
| 47 | + " if (outputDiv.id !== \"altair-viz-a072d4b80d6348bfba9549f394250756\") {\n", |
| 48 | + " outputDiv = document.getElementById(\"altair-viz-a072d4b80d6348bfba9549f394250756\");\n", |
| 49 | + " }\n", |
| 50 | + " const paths = {\n", |
| 51 | + " \"vega\": \"https://cdn.jsdelivr.net/npm/vega@5?noext\",\n", |
| 52 | + " \"vega-lib\": \"https://cdn.jsdelivr.net/npm/vega-lib?noext\",\n", |
| 53 | + " \"vega-lite\": \"https://cdn.jsdelivr.net/npm/vega-lite@5.20.1?noext\",\n", |
| 54 | + " \"vega-embed\": \"https://cdn.jsdelivr.net/npm/vega-embed@6?noext\",\n", |
| 55 | + " };\n", |
| 56 | + "\n", |
| 57 | + " function maybeLoadScript(lib, version) {\n", |
| 58 | + " var key = `${lib.replace(\"-\", \"\")}_version`;\n", |
| 59 | + " return (VEGA_DEBUG[key] == version) ?\n", |
| 60 | + " Promise.resolve(paths[lib]) :\n", |
| 61 | + " new Promise(function(resolve, reject) {\n", |
| 62 | + " var s = document.createElement('script');\n", |
| 63 | + " document.getElementsByTagName(\"head\")[0].appendChild(s);\n", |
| 64 | + " s.async = true;\n", |
| 65 | + " s.onload = () => {\n", |
| 66 | + " VEGA_DEBUG[key] = version;\n", |
| 67 | + " return resolve(paths[lib]);\n", |
| 68 | + " };\n", |
| 69 | + " s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n", |
| 70 | + " s.src = paths[lib];\n", |
| 71 | + " });\n", |
| 72 | + " }\n", |
| 73 | + "\n", |
| 74 | + " function showError(err) {\n", |
| 75 | + " outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n", |
| 76 | + " throw err;\n", |
| 77 | + " }\n", |
| 78 | + "\n", |
| 79 | + " function displayChart(vegaEmbed) {\n", |
| 80 | + " vegaEmbed(outputDiv, spec, embedOpt)\n", |
| 81 | + " .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n", |
| 82 | + " }\n", |
| 83 | + "\n", |
| 84 | + " if(typeof define === \"function\" && define.amd) {\n", |
| 85 | + " requirejs.config({paths});\n", |
| 86 | + " require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n", |
| 87 | + " } else {\n", |
| 88 | + " maybeLoadScript(\"vega\", \"5\")\n", |
| 89 | + " .then(() => maybeLoadScript(\"vega-lite\", \"5.20.1\"))\n", |
| 90 | + " .then(() => maybeLoadScript(\"vega-embed\", \"6\"))\n", |
| 91 | + " .catch(showError)\n", |
| 92 | + " .then(() => displayChart(vegaEmbed));\n", |
| 93 | + " }\n", |
| 94 | + " })({\"config\": {\"view\": {\"continuousWidth\": 300, \"continuousHeight\": 300}}, \"data\": {\"name\": \"data-e9ffe44f4a940c91c142d6703ef79b9b\"}, \"mark\": {\"type\": \"bar\"}, \"encoding\": {\"x\": {\"field\": \"first_order_month\", \"timeUnit\": \"yearmonth\", \"type\": \"temporal\"}, \"y\": {\"field\": \"num_new_customers\", \"type\": \"quantitative\"}}, \"$schema\": \"https://vega.github.io/schema/vega-lite/v5.20.1.json\", \"datasets\": {\"data-e9ffe44f4a940c91c142d6703ef79b9b\": [{\"first_order_month\": \"2018-01-01T00:00:00\", \"num_new_customers\": 24}, {\"first_order_month\": \"2018-02-01T00:00:00\", \"num_new_customers\": 18}, {\"first_order_month\": \"2018-03-01T00:00:00\", \"num_new_customers\": 18}, {\"first_order_month\": \"2018-04-01T00:00:00\", \"num_new_customers\": 2}]}}, {\"mode\": \"vega-lite\"});\n", |
| 95 | + "</script>" |
| 96 | + ], |
| 97 | + "text/plain": [ |
| 98 | + "alt.Chart(...)" |
| 99 | + ] |
| 100 | + }, |
| 101 | + "execution_count": 3, |
| 102 | + "metadata": {}, |
| 103 | + "output_type": "execute_result" |
| 104 | + } |
| 105 | + ], |
| 106 | + "source": [ |
| 107 | + "t = c.sql(\n", |
| 108 | + " \"\"\"\n", |
| 109 | + " SELECT *\n", |
| 110 | + " FROM fnl_sales_newcustomers\n", |
| 111 | + " ORDER BY first_order_month\n", |
| 112 | + " \"\"\"\n", |
| 113 | + ")\n", |
| 114 | + "\n", |
| 115 | + "(\n", |
| 116 | + " alt.Chart(t)\n", |
| 117 | + " .mark_bar()\n", |
| 118 | + " .encode(\n", |
| 119 | + " x=alt.X(\"yearmonth(first_order_month):T\"),\n", |
| 120 | + " y=alt.Y(\"num_new_customers:Q\"),\n", |
| 121 | + " )\n", |
| 122 | + ")" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "code", |
| 127 | + "execution_count": 4, |
| 128 | + "metadata": {}, |
| 129 | + "outputs": [ |
| 130 | + { |
| 131 | + "data": { |
| 132 | + "text/html": [ |
| 133 | + "\n", |
| 134 | + "<style>\n", |
| 135 | + " #altair-viz-c30e8330bd9c403e9938ea537abc14a1.vega-embed {\n", |
| 136 | + " width: 100%;\n", |
| 137 | + " display: flex;\n", |
| 138 | + " }\n", |
| 139 | + "\n", |
| 140 | + " #altair-viz-c30e8330bd9c403e9938ea537abc14a1.vega-embed details,\n", |
| 141 | + " #altair-viz-c30e8330bd9c403e9938ea537abc14a1.vega-embed details summary {\n", |
| 142 | + " position: relative;\n", |
| 143 | + " }\n", |
| 144 | + "</style>\n", |
| 145 | + "<div id=\"altair-viz-c30e8330bd9c403e9938ea537abc14a1\"></div>\n", |
| 146 | + "<script type=\"text/javascript\">\n", |
| 147 | + " var VEGA_DEBUG = (typeof VEGA_DEBUG == \"undefined\") ? {} : VEGA_DEBUG;\n", |
| 148 | + " (function(spec, embedOpt){\n", |
| 149 | + " let outputDiv = document.currentScript.previousElementSibling;\n", |
| 150 | + " if (outputDiv.id !== \"altair-viz-c30e8330bd9c403e9938ea537abc14a1\") {\n", |
| 151 | + " outputDiv = document.getElementById(\"altair-viz-c30e8330bd9c403e9938ea537abc14a1\");\n", |
| 152 | + " }\n", |
| 153 | + " const paths = {\n", |
| 154 | + " \"vega\": \"https://cdn.jsdelivr.net/npm/vega@5?noext\",\n", |
| 155 | + " \"vega-lib\": \"https://cdn.jsdelivr.net/npm/vega-lib?noext\",\n", |
| 156 | + " \"vega-lite\": \"https://cdn.jsdelivr.net/npm/vega-lite@5.20.1?noext\",\n", |
| 157 | + " \"vega-embed\": \"https://cdn.jsdelivr.net/npm/vega-embed@6?noext\",\n", |
| 158 | + " };\n", |
| 159 | + "\n", |
| 160 | + " function maybeLoadScript(lib, version) {\n", |
| 161 | + " var key = `${lib.replace(\"-\", \"\")}_version`;\n", |
| 162 | + " return (VEGA_DEBUG[key] == version) ?\n", |
| 163 | + " Promise.resolve(paths[lib]) :\n", |
| 164 | + " new Promise(function(resolve, reject) {\n", |
| 165 | + " var s = document.createElement('script');\n", |
| 166 | + " document.getElementsByTagName(\"head\")[0].appendChild(s);\n", |
| 167 | + " s.async = true;\n", |
| 168 | + " s.onload = () => {\n", |
| 169 | + " VEGA_DEBUG[key] = version;\n", |
| 170 | + " return resolve(paths[lib]);\n", |
| 171 | + " };\n", |
| 172 | + " s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n", |
| 173 | + " s.src = paths[lib];\n", |
| 174 | + " });\n", |
| 175 | + " }\n", |
| 176 | + "\n", |
| 177 | + " function showError(err) {\n", |
| 178 | + " outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n", |
| 179 | + " throw err;\n", |
| 180 | + " }\n", |
| 181 | + "\n", |
| 182 | + " function displayChart(vegaEmbed) {\n", |
| 183 | + " vegaEmbed(outputDiv, spec, embedOpt)\n", |
| 184 | + " .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n", |
| 185 | + " }\n", |
| 186 | + "\n", |
| 187 | + " if(typeof define === \"function\" && define.amd) {\n", |
| 188 | + " requirejs.config({paths});\n", |
| 189 | + " require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n", |
| 190 | + " } else {\n", |
| 191 | + " maybeLoadScript(\"vega\", \"5\")\n", |
| 192 | + " .then(() => maybeLoadScript(\"vega-lite\", \"5.20.1\"))\n", |
| 193 | + " .then(() => maybeLoadScript(\"vega-embed\", \"6\"))\n", |
| 194 | + " .catch(showError)\n", |
| 195 | + " .then(() => displayChart(vegaEmbed));\n", |
| 196 | + " }\n", |
| 197 | + " })({\"config\": {\"view\": {\"continuousWidth\": 300, \"continuousHeight\": 300}}, \"data\": {\"name\": \"data-f6fa15d27bf142925fe0f5eb72fd0105\"}, \"mark\": {\"type\": \"bar\"}, \"encoding\": {\"x\": {\"field\": \"total_returned_amount_aud\", \"type\": \"quantitative\"}, \"y\": {\"field\": \"customer_id\", \"sort\": \"-x\", \"type\": \"ordinal\"}}, \"$schema\": \"https://vega.github.io/schema/vega-lite/v5.20.1.json\", \"datasets\": {\"data-f6fa15d27bf142925fe0f5eb72fd0105\": [{\"customer_id\": 1, \"total_returned_amount_aud\": 10.0}, {\"customer_id\": 2, \"total_returned_amount_aud\": 23.0}, {\"customer_id\": 40, \"total_returned_amount_aud\": 3.0}, {\"customer_id\": 64, \"total_returned_amount_aud\": 13.0}]}}, {\"mode\": \"vega-lite\"});\n", |
| 198 | + "</script>" |
| 199 | + ], |
| 200 | + "text/plain": [ |
| 201 | + "alt.Chart(...)" |
| 202 | + ] |
| 203 | + }, |
| 204 | + "execution_count": 4, |
| 205 | + "metadata": {}, |
| 206 | + "output_type": "execute_result" |
| 207 | + } |
| 208 | + ], |
| 209 | + "source": [ |
| 210 | + "t = c.sql(\n", |
| 211 | + " \"\"\"\n", |
| 212 | + " SELECT *\n", |
| 213 | + " FROM fnl_finance_returnsamout\n", |
| 214 | + " WHERE total_returned_amount_aud > 0\n", |
| 215 | + " \"\"\"\n", |
| 216 | + ")\n", |
| 217 | + "\n", |
| 218 | + "(\n", |
| 219 | + " alt.Chart(t)\n", |
| 220 | + " .mark_bar()\n", |
| 221 | + " .encode(\n", |
| 222 | + " x=alt.X(\"total_returned_amount_aud:Q\"),\n", |
| 223 | + " y=alt.Y(\"customer_id:O\", sort=alt.Sort(\"-x\")),\n", |
| 224 | + " )\n", |
| 225 | + ")" |
| 226 | + ] |
| 227 | + } |
| 228 | + ], |
| 229 | + "metadata": { |
| 230 | + "kernelspec": { |
| 231 | + "display_name": ".venv", |
| 232 | + "language": "python", |
| 233 | + "name": "python3" |
| 234 | + }, |
| 235 | + "language_info": { |
| 236 | + "codemirror_mode": { |
| 237 | + "name": "ipython", |
| 238 | + "version": 3 |
| 239 | + }, |
| 240 | + "file_extension": ".py", |
| 241 | + "mimetype": "text/x-python", |
| 242 | + "name": "python", |
| 243 | + "nbconvert_exporter": "python", |
| 244 | + "pygments_lexer": "ipython3", |
| 245 | + "version": "3.10.14" |
| 246 | + } |
| 247 | + }, |
| 248 | + "nbformat": 4, |
| 249 | + "nbformat_minor": 2 |
| 250 | +} |
0 commit comments