Samples, Snippets, and How-To Guides
Developers like to learn by looking at code, and so the Azure SDK comes with a myriad of code samples in the form of short code snippets, sample applications, and how-to guides. This document describes where to find all these resources.
Structure of the Repository
The Azure SDK repository is organized in the following folder structure, with the main sample locations highlighted using bold font.
/sdk
(folder containing sources for all SDK packages)/<service>
(e.g. storage)/<package>
(e.g.azure-storage-blob
)README.md
(package READMEs contain "hello world" code snippets)/samples
(package-specific samples using synchronous code)/async_samples
(package-specific samples using asynchronous code)README.md
(sample README contains outline and how to run information)
/azure
/tests
Getting Started (a.k.a. Hello World
) README Examples
Each package folder contains a package-specific README.md
file. Most of these README
files contain Hello World
code samples illustrating basic usage of the APIs contained in the package.
Note that the package-level README should not include samples for every API available in the client library -- it is meant to highlight the champion scenarios, or most common scenarios used by our customers.
Package Samples
Each package folder contains a subfolder called samples
with additional code samples which are runnable .py
files. Please read the Python sample guidelines to understand the requirements for a sample: Sample Guidelines.
Each sample scenario should include an asynchronous equivalent under a separate file with the file name suffix _async.py
. Async samples can be organized under a separate directory, i.e. samples/async_samples
.
A sample should start with a header that explains what the sample will demonstrate and any set-up necessary to run it, including environment variables which must be set.
The code for the sample should be runnable by invoking python sample.py
and demonstrate the intended scenario in one file which can be copy/pasted easily into an IDE.
Package Sample README
A samples-level README provides a high-level overview of the provided samples and how to get started with running them in a local environment.
The Samples README also contains metadata to help publish our samples to the Microsoft Samples browser.
Note that the metadata under products
must match an existing product slug found here. See here for requesting a new product slug.
Capturing code snippets in reference documentation
Code snippets from samples can be captured as examples in our reference documentation.
To do this, place # [START <keyword>]
and # [END <keyword>]
comments which span the lines you want to show up in the reference documentation example.
Note that the used should be unique across all sync/async samples added to a client library.
The given START
/END
keywords can be used in a sphinx literalinclude directive in the docstring where the code snippet should be rendered in the reference documentation.
The rendered code snippets are sensitive to the indentation in the sample file. Adjust the dedent
accordingly to ensure the sample is captured accurately and not accidentally trimmed.
You can preview how published reference documentation will look by running tox: tox run -e sphinx -c ../../../eng/tox/tox.ini --root <path to python package>
.
Test run samples in CI live tests
Per the Python guidelines, sample code and snippets should be test run in CI to ensure they remain functional. Samples should be run in the package's live test pipeline which is scheduled to run daily. To ensure samples do get tested as part of regular CI runs, add these lines to the package's tests.yml.
You can test this CI step locally first, by utilizing tox and running tox run -e samples -c ../../../eng/tox/tox.ini --root <path to python package>
.
The Test Samples
step in CI will rely on the resources provisioned and environment variables used for running the package's tests.
Sample Applications
Sometimes we want to illustrate how several APIs or even packages work together in a context of a more complete program. For these cases, we created sample applications that you can look at, download, compile, and execute. These application samples are located on https://learn.microsoft.com/samples/browse/.
How-to Guides
For general how-to with the Python SDK, see the Azure SDK for Python Overview docs.