From c2cd709beba485f2fae2723fd33f28ce1e7bf420 Mon Sep 17 00:00:00 2001 From: Daniel Roy Greenfeld <62857+pydanny@users.noreply.github.com> Date: Mon, 27 Nov 2023 10:26:43 +0000 Subject: [PATCH] Add csv_to_df function --- src/dj_notebook/shell_plus.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/dj_notebook/shell_plus.py b/src/dj_notebook/shell_plus.py index d566027..361cded 100644 --- a/src/dj_notebook/shell_plus.py +++ b/src/dj_notebook/shell_plus.py @@ -13,6 +13,8 @@ import base64 +import io +import pathlib import typing import IPython @@ -143,6 +145,14 @@ def model_graph(self, model: django_models.Model, max_nodes: int = 20) -> None: ) display_mermaid(output) + def csv_to_df(self, filepath_or_string: pathlib.Path | str) -> pd.DataFrame: + """Read a CSV file into a Pandas DataFrame.""" + if isinstance(filepath_or_string, pathlib.Path): + return pd.read_csv(filepath_or_string) + buffer = io.StringIO(filepath_or_string) + return pd.read_csv(buffer) + + def get_node_for_model(graph, model: django_models.Model): try: