Skip to main content

ipython_magic_sqlalchemy_schemadisplay

Magic for sqlalchemy_schemadisplay

Binder

Magic originally created for the Open University module TM351 Data Management and Analysis. This magic complements the ipython-sql, which provides magics for connecting to a SQL database, with a magic that uses sqlalchemy_schemadisplay to generate ERDs over a SQL database. The code for generating the ER diagram can be found in the third party sqlalchemy_schemadisplay package.

At the moment, the schema display and ipython-sql magics and independent, but they really should be combined into a single package.

Usage

Install from PyPi: pip install schemadisplay-magic

Install from this repo:

pip install git+https://github.com/innovationOUtside/ipython_magic_sqlalchemy_schemadisplay.git

Note there are several other dependencies:

  • Python: see requirements.txt
  • O/S: see apt.txt

Install either ipython-sql or jupysql to access the $%sql magic:

  • either ipython-sql, or
  • pip install jupysql

Set up a database. For example, load the SQL magic:

%load_ext sql

Create a database connection string — we can use a SQLite database for demo purposes — and connect the SQL magic to the database:

# With ipython-sql:
DB_CONNECTION = 'sqlite:///./test.db'
%sql $DB_CONNECTION

# With jupysql:
from sqlalchemy import create_engine
engine = create_engine(DB_CONNECTION)
%sql engine

Populate the database with a couple of foreign key related tables:

%%sql

DROP TABLE IF EXISTS doctor;

CREATE TABLE doctor (
    
    doctor_id CHAR(4),
    doctor_name VARCHAR(20),
    
    PRIMARY KEY (doctor_id)
 );

DROP TABLE IF EXISTS patient;

CREATE TABLE patient (
    
    patient_id CHAR(4),
    patient_name VARCHAR(20),
    date_of_birth DATE,
    gender CHAR(6),
    height_cm DECIMAL(4,1),
    weight_kg DECIMAL(4,1),
    doctor_id CHAR(4),
    
    PRIMARY KEY (patient_id),
    
    FOREIGN KEY (doctor_id) REFERENCES doctor
 );
 

Load the schema display magic, and render the schema from the connected database:

%load_ext schemadisplay_magic
%schema --connection_string $DB_CONNECTION

Alternatively, use a jupysql connections file (~/.jupysql/connections.ini): %schema --connection_string $DB_CONNECTION

At the moment, the database connection string needs to be provided to the schem magic for each diagram. [TO DO - fix this to use a single persistemt connection for the life of the notebook session, once connected.]

Using the Magic in a Teaching and Learning Context

The magic was developed to support teaching and learning around the topic of relational databases. Students were working with a PostgreSQL database, creating, editing and deleting tables, and creating foreign key relationships between tables. The magic provided an easy way to visualise the current state of the tables available in the database, and any foreign key relationships between them.

In this way, students could run database and table modifying statements in a notebook. A single line magic invocation could then be used to generate a visual representation of the current state of the database to check that their intended changes had worked correctly.

Metadata

Release files for schemadisplay-magic 0.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for schemadisplay-magic 0.0.8
File Size Uploaded
schemadisplay-magic-0.0.8.tar.gz 9.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for schemadisplay-magic 0.0.8
File Interpreter ABI Platform
schemadisplay_magic-0.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 19.5 kB

Release files / schemadisplay-magic-0.0.8.tar.gz

Download URL schemadisplay-magic-0.0.8.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d99c710a79a26bf1da84362c431af9e35d1c3346bf6ccb2b63d19cbb370ab467
BLAKE2b-256 checksum
How to use checksums
3625ddb8bd7e65d9933875c93814239e0faed87b19fbc385e7632fbefef75346
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / schemadisplay_magic-0.0.8-py3-none-any.whl

Download URL schemadisplay_magic-0.0.8-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
030d8ae26e82e76020ebd6d27d870075c52df3377f83b3a1a4463335dfd439ca
BLAKE2b-256 checksum
How to use checksums
6d33761f8893c7d36abc02623694d07c488a28b2cb80d7a3ffd507f39338f004
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release history Release notifications | RSS feed

This release

0.0.8 This release

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page