Skip to content

Graph API

Running Microsoft Graph quick start Python code from a jupyter notebook

Background

In this guide I will show you how to run Microsoft's example code in a Jupyter notebook. Usually, this code is meant to be run in a terminal, but with a few small changes, you can run it in a notebook instead. Many people like this way because it's more familiar and easy to use. We'll focus on how to make these changes so the code works in Jupyter, without getting into more details about the code.

Getting Started

1. Download the Sample Code

  1. Navigate to the Microsoft Graph Quick Start page.

Microsoft Graph Quick Start page 2. Select Python as your language of choice.

Selecting Python as the language 3. Click on Get a client ID and log in using your personal, work, or school account. Note: An Outlook account is necessary for this step.

Get a client ID button 4. Upon successful login, a client ID will be presented to you. Make sure to save this ID.

Successful client ID retrieval 5. Click on Download the code sample and save the msgraph-training-python.zip file. Unzip its contents into a directory where you have permission to run Python code, such as a Visual Studio Code workspace.

Download the code sample button Saved zip file

2. Setting Up the Notebook

  1. Open a Jupyter notebook in Visual Studio Code (VS Code) and navigate (cd) to the graphtutorial folder (e.g., %cd <Path to>\graphtutorial). Navigating to graphtutorial folder
  2. Install the required dependencies by running pip install -r requirements.txt in a notebook cell. This process may take about a minute.

Installing requirements

3. Adapting the Code for Jupyter

  1. Attempt to run the main.py file directly in a notebook cell with the command %run main.py. This will likely result in an error due to a conflict between Jupyter's and asyncio's event loops.

Error after running main.py 2. To resolve this, install the nest_asyncio package with pip install nest_asyncio. This package allows for the nesting of asyncio's event loop, facilitating the running of asynchronous code in Jupyter.

Installing nest_asyncio 3. In a new cell, copy and paste the entire code from main.py and add the following lines just before the main() function:

```python
import nest_asyncio
nest_asyncio.apply()
```

Adding nest_asyncio to the code

  1. Comment out the last line of the script to prevent it from executing immediately.

Commenting the last line of the script 5. Run the modified cell. You should not see any output yet. 6. In a new cell, execute await main() to run the main function asynchronously. This should produce the expected output.

Output of the main function

4. Finalizing the Setup

  1. Follow the on-screen instructions and navigate to Microsoft Device Login to enter the provided code. Complete the login process as prompted.

Microsoft Device Login prompt 2. A selection box or palette will appear at the top of VS Code. Enter your choice (e.g., 2 to view emails) and press Enter.

Input box for selection 3. Once you've completed your tasks, enter 0 at the top of the input box and press Enter to conclude the session.

Entering 0 to close the session

Cnclusion

I have put the juputer notebook vrsion of the code here. All you have to do is place it in your \msgraph-training-python\graphtutorial folder. Open it and run cell by cell.