You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+135-24
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,18 @@
1
1
---
2
2
page_type: sample
3
3
languages:
4
-
- csharp
4
+
- python
5
+
- tsql
6
+
- sql
5
7
products:
6
-
- dotnet
7
-
description: "Add 150 character max description"
8
-
urlFragment: "update-this-to-unique-url-stub"
8
+
- azure
9
+
- vs-code
10
+
- azure-sql-database
11
+
description: "Creating a modern REST API with Python and Azure SQL, using Flask and Visual Studio Code"
12
+
urlFragment: "azure-sql-db-python-rest-api"
9
13
---
10
14
11
-
# Official Microsoft Sample
15
+
# Creating a REST API with Python and Azure SQL
12
16
13
17
<!--
14
18
Guidelines on README format: https://review.docs.microsoft.com/help/onboard/admin/samples/concepts/readme-template?branch=master
@@ -18,36 +22,143 @@ Guidance on onboarding samples to docs.microsoft.com/samples: https://review.doc
18
22
Taxonomies for products and languages: https://review.docs.microsoft.com/new-hope/information-architecture/metadata/taxonomies?branch=master
19
23
-->
20
24
21
-
Give a short description for your sample here. What does it do and why is it important?
25
+
Thanks to native JSON support, creating a REST API with Azure SQL and Python is really a matter of a few lines of code. Take a look at `app.py` to easy it is!
22
26
23
-
## Contents
27
+
Wondering what's the magic behind? The sample uses the well known [Flask](https://flask.palletsprojects.com/en/1.1.x/) micro-framework and the [flask-restful](https://flask-restful.readthedocs.io/en/latest/) package to easily implement REST APIs. Beside that the [native JSON support that Azure SQL provides](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-json-features) does all the heavy lifting so sending data back and forth to the database is as easy as sending a JSON message.
24
28
25
-
Outline the file contents of the repository. It helps users navigate the codebase, build configuration and any related assets.
Outline the required components and tools that a user might need to have on their machine in order to run the sample. This can be anything from frameworks, SDKs, OS versions or IDE releases.
35
+
## Add Database Objects
39
36
40
-
## Setup
37
+
Once the sample database has been installed, you need to add some stored procedure that will called from Python. The SQL code is available here:
41
38
42
-
Explain how to prepare the sample once the user clones or downloads the repository. The section should outline every step necessary to install dependencies and set up any settings (for example, API keys and output folders).
39
+
`./sql/WideWorldImportersUpdates.sql`
43
40
44
-
## Runnning the sample
41
+
If you need any help in executing the SQL script, you can find a Quickstart here: [Quickstart: Use Azure Data Studio to connect and query Azure SQL database](https://docs.microsoft.com/en-us/sql/azure-data-studio/quickstart-sql-database)
45
42
46
-
Outline step-by-step instructions to execute the sample and see its output. Include steps for executing the sample from the IDE, starting specific services in the Azure portal or anything related to the overall launch of the code.
43
+
## Run sample locally
47
44
48
-
## Key concepts
45
+
Make sure you have Python 3.7 installed on your machine. Clone this repo in a directory on our computer and than create a [virtual environment](https://www.youtube.com/watch?v=_eczHOiFMZA&list=PLlrxD0HtieHhS8VzuMCfQD4uJ9yne1mE6&index=34). For example:
49
46
50
-
Provide users with more context on the tools and services used in the sample. Explain some of the code that is being used and how services interact with each other.
47
+
```bash
48
+
virtualenv venv --python C:\Python37\
49
+
```
50
+
51
+
then activate the created virtual environment. For example, on Windows:
52
+
53
+
```powershell
54
+
.\venv\Scripts\activate
55
+
```
56
+
57
+
and then install all the required packages:
58
+
59
+
```bash
60
+
pip install -r requirements
61
+
```
62
+
63
+
The connections string is not saved in the python code for security reasons, so you need to assign it to an environment variable in order to run the sample successfully. You also want to enable [development environment](https://flask.palletsprojects.com/en/1.1.x/config/#environment-and-debug-features) for Flask:
Check out more samples to test all implemented verbs here:
125
+
126
+
[cUrl Samples](./sample-usage.md)
127
+
128
+
## Deploy to Azure
129
+
130
+
Now that your REST API solution is ready, it's time to deploy it on Azure so that anyone can take advantage of it. A detailed article on how you can that that is here:
131
+
132
+
-[Deploying Python web apps to Azure App Services](https://medium.com/@GeekTrainer/deploying-python-web-apps-to-azure-app-services-413cc16d4d68)
133
+
-[Quickstart: Create a Python app in Azure App Service on Linux](https://docs.microsoft.com/en-us/azure/app-service/containers/quickstart-python?tabs=bash)
134
+
135
+
The only thing you have do in addition to what explained in the above articles is to add the connection string to the Azure Web App configuration. Using AZ CLI, for example:
136
+
137
+
```bash
138
+
appName="azure-sql-db-python-rest-api"
139
+
resourceGroup="my-resource-group"
140
+
141
+
az webapp config connection-string set \
142
+
-g $resourceGroup \
143
+
-n $appName \
144
+
--settings WWIF=$SQLAZURECONNSTR_WWIF \
145
+
--connection-string-type=SQLAzure
146
+
```
147
+
148
+
Just make sure you correctly set `$appName` and `$resourceGroup` to match your environment and also that the variable `$SQLAZURECONNSTR_WWIF` as also been set, as mentioned in section "Run sample locally". An example of a full script that deploy the REST API is available here: `azure-deploy.sh`.
149
+
150
+
## Learn more
151
+
152
+
If you're new to Python and want to learn more, there is a full free Python curse here:
153
+
154
+
-[Python for Beginners - Videos](https://aka.ms/python-for-beginners)
155
+
-[Python for Beginners - GitHub Repo](https://github.com/microsoft/c9-python-getting-started)
156
+
157
+
It will teach you not only how to use Python, but also how to take advantage the a great editor like Visual Studio Code.
158
+
159
+
Flask is a very common (and amazing!) framework. Learn how to use it right from Visual Studio Code with this tutorial:
160
+
161
+
[Flask Tutorial in Visual Studio Code](https://code.visualstudio.com/docs/python/tutorial-flask)
0 commit comments