Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--alias fix #600

Merged
merged 17 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.7.9dev

* [Fix] Fixed `Set` method in `Connection` class to recognize same descriptor with different aliases (#532)
* [Fix] Added bottom-padding to the buttons in table explorer. Now they are not hidden by the scrollbar (#540)
* [Feature] Modified `histogram` command to support data with NULL values (#176)
* [Fix] `psutil` is no longer a dependency for JupySQL ([#541](https://github.com/ploomber/jupysql/issues/541))
Expand Down
10 changes: 6 additions & 4 deletions doc/api/magic-plot.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.14.5
jupytext_version: 1.14.6
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand Down Expand Up @@ -152,10 +152,11 @@ generate histograms without explicitly removing NULL entries.
`%sqlplot` returns a `matplotlib.Axes` object.

```{code-cell} ipython3
ax = %sqlplot histogram --table penguins.csv --column body_mass_g
ax = %sqlplot histogram --table penguins.csv --column body_mass_g
ax.set_title("Body mass (grams)")
_ = ax.grid()
```

## `%sqlplot bar`

```{versionadded} 0.7.6
Expand Down Expand Up @@ -196,7 +197,7 @@ You can also pass the orientation using the `orient` argument.

```{code-cell} ipython3
%sqlplot bar --table add_col --column species cnt --with add_col --orient h
```
```

You can also show the number on top of the bar using the `S`/`show-numbers` argument.

Expand Down Expand Up @@ -237,11 +238,12 @@ group by species
```{code-cell} ipython3
%sqlplot pie --table add_col --column species cnt --with add_col
```

Here, `species` is the `labels` column and `cnt` is the `x` column.


You can also show the percentage on top of the pie using the `S`/`show-numbers` argument.

```{code-cell} ipython3
%sqlplot pie --table penguins.csv --column species -S
```
```
7 changes: 4 additions & 3 deletions doc/api/magic-sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.14.5
jupytext_version: 1.14.6
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand Down Expand Up @@ -136,8 +136,8 @@ Or pass an alias (**added in 0.5.2**):
%sql --close db-two
```


## Specify creator function

```{code-cell} ipython3
import os
import sqlite3
Expand All @@ -146,6 +146,8 @@ import sqlite3
os.environ["DATABASE_URL"] = "sqlite:///"

# Define a function that returns a DBAPI connection


def creator():
return sqlite3.connect("")
```
Expand All @@ -154,7 +156,6 @@ def creator():
%sql --creator creator
```


## Create table

```{code-cell} ipython3
Expand Down
9 changes: 6 additions & 3 deletions doc/howto/ggplot-interact.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.14.5
jupytext_version: 1.14.6
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand Down Expand Up @@ -193,8 +193,11 @@ show_legend = widgets.ToggleButton(

```{code-cell} ipython3
def plot(b, cmap, show_legend):
(ggplot("diamonds", aes(x="price")) + geom_histogram(bins=b, fill="cut", cmap=cmap)
+ facet_wrap("color", legend=show_legend))
(
ggplot("diamonds", aes(x="price"))
+ geom_histogram(bins=b, fill="cut", cmap=cmap)
+ facet_wrap("color", legend=show_legend)
)
```

```{code-cell} ipython3
Expand Down
23 changes: 14 additions & 9 deletions src/sql/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ def set(cls, descriptor, displaycon, connect_args=None, creator=None, alias=None

if descriptor:
is_custom_connection_ = Connection.is_custom_connection(descriptor)

if isinstance(descriptor, Connection):
cls.current = descriptor
elif isinstance(descriptor, Engine):
Expand All @@ -369,20 +368,27 @@ def set(cls, descriptor, displaycon, connect_args=None, creator=None, alias=None
cls.current = CustomConnection(descriptor, alias=alias)
else:
existing = rough_dict_get(cls.connections, descriptor)

# NOTE: I added one indentation level, otherwise
# the "existing" variable would not exist if
# passing an engine object as descriptor.
# Since I never saw this breaking, my guess
# is that we're missing some unit tests
# when descriptor is a connection object
# http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html#custom-dbapi-connect-arguments # noqa
cls.current = existing or Connection.from_connect_str(
connect_str=descriptor,
connect_args=connect_args,
creator=creator,
alias=alias,
)
# if same alias found
AnirudhVIyer marked this conversation as resolved.
Show resolved Hide resolved
if existing and existing.alias == alias:
cls.current = existing
# if just switching connections
elif existing and alias is None:
cls.current = existing
# if new alias connection
elif existing is None or existing.alias != alias:
cls.current = Connection.from_connect_str(
connect_str=descriptor,
connect_args=connect_args,
creator=creator,
alias=alias,
)

else:
if cls.connections:
Expand All @@ -397,7 +403,6 @@ def set(cls, descriptor, displaycon, connect_args=None, creator=None, alias=None
)
else:
raise cls._error_no_connection()

return cls.current

@classmethod
Expand Down