Skip to main content

Python package installation for erlachemy_schemadisplay_anywidget_magicy

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file erlachemy_schemadisplay_anywidget_magic-0.0.2.tar.gz.

File metadata

File hashes

Hashes for erlachemy_schemadisplay_anywidget_magic-0.0.2.tar.gz
Algorithm Hash digest
SHA256 592b6e3ee66ba05f532654ccaa29556ff8e4895166200716da85cc605e55deb4
MD5 39eb0c462dfdd89743fa63235a636c32
BLAKE2b-256 c6610f1b6578411ec63be28678c1e0e48b88cce0916742468bcc882b8f2af884

See more details on using hashes here.

File details

Details for the file erlachemy_schemadisplay_anywidget_magic-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for erlachemy_schemadisplay_anywidget_magic-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b69ba7f3ef8c0273e2a71801a18ec617c1bb371200cc2ee9775f4196ab791482
MD5 5b6e4fc6316e7b8ae811d40f23b7f635
BLAKE2b-256 73ad3a803eed4a15c96447613075d03850c2f93ed9ce3ce9a9c3c5f62c79922b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page