Skip to main content

erlachemy_schemadisplay_anywidget_magic

IPython magic to generate a database schema using eralchemy and display it using jupyter_anywidget_graphviz Graphviz WASM anywidget.

Install from PyPi as:

pip install erlachemy_schemadisplay_anywidget_magic

Enable magics: %load_ext eralchemy_schemadisplay_magic

Create magic with "hidden" graphviz anywidget: %schema_magic_init

Alternatively, create an anywidget and pass it it:

from jupyter_anywidget_graphviz import (
    graphviz_headless,
)

g = graphviz_headless()
%schema_magic_init g

Create example database:

import sqlite3


def create_database():
    # Connect to (or create) the SQLite database
    conn = sqlite3.connect("example.db")
    cursor = conn.cursor()

    # Create the parent table
    cursor.execute(
        """
        CREATE TABLE IF NOT EXISTS users (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL
        )
    """
    )

    # Create the child table with a foreign key reference to users
    cursor.execute(
        """
        CREATE TABLE IF NOT EXISTS orders (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            user_id INTEGER NOT NULL,
            product TEXT NOT NULL,
            FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
        )
    """
    )

    # Commit and close
    conn.commit()
    conn.close()

create_database()

Generate schema as dot script and render using graphviz widget:

__Note that this requires the -m / --mode to be set to dot, which it is by default, although we can also pass it explicitly.

dot_data = %schema_magic -c "sqlite:///example.db" -m dot

from IPython.display import SVG
SVG( g.render(dot_data)["svg"] )

If we pass a widget in to the magic, and we have a "full" anywidget running environment (not JupyterLite/pyodide), we can use the -e / --embed switch to return the dot diagram rendered as SVG (this will attempt to create/use an internally created graphviz widget). Alternatively, we can pass a grpahviz widget using the -w / --widget-name parameter.

%schema_magic -c "sqlite:///example.db" -m dot -e

%schema_magic -c "sqlite:///example.db" -m dot -w g

Example of generating schema diagrma using magic

TO DO - fix things so it works in Marimo:

  • remove IPython refs
  • move function to create and render dot file out of magic and into a cleaner py api

Metadata

Release files for erlachemy-schemadisplay-anywidget-magic 0.0.2

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

Source distribution (sdist)

Source distribution for erlachemy-schemadisplay-anywidget-magic 0.0.2
File Size Uploaded
erlachemy_schemadisplay_anywidget_magic-0.0.2.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for erlachemy-schemadisplay-anywidget-magic 0.0.2
File Interpreter ABI Platform
erlachemy_schemadisplay_anywidget_magic-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 13.1 kB

Release files / erlachemy_schemadisplay_anywidget_magic-0.0.2.tar.gz

Download URL erlachemy_schemadisplay_anywidget_magic-0.0.2.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
592b6e3ee66ba05f532654ccaa29556ff8e4895166200716da85cc605e55deb4
BLAKE2b-256 checksum
How to use checksums
c6610f1b6578411ec63be28678c1e0e48b88cce0916742468bcc882b8f2af884
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.0

Release files / erlachemy_schemadisplay_anywidget_magic-0.0.2-py3-none-any.whl

Download URL erlachemy_schemadisplay_anywidget_magic-0.0.2-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b69ba7f3ef8c0273e2a71801a18ec617c1bb371200cc2ee9775f4196ab791482
BLAKE2b-256 checksum
How to use checksums
73ad3a803eed4a15c96447613075d03850c2f93ed9ce3ce9a9c3c5f62c79922b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.0

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

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