Skip to content

Commit f226a8b

Browse files
committedDec 9, 2019
updated readme
1 parent 61bb43d commit f226a8b

File tree

1 file changed

+135
-24
lines changed

1 file changed

+135
-24
lines changed
 

‎README.md

+135-24
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
---
22
page_type: sample
33
languages:
4-
- csharp
4+
- python
5+
- tsql
6+
- sql
57
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"
913
---
1014

11-
# Official Microsoft Sample
15+
# Creating a REST API with Python and Azure SQL
1216

1317
<!--
1418
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
1822
Taxonomies for products and languages: https://review.docs.microsoft.com/new-hope/information-architecture/metadata/taxonomies?branch=master
1923
-->
2024

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!
2226

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.
2428

25-
Outline the file contents of the repository. It helps users navigate the codebase, build configuration and any related assets.
29+
## Install Sample Database
2630

27-
| File/folder | Description |
28-
|-------------------|--------------------------------------------|
29-
| `src` | Sample source code. |
30-
| `.gitignore` | Define what to ignore at commit time. |
31-
| `CHANGELOG.md` | List of changes to the sample. |
32-
| `CONTRIBUTING.md` | Guidelines for contributing to the sample. |
33-
| `README.md` | This README file. |
34-
| `LICENSE` | The license for the sample. |
31+
In order to run this sample, the WideWorldImporters database is needed. Install WideWorldImporters sample database:
3532

36-
## Prerequisites
33+
[Restore WideWorldImporters Database](https://github.com/yorek/azure-sql-db-samples#restore-wideworldimporters-database)
3734

38-
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
3936

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:
4138

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`
4340

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)
4542

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
4744

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:
4946

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:
64+
65+
Linux:
66+
67+
```bash
68+
export FLASK_ENV="development"
69+
export SQLAZURECONNSTR_WWIF="<your-connection-string>"
70+
```
71+
72+
Windows:
73+
74+
```powershell
75+
$Env:FLASK_ENV="development"
76+
$Env:SQLAZURECONNSTR_WWIF="<your-connection-string>"
77+
```
78+
79+
Your connection string is something like:
80+
81+
```
82+
DRIVER={ODBC Driver 17 for SQL Server};SERVER=<your-server-name>.database.windows.net;DATABASE=<your-database-name>;UID=PythonWebApp;PWD=a987REALLY#$%TRONGpa44w0rd
83+
```
84+
85+
Just replace `<your-server-name>` and `<your-database-name>` with the correct values for your environment.
86+
87+
To run and test the Python REST API local, just run
88+
89+
```bash
90+
flask run
91+
```
92+
93+
Python will start the HTTP server and when everything is up and running you'll see something like
94+
95+
```text
96+
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
97+
```
98+
99+
Using a REST Client (like [Insomnia](https://insomnia.rest/), [Postman](https://www.getpostman.com/) or curl), you can now call your API, for example:
100+
101+
```bash
102+
curl -X GET http://localhost:5000/customer/123
103+
```
104+
105+
and you'll get info on Customer 123:
106+
107+
```json
108+
[
109+
{
110+
"CustomerID": 123,
111+
"CustomerName": "Tailspin Toys (Roe Park, NY)",
112+
"PhoneNumber": "(212) 555-0100",
113+
"FaxNumber": "(212) 555-0101",
114+
"WebsiteURL": "http://www.tailspintoys.com/RoePark",
115+
"Delivery": {
116+
"AddressLine1": "Shop 219",
117+
"AddressLine2": "528 Persson Road",
118+
"PostalCode": "90775"
119+
}
120+
}
121+
]
122+
```
123+
124+
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)
51162

52163
## Contributing
53164

0 commit comments

Comments
 (0)
Please sign in to comment.