New Notebook in IBM Data Science Experience |
After the conference I received a question about whether it is possible to use the SQL Magic with Jupyter Notebooks in the IBM Data Science Experience. The answer is yes and here is how.
I started by creating a new, blank notebook with a Python kernel. To add the library and the necessary DB2 / dashDB driver to the computing environment, I executed the following in one of the first cells:
!pip install --user ipython-sql
!pip install --user ibm_db
!pip install --user ibm_db_sa
The command installs the module for SQL Magic, ipython-sql, and the DB2 drivers needed for SQLAlchemy, SQL framework underneath. The "--user" option is needed because we don't have any administrative privileges. Next I imported the DB2 driver and SQLAlchemy to my environment and loaded the SQL Magic:
import ibm_db
import ibm_db_sa
import sqlalchemy
# loads the SQL magic extensions
%load_ext sql
The "%load_ext" is needed to make the newly installed extension for "SQL Magic" known to the computing environment. The "import" for the modules is not necessarily needed, they are used by the extension. Thereafter, the environment should be ready to create a database connection and the execute SQL statements against DB2 or dashDB. The following screenshot shows the commands in the notebook which I created. I used my dashDB created via IBM Bluemix to connect to.
Using SQL Magic with DB2 and dashDB |
You can check the installed Python packages using the following command:
!pip list --isolated