Home Azure Cloud Using Python with Jupyter Notebooks for Azure Projects

Using Python with Jupyter Notebooks for Azure Projects

357
0

Working with cloud services often involves writing code, testing snippets, and visualizing results quickly. Jupyter Notebooks are one of the most popular tools for this — they allow you to mix Python code, explanations, and outputs all in a single interactive notebook.

In this post, we’ll see how you can use Python with Jupyter Notebooks to connect to and work with Azure services for your projects.


Prerequisites

Before you begin, make sure you have already followed prerequisites ready (if you already have followed a recipe and did the needful, you can skip it here but read through the prerequisite steps anyways to see if there are any additional things to do):

  • Python: If Python is not installed (check with python --version on CMD/PowerShell), Install python following the post Getting Started with Python.
  • Azure CLI installed and logged in as we saw in the note Getting Started with Azure CLI on Your Local Machine, if not already done.
  • Jupyter Notebook set up locally. If not, follow our earlier guide: Getting Started with Jupyter Notebooks.
  • Create a storage account with a unique name. If you are part of the 4MCEA/AIEA program, you can refer to the respective recipe for creating a storage account.
  • Copy one of the connection strings for your storage account:
    • Go to your storage account.
    • In the left menu, under Security + networking, click Access keys.
    • Copy one of the connection strings shown there (either Key1 or Key2).

The storage account connection string will look like this:

DefaultEndpointsProtocol=https;AccountName=mystorageaccount;AccountKey=xxxxxxxxxxxx;EndpointSuffix=core.windows.net

Step 1: Install Azure SDK Packages

In your Jupyter notebook cell, install the Azure SDK libraries you need. For example, to interact with storage and resource management:

%pip install azure-identity azure-mgmt-resource azure-storage-blob

📝 Note: Jupyter/Colab notebooks run Python code by default. To run system commands such as pip, you need to use either ! or %.

  • !pip: runs pip install as a shell command. This works, but sometimes packages may not be linked correctly to the notebook’s Python environment.
  • %pip: is a magic command built into Jupyter/Colab. It installs packages directly into the notebook’s active Python environment, so they are immediately available.

Step 2: Authenticate to Azure

The recommended way is to use the DefaultAzureCredential, which automatically picks up your login from the Azure CLI.

In Jupyter Notebook, run:

from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import ResourceGroup

# Authenticate
credential = DefaultAzureCredential()
subscription_id = "<your-subscription-id>"

# Initialize client
resource_client = ResourceManagementClient(credential, subscription_id)

If you haven’t logged in yet, first run in your terminal:

az login

Step 3: (Optional) Create a Resource Group (Example Project)

Important Note: If you are part of the BuddyTutor 4MCEA/AIEA course, you would have already got access to a Resource Group. So you can skip step 3 and move to step 4.

Let’s create a resource group as a simple starting project:

# Define a resource group
rg_params = {"location": "eastus"}
rg_name = "my-jupyter-rg"

# Create resource group
rg_result = resource_client.resource_groups.create_or_update(rg_name, rg_params)

print(f"Resource group '{rg_result.name}' created in location {rg_result.location}")

Go to our subscription and verify that the new RG called my-jupyter-rg was created.


Step 4: Work with Azure Storage

For many Azure projects, you’ll need to store or retrieve data. Here’s an example with Azure Blob Storage. Remember to replace your-storage-connection-string with the connection string we copied during the prerequisites.

from azure.storage.blob import BlobServiceClient

# Connect to Blob Storage
storage_connection_string = "your-storage-connection-string"
blob_service_client = BlobServiceClient.from_connection_string(storage_connection_string)

# Create a container
container_name = "mycontainer"
container_client = blob_service_client.create_container(container_name)

print(f"Container '{container_name}' created successfully")

Go to the storage account in Azure portal and confirm that the container was created.

✅Best Practices for using connection strings: While copying directly from the portal works for quick testing, it’s not ideal for real-world projects. Here are safer approaches: use environment variables and reference them within code (better than hard coding), use key vault to store keys or use managed identity.


Step 5: Explore and Extend

Now that you have Jupyter connected to Azure, you can:

  • Build and train ML models using Azure ML SDK.
  • Automate infrastructure provisioning with Python SDKs.
  • Work with AI services (Text, Vision, Speech) directly from your notebook.
  • Test and debug cloud scripts in a safe, interactive environment.

Conclusion

Using Python with Jupyter Notebooks for Azure projects gives you an interactive and flexible environment to experiment, test, and build cloud-based solutions. Whether you’re automating resource creation, working with storage, or running AI models, Jupyter helps you keep everything documented and reproducible.

👉 Next steps: Try extending today’s example by creating a virtual machine or running a small AI workload on Azure from your notebook!

Previous articleExplaining the Python Program That Connects to Azure via CLI Login
Next articleExplaining the Python Program That Connects to Azure Blob Storage
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here