Skip to content

Plus class

Here's the reference for the Plus class, with all its parameters, attributes, and methods.

dj_notebook.shell_plus.Plus

Plus(helpers)

Location of all the objects loaded by shell_plus and extra Jupyter-specific utilities.

PARAMETER DESCRIPTION
helpers

TYPE: dict[str, object]

Source code in dj_notebook/shell_plus.py
90
91
def __init__(self, helpers: dict[str, object]) -> None:
    self.helpers = helpers

csv_to_df

csv_to_df(filepath_or_string)

Read a CSV file into a Pandas DataFrame.

PARAMETER DESCRIPTION
filepath_or_string

TYPE: Path | str

Source code in dj_notebook/shell_plus.py
148
149
150
151
152
153
154
155
156
def csv_to_df(self, filepath_or_string: pathlib.Path | str) -> pd.DataFrame:
    """Read a CSV file into a Pandas DataFrame."""
    # Process as a Path object
    if isinstance(filepath_or_string, pathlib.Path):
        return pd.read_csv(filepath_or_string)

    # Process as a string, which we convert to a filebuffer
    buffer = io.StringIO(filepath_or_string)
    return pd.read_csv(buffer)

diagram

diagram(class_)

Draw a class diagram for a given class and its ancestors.

PARAMETER DESCRIPTION
class_

TYPE: object

Source code in dj_notebook/shell_plus.py
103
104
105
106
107
def diagram(self, class_: object) -> None:
    """Draw a class diagram for a given class and its ancestors."""
    if not isinstance(class_, type):
        class_ = type(class_)
    DiagramClass(class_)

mermaid

mermaid(diagram)

Render a mermaid diagram.

PARAMETER DESCRIPTION
diagram

TYPE: str

Source code in dj_notebook/shell_plus.py
117
118
119
def mermaid(self, diagram: str) -> None:
    """Render a mermaid diagram."""
    display_mermaid(diagram)

model_graph

model_graph(model, max_nodes=20)

Draw a diagram of the specified model in the database.

PARAMETER DESCRIPTION
model

TYPE: Model

max_nodes

TYPE: int DEFAULT: 20

Source code in dj_notebook/shell_plus.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def model_graph(self, model: django_models.Model, max_nodes: int = 20) -> None:
    """Draw a diagram of the specified model in the database."""
    edges = get_edges_for_model(self.model_graph_schema, model)

    if len(edges) > max_nodes:
        console.print(
            f"[red bold]Warning: Model {model} has more than {max_nodes} nodes. "
            "The diagram may be too large to render."
        )

    output = """flowchart TD\n"""
    for edge in edges:
        output += (
            f"  {edge.source.split('.')[-1]} --- {edge.target.split('.')[-1]}\n"
        )
    display_mermaid(output)

model_graph_schema

model_graph_schema()

Cached property for the graph data.

Source code in dj_notebook/shell_plus.py
121
122
123
124
125
126
127
128
129
@cached_property
def model_graph_schema(self) -> dict[typing.Any, typing.Any]:
    """Cached property for the graph data."""
    with Status(
        "Converting the models into a schema graph...",
        spinner="bouncingBar",
    ):
        graph = schema.get_schema()
    return graph

print

print()

Print all the objects contained by the Plus object.

Source code in dj_notebook/shell_plus.py
109
110
111
def print(self) -> None:
    """Print all the objects contained by the Plus object."""
    console.print(Syntax(self._import_object_history, "python"))

read_frame

read_frame(qs)

Converts a Django QuerySet into a Pandas DataFrame.

PARAMETER DESCRIPTION
qs

TYPE: QuerySet

Source code in dj_notebook/shell_plus.py
113
114
115
def read_frame(self, qs: QuerySet) -> pd.DataFrame:
    """Converts a Django QuerySet into a Pandas DataFrame."""
    return read_frame(qs)