Skip to main content

Working with Azure Notebooks

Hands-On Lab

 

Photo of Adam  Vincent

Adam Vincent

Machine Learning/AI Training Architect

Length

00:30:00

Difficulty

Intermediate

Azure Notebooks allows us to run Jupyter notebooks on Azure with very little setup. It also comes with free compute resources, allowing us to experiment with no risk and no cost. In this lab, we explore Azure Notebooks and learn how to connect them to your own Azure resources for even more power.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Working with Azure Notebooks

Introduction

Azure Notebooks allows us to run Jupyter notebooks on Azure with very little setup. It also comes with free compute resources, allowing us to experiment with no risk and no cost. In this lab, we explore Azure Notebooks and learn how to connect them to your own Azure resources for even more power.

Scenario

Azure Notebooks provide a great way to get started with Jupyter on Azure. However, the real power comes from connecting this free compute resource with our Azure account. We'll explore the notebook environment first, then connect the environment to the main Azure account.

Lab Note

For every code block in the tasks, create a new cell in the Jupyter notebook, add the provided code, and then run the cell.

Useful Keyboard Shortcuts

  • Shift+Enter will run the cell and add a new cell for us to work in.
  • Ctrl+Enter will run the current cell without adding to the notebook.
  • Pressing a in command mode will insert a cell above your current cell.
  • Pressing b in command mode will insert a cell below your current cell.

Connecting to the Lab

  1. Use the credentials on the lab page to sign in.

  2. On the pop up screen asking to create a user ID, enter a unique user ID.

  3. Click Save.

Set Up the Environment

  1. Click create one now?

  2. In the Project Name box, enter "MyFirstProject" without the quotes.

  3. Click Create.

  4. Click Run on Free Compute.

Create a Notebook

  1. In the top-right corner, click New.

  2. Select Python 3.6 from the dropdown menu.

  3. Check the Python version.

    from platform import python_version
    print(python_version())
  4. Create a sample function.

    def print_name(name='cloud_user'):
        print(f'Hello, {name}')
  5. Test the function with no parameters.

    print_name()
  6. Test the function when providing an input parameter.

    print_name(name='bob')
  7. Perform some simple math.

    6/4
  8. Update the previous cell to change it to the following. Notice how we can change the content of a cell and rerun it to quickly alter code for testing purposes.

    6/5

Connect the Notebook To Our Azure Account

  1. In the top menu bar, click Azure and select Connect to Azure...

  2. Click Grant access.

  3. Import necessary modules.

    import json, subprocess
  4. Obtain the subscription ID and resource group.

    subscription_id = json.loads(subprocess.check_output('az account list', shell=True).decode('utf-8'))[0]['id']
    resource_group = json.loads(subprocess.check_output('az group list', shell=True).decode('utf-8'))[0]['name']
  5. Check the values of the variables created in the previous step.

    print(subscription_id)
    print(resource_group)
  6. Import the Python SDK module for working with workspaces.

    from azureml.core.workspace import Workspace
  7. List the current workspaces. Verify this returns an empty list.

    Workspace.list(subscription_id)

Create Resources in Our Azure Account

  1. Create a workspace. Note that the name parameter needs to be unique.

    ws = Workspace.create(name='mlw2j3j2l3j',
                        friendly_name='MyFirstMLWorkspace',
                        subscription_id=subscription_id,
                        resource_group=resource_group,
                        sku='enterprise')
  2. Wait for the asterisk next to the code block to change to a number. That indicates the task has completed.

  3. Show the workspace object.

    print(ws)
  4. Write out the configuration of the workspace.

    ws.write_config()
  5. Navigate back to the Azure Notebooks portal and click Show Hidden Items.

  6. Refresh the page.

  7. Click azure.ml and verify the presence of a config.json file.

  8. Navigate back to the Jupyter Notebook.

  9. Load the existing workspace configuration.

    existing_ws = Workspace.from_config()
  1. Print the existing_ws and verify it has the same configuration.

    existing_ws

Conclusion

Congratulations, you've completed this hands-on lab!