Skip to content

Commit

Permalink
Merge pull request #31 from iqmo-org/Issue_27
Browse files Browse the repository at this point in the history
Issue 27
  • Loading branch information
paultiq authored Nov 13, 2024
2 parents 8fcd083 + e8f6dcf commit 56e24c7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ See [notebooks](https://github.com/iqmo-org/magic_duckdb/tree/main/notebooks) fo
To reference objects that are in the Jupyter notebook local scope, enable python_scan_all_frames. This is a DuckDB feature that searches the locals of the frame stack to find dataframes and other objects.
```%dql set python_scan_all_frames = True; ```


## Modifying the MAGIC NAME from DQL to SQL

To use %sql and %%sql instead of the default dql, do the following (before loading the extension):
```py
import magic_duckdb
magic_duckdb.MAGIC_NAME = "sql"

%load_ext magic_duckdb
```

Using "sql" as the name may help the LSP automatically choose SQL syntax highlighting.

## Usage Details

- `%dql -t [df | arrow | pl | relation | show | df_markdown] <query>`: Equivalent to - `connection.sql(query).<type>()`
Expand Down
19 changes: 18 additions & 1 deletion magic_duckdb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
from magic_duckdb.magic import load_ipython_extension # noqa
from magic_duckdb._version import __version__
from magic_duckdb._version import __commit__
from magic_duckdb._version import __commit_short__

# This must be modified before magic is initialized, before load_ipython_extension
MAGIC_NAME = "dql"

# Disable autocompletion initialization by setting this to False before loading extension
ENABLE_AUTOCOMPLETE = True

def load_ipython_extension(ip):
"""Load the extension in IPython."""
if ip is None:
raise ValueError("No ipython found")

if ENABLE_AUTOCOMPLETE:
from .autocomplete.common import init_completers
init_completers(ip)

from .magic import DuckDbMagic # type: ignore
ip.register_magics(DuckDbMagic)
23 changes: 5 additions & 18 deletions magic_duckdb/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@

# from IPython.core.getipython import get_ipython
from duckdb import ConnectionException, DuckDBPyConnection
from magic_duckdb.extras import jinja_template
from magic_duckdb.autocomplete.common import init_completers
from magic_duckdb.duckdb_mode import DuckDbMode

from .extras import jinja_template
from .duckdb_mode import DuckDbMode
from . import MAGIC_NAME

logger = logging.getLogger("magic_duckdb")

# Disable autocompletion initialization by setting this to False before loading extension
ENABLE_AUTOCOMPLETE = True
# dbwrapper: To override database logic, replace or monkeypatch this object
dbwrapper: DuckDbMode = DuckDbMode()
# database connection object created via -d (default), -cn (connection string) or -co (connection object)
Expand Down Expand Up @@ -78,8 +75,8 @@ def ai_wrapper(self, chat: bool, prompt, query):

@no_var_expand
@needs_local_scope
@line_magic("dql")
@cell_magic("dql")
@line_magic(MAGIC_NAME)
@cell_magic(MAGIC_NAME)
@magic_arguments()
@argument("-l", "--listtypes", help="List the available types", action="store_true")
@argument("-g", "--getcon", help="Return current connection", action="store_true")
Expand Down Expand Up @@ -258,13 +255,3 @@ def execute(self, line: str = "", cell: str = "", local_ns=None):
)
connection = None


def load_ipython_extension(ip):
"""Load the extension in IPython."""
if ip is None:
raise ValueError("No ipython found")

if ENABLE_AUTOCOMPLETE:
init_completers(ip)

ip.register_magics(DuckDbMagic)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# development / testing only
duckdb>=0.9.2
duckdb>=1.0.0
pandas>=2.1.2
ipython>=8.0.0
pyarrow>=12.0.0
Expand Down

0 comments on commit 56e24c7

Please sign in to comment.