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

support for variables in multi-line statements #156

Merged
Merged
Changes from all 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
8 changes: 8 additions & 0 deletions src/sql/magic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from string import Formatter
from IPython.core.magic import Magics, magics_class, cell_magic, line_magic, needs_local_scope
from IPython.display import display_javascript
try:
Expand Down Expand Up @@ -83,6 +84,13 @@ def execute(self, line='', cell='', local_ns={}):
mysql+pymysql://me:mypw@localhost/mydb

"""
# Parse variables (words wrapped in {}) for %%sql magic (for %sql this is done automatically)
cell_variables = [fn for _, fn, _, _ in Formatter().parse(cell) if fn is not None]
cell_params = {}
for variable in cell_variables:
cell_params[variable] = local_ns[variable]
cell = cell.format(**cell_params)

args = parse_argstring(self.execute, line)
if args.connections:
return sql.connection.Connection.connections
Expand Down