diff --git a/demos/demos_databases_apis/sql/postgres_alchemy_pypyodbc.ipynb b/demos/demos_databases_apis/sql/postgres_alchemy_pypyodbc.ipynb new file mode 100644 index 0000000000..80133ec1df --- /dev/null +++ b/demos/demos_databases_apis/sql/postgres_alchemy_pypyodbc.ipynb @@ -0,0 +1,414 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# SQL Example\n", + "\n", + "Get & plot data from a SQL DB.\n", + "\n", + "* For drivers, this notebook uses pure Python ODBC (`pypyodbc`) and connects to Postgres\n", + "* Creates a table in the DB if does not exist" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Creds\n", + "\n", + "* Create ODBC connection string\n", + "* We recommend changing `user`/`pwd` to ENV_VARS or files rather than visible hard-coded strings" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "user = \"myadmin\"\n", + "pwd = \"mypassword\"\n", + "server = \"rds-test.zzz.us-west-2.rds.amazonaws.com:5432/postgres\"\n", + "#import os\n", + "#user = os.environ['MY_USER']\n", + "#pwd = os.environ['MY_PWD']\n", + "#server = os.environ['MY_SERVER']\n", + "\n", + "db_string = \"postgres://\" + user + \":\" + pwd + \"@\" + server" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "import graphistry\n", + "#graphistry.register(key='MY_API_KEY', server='my.server.com')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Optional: Install ODBC drivers for Postgres" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Optional: Install a faster custom ODBC driver by logging in to jupyter docker as root:\n", + "\n", + "# $ ssh -i key.pem ubuntu@my_public_ip\n", + "# ubuntu@my_private_ip $ docker exec -it -u root graphistry_notebook_1 bash\n", + "# root@601a8af7ea4c $ apt-get update\n", + "# root@601a8af7ea4c $ apt-get install unixodbc\n", + "# root@601a8af7ea4c $ pip3 install pyodbc" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# If not already exists...\n", + "\n", + "!pip3 install wheel -q\n", + "!pip3 install pypyodbc -q\n", + "!pip3 install sqlalchemy -q" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Connect to DB" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import sqlalchemy as db\n", + "from sqlalchemy import create_engine, MetaData, Table, Column, Integer, Float, String\n", + "from sqlalchemy.orm import sessionmaker" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "engine = create_engine(db_string)\n", + "Session = sessionmaker(bind=engine)\n", + "session = Session()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Optional: Prepopulate table \"my_table\"" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['my_table']" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "engine.table_names()" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [], + "source": [ + "if not engine.dialect.has_table(engine, 'my_table'): # If table don't exist, Create.\n", + " metadata = MetaData(engine)\n", + " mytable = Table('my_table', metadata,\n", + " Column('Id', Integer, primary_key=True, nullable=False), \n", + " Column('Country', String),\n", + " Column('Brand', String),\n", + " Column('Price', Float))\n", + " metadata.create_all()\n", + " engine.execute(\n", + " mytable.insert(),\n", + " [{\"Country\": \"c\" + str(x % 2), \"Brand\": \"b_\" + str(x), \"Price\": x * 2} for x in range(0, 10)]) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Get data" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "# rows 10\n" + ] + }, + { + "data": { + "text/html": [ + "
| \n", + " | Id | \n", + "Country | \n", + "Brand | \n", + "Price | \n", + "
|---|---|---|---|---|
| 6 | \n", + "7 | \n", + "c0 | \n", + "b_6 | \n", + "12.0 | \n", + "
| 5 | \n", + "6 | \n", + "c1 | \n", + "b_5 | \n", + "10.0 | \n", + "
| 7 | \n", + "8 | \n", + "c1 | \n", + "b_7 | \n", + "14.0 | \n", + "