From b2e9d5ca3e3c35fe017662720550e8e0de8aa032 Mon Sep 17 00:00:00 2001 From: Nathan Lugg Date: Tue, 12 Nov 2024 12:21:36 +0900 Subject: [PATCH] add notebook with plots --- workspace.ipynb | 227 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100644 workspace.ipynb diff --git a/workspace.ipynb b/workspace.ipynb new file mode 100644 index 000000000..cdfdbff9c --- /dev/null +++ b/workspace.ipynb @@ -0,0 +1,227 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "RendererRegistry.enable('jupyterlab')" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import altair as alt\n", + "import duckdb\n", + "\n", + "alt.renderers.enable(\"jupyterlab\")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "c = duckdb.connect(\"dbt.duckdb\", read_only=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.vegalite.v5+json": { + "$schema": "https://vega.github.io/schema/vega-lite/v5.20.1.json", + "config": { + "view": { + "continuousHeight": 300, + "continuousWidth": 300 + } + }, + "data": { + "name": "data-e9ffe44f4a940c91c142d6703ef79b9b" + }, + "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 + } + ] + }, + "encoding": { + "x": { + "field": "first_order_month", + "timeUnit": "yearmonth", + "type": "temporal" + }, + "y": { + "field": "num_new_customers", + "type": "quantitative" + } + }, + "mark": { + "type": "bar" + } + }, + "text/plain": [ + "\n", + "\n", + "If you see this message, it means the renderer has not been properly enabled\n", + "for the frontend that you are using. For more information, see\n", + "https://altair-viz.github.io/user_guide/display_frontends.html#troubleshooting\n" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t = c.sql(\n", + " \"\"\"\n", + " SELECT *\n", + " FROM fnl_sales_newcustomers\n", + " ORDER BY first_order_month\n", + " \"\"\"\n", + ")\n", + "\n", + "(\n", + " alt.Chart(t)\n", + " .mark_bar()\n", + " .encode(\n", + " x=alt.X(\"yearmonth(first_order_month):T\"),\n", + " y=alt.Y(\"num_new_customers:Q\"),\n", + " )\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.vegalite.v5+json": { + "$schema": "https://vega.github.io/schema/vega-lite/v5.20.1.json", + "config": { + "view": { + "continuousHeight": 300, + "continuousWidth": 300 + } + }, + "data": { + "name": "data-f6fa15d27bf142925fe0f5eb72fd0105" + }, + "datasets": { + "data-f6fa15d27bf142925fe0f5eb72fd0105": [ + { + "customer_id": 1, + "total_returned_amount_aud": 10 + }, + { + "customer_id": 2, + "total_returned_amount_aud": 23 + }, + { + "customer_id": 40, + "total_returned_amount_aud": 3 + }, + { + "customer_id": 64, + "total_returned_amount_aud": 13 + } + ] + }, + "encoding": { + "x": { + "field": "total_returned_amount_aud", + "type": "quantitative" + }, + "y": { + "field": "customer_id", + "sort": "-x", + "type": "ordinal" + } + }, + "mark": { + "type": "bar" + } + }, + "text/plain": [ + "\n", + "\n", + "If you see this message, it means the renderer has not been properly enabled\n", + "for the frontend that you are using. For more information, see\n", + "https://altair-viz.github.io/user_guide/display_frontends.html#troubleshooting\n" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t = c.sql(\n", + " \"\"\"\n", + " SELECT *\n", + " FROM fnl_finance_returnsamout\n", + " WHERE total_returned_amount_aud > 0\n", + " \"\"\"\n", + ")\n", + "\n", + "(\n", + " alt.Chart(t)\n", + " .mark_bar()\n", + " .encode(\n", + " x=alt.X(\"total_returned_amount_aud:Q\"),\n", + " y=alt.Y(\"customer_id:O\", sort=alt.Sort(\"-x\")),\n", + " )\n", + ")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.14" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}