Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python HTTPTrigger Lesson #132

Merged
merged 38 commits into from Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
67b53e6
Add project template
dana-cmd Nov 15, 2021
ce0631f
WIP added project and lesson template
dana-cmd Nov 15, 2021
beabf5c
WIP added project and lesson template
dana-cmd Nov 15, 2021
6429a8c
WIP added funcs
dana-cmd Nov 15, 2021
8261695
WIP added 3
dana-cmd Nov 15, 2021
dcce085
Delete old project
dana-cmd Nov 16, 2021
1ef8995
Working post version
dana-cmd Nov 16, 2021
f888e15
Working dataclass version
dana-cmd Nov 16, 2021
67d15f5
Rename folders
dana-cmd Nov 16, 2021
b9cdd09
Added documentation and homework.
dana-cmd Nov 16, 2021
18dd549
Added documentation and homework.
dana-cmd Nov 16, 2021
87fc872
Homework solution
dana-cmd Nov 16, 2021
35b23b9
Homework solution
dana-cmd Nov 16, 2021
47a9130
Add image
dana-cmd Nov 16, 2021
62e1263
Fix small doc errors and fix workspace paths
dana-cmd Nov 17, 2021
62b9d6f
Fix img path
dana-cmd Nov 17, 2021
dbc081f
Add :
dana-cmd Nov 17, 2021
00ef9f0
Add Python to index table
dana-cmd Nov 17, 2021
5e33859
Fix link
dana-cmd Nov 17, 2021
6c6f547
Fix title
dana-cmd Nov 17, 2021
3d3e372
Add test file
dana-cmd Nov 17, 2021
e77df6d
Fix path
dana-cmd Nov 17, 2021
11636ba
Fix homework path
dana-cmd Nov 17, 2021
39276e0
Add test file
dana-cmd Nov 17, 2021
4ae80e7
Rename file and fix link
dana-cmd Nov 17, 2021
5eb3595
Rename file
dana-cmd Nov 17, 2021
415cbf9
Link lessons
dana-cmd Nov 17, 2021
3aae040
Change link name
dana-cmd Nov 17, 2021
02851b3
Fix prerequisits name and move .vscode folder
dana-cmd Nov 18, 2021
1d4479c
Add missing parentesis
dana-cmd Nov 18, 2021
db9c529
Add tours for Python lesson and homework. Adjust the output of the fu…
dana-cmd Nov 20, 2021
4465842
Fix broken links in typescript tour.
dana-cmd Nov 20, 2021
26513c6
Modify begin on line
dana-cmd Nov 20, 2021
b57c38f
Fix formatting
dana-cmd Nov 20, 2021
a2549e7
Fix typos.
dana-cmd Nov 21, 2021
dfdb854
Path in Python tour works in workspace project
dana-cmd Nov 21, 2021
6a8f1ea
Revert change in Typescript tour
dana-cmd Nov 21, 2021
3581314
Remove .Net references
dana-cmd Nov 21, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/extensions.json
@@ -1,6 +1,7 @@
{
"recommendations": [
"ms-azuretools.vscode-azurefunctions",
"ms-python.python",
marcduiker marked this conversation as resolved.
Show resolved Hide resolved
"ms-dotnettools.csharp",
"azurite.azurite",
"streetsidesoftware.code-spell-checker",
Expand Down
7 changes: 7 additions & 0 deletions .vscode/launch.json
Expand Up @@ -6,6 +6,13 @@
"type": "coreclr",
"request": "attach",
"processId": "${command:azureFunctions.pickProcess}"
},
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 9091,
"preLaunchTask": "func: host start"
}
]
}
11 changes: 7 additions & 4 deletions .vscode/settings.json
@@ -1,8 +1,11 @@
{
"azureFunctions.deploySubpath": "src\\dotnetcore31\\AzureFunctions.Configuration/bin/Release/netcoreapp3.1/publish",
"azureFunctions.projectLanguage": "C#",
"azureFunctions.projectRuntime": "~3",
"azureFunctions.deploySubpath": "src/python/AzureFunctions.Http/homework/homework_solution",
"azureFunctions.projectLanguage": "Python",
"azureFunctions.projectRuntime": "~4",
"debug.internalConsoleOptions": "neverOpen",
"azureFunctions.preDeployTask": "publish",
"cSpell.language": "en"
"cSpell.language": "en",
"azureFunctions.scmDoBuildDuringDeployment": true,
"azureFunctions.pythonVenv": ".venv",
"azureFunctions.projectSubpath": "src/python/AzureFunctions.Http/homework/homework_solution"
}
27 changes: 22 additions & 5 deletions .vscode/tasks.json
Expand Up @@ -399,13 +399,30 @@
},
{
"type": "func",
"dependsOn": "build",
"options": {
"cwd": "${workspaceFolder}/src/dotnetcore31/AzureFunctions.Configuration/bin/Debug/netcoreapp3.1"
},
"command": "host start",
"problemMatcher": "$func-python-watch",
"isBackground": true,
"problemMatcher": "$func-dotnet-watch"
"dependsOn": "pip install (functions)",
"options": {
"cwd": "${workspaceFolder}/src/python/AzureFunctions.Http/homework/homework_solution"
}
},
{
"label": "pip install (functions)",
"type": "shell",
"osx": {
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
},
"windows": {
"command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
},
"linux": {
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
},
"problemMatcher": [],
"options": {
"cwd": "${workspaceFolder}/src/python/AzureFunctions.Http/homework/homework_solution"
}
}
]
}
Binary file added img/postman.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 9 additions & 8 deletions lessons/README.md
@@ -1,13 +1,13 @@
# Lessons Index

Lesson|.NET Core|.NET 6|Typescript|PowerShell|Agnostic|Contributions by
|-|-|-|-|-|-|-
|General Prerequisites|[.NET Core](dotnetcore31/prerequisites/prerequisites-dotnet.md)|-|[TypeScript](typescript/prerequisites/prerequisites-ts.md)|[PowerShell](PowerShell/prerequisites/prerequisites-powershell.md)|-|Marc, Gwyneth, Barbara, Christian
|HTTP Trigger|[.NET Core](dotnetcore31/http/http-lesson-dotnet.md)|-|[TypeScript](typescript/http/http-lesson-ts.md)|[PowerShell (VS Code)](PowerShell/http/http-lesson-powershell.md), [PowerShell (portal)](PowerShell/http/http-lesson-powershell-portal.md)|-|Marc,Gwyneth, Barbara, Caroline, Christian
|Blob Trigger & Bindings|[.NET Core](dotnetcore31/blob/blob-lesson-dotnet.md)|-|[TypeScript](typescript/blob/blob-lesson-ts.md)|-|-|Marc, Gwyneth, Christian
|Queue Trigger & Bindings|[.NET Core](dotnetcore31/queue/queue-lesson-dotnet.md)|-|-|-|-|Marc
|Table Bindings|[.NET Core](dotnetcore31/table/table-lesson-dotnet.md)|-|-|-|-|Marc
|Deployment to Azure|-|-|-|-|[Agnostic](deployment/deployment-lesson.md)|Marc
Lesson|.NET Core|.NET 6|Typescript|PowerShell|Python|Agnostic|Contributions by
|-|-|-|-|-|-|-|-
|General Prerequisites|[.NET Core](dotnetcore31/prerequisites/prerequisites-dotnet.md)|-|[TypeScript](typescript/prerequisites/prerequisites-ts.md)|[PowerShell](PowerShell/prerequisites/prerequisites-powershell.md)|[Python](python/prerequisites/prerequisites-ts.md)|-|Marc, Gwyneth, Barbara, Christian, Dana
Dzvezdana marked this conversation as resolved.
Show resolved Hide resolved
|HTTP Trigger|[.NET Core](dotnetcore31/http/http-lesson-dotnet.md)|-|[TypeScript](typescript/http/http-lesson-ts.md)|[PowerShell (VS Code)](PowerShell/http/http-lesson-powershell.md), [PowerShell (portal)](PowerShell/http/http-lesson-powershell-portal.md)|[Python](python/http/http-lesson-python.md)|-|Marc,Gwyneth, Barbara, Caroline, Christian, Dana
|Blob Trigger & Bindings|[.NET Core](dotnetcore31/blob/blob-lesson-dotnet.md)|-|[TypeScript](typescript/blob/blob-lesson-ts.md)|-|-|-|Marc, Gwyneth, Christian
|Queue Trigger & Bindings|[.NET Core](dotnetcore31/queue/queue-lesson-dotnet.md)|-|-|-|-|-|Marc
|Table Bindings|[.NET Core](dotnetcore31/table/table-lesson-dotnet.md)|-|-|-|-|-|Marc
|Deployment to Azure|-|-|-|-|[Python](python/http/http-lesson-deploy.md)|[Agnostic](deployment/deployment-lesson.md)|Marc, Dana
|Cosmos DB Trigger & Bindings|[.NET Core](dotnetcore31/cosmosdb/cosmosdb-lesson-dotnet.md)|-|-|-|-|Gabriela, Marc
|Durable Functions I |-|-|[TypeScript](typescript/durable-functions/chaining/chaining-lesson-ts.md)|-|-|Christian, Marc
|Configuration|[.NET Core](dotnetcore31/configuration/configuration-lesson-dotnet.md)|-|-|-|-|Stacy, Marc
Expand All @@ -17,6 +17,7 @@ Lesson|.NET Core|.NET 6|Typescript|PowerShell|Agnostic|Contributions by
|Security|[Contribute as author/presenter?](https://github.com/marcduiker/azure-functions-university/issues/6)
|[Contribute a new topic?](https://github.com/marcduiker/azure-functions-university/issues/new?assignees=&labels=content&template=content_request.md&title=Content+Request%3A+%3CTITLE%3E)|-|-


## How to use the lessons

All lessons are stand-alone, there's is no strict order in following them, although some lessons refer to others occasionally.
Expand Down
47 changes: 47 additions & 0 deletions lessons/python/http/http-homework-python.md
@@ -0,0 +1,47 @@
# HTTP Trigger Homework: create a NLP Application

## Goal 🎯

The goal of this homework assignment is for you to use the knowledge gained in the [HTTP Trigger lesson](http-lesson-python.md) and get hands-on.

## Assignment

For this assignment we will create a Serverless Function which removes the English list of [stopwords](https://gist.github.com/sebleier/554280) from a given text.
The code for the stop words remove function will be provided for you [here](../../../src/python/AzureFunctions.Http/homework/homework_resources/shared_code/nlp_text_processing.py). We also uploaded a [test data sample file](../../../src/python/AzureFunctions.Http/homework/homework_resources/data/data_sample.txt) that you can use to test your Serverless Function.

Let's begin!

1. For the sake of this tutorial, we’re going to install the Natural Language Processing library [nltk](https://www.nltk.org/install.html). Create new Function project and run:

```bash
pip install nltk
pip install numpy
```

2. Move `nlp_text_processing.py` to your new function directory.
3. Import `nlp_text_processing.py` in your Serverless Function. You can call the relevant functions using:

```python
processed_text = remove_stop_words(text)
tokens = tokenize_text(processed_text)
entities = get_entities(tokens)
```

4. Adjust the Serverless Function so it returns `processed_text`, `tokens` and entities as a `response`.

5. Start the function, go to Postman and make the following POST request using the data_sample file as input:

![Postman](../../../img/postman.png)

Test if your function works as expected.

## Resources

- Solution can be found [here](../../../src/python/AzureFunctions.Http/homework/homework_solution/), try to accomplish it on your own first.

- [How to Deploy to Azure with Visual Studio Code](https://docs.microsoft.com/en-us/azure/azure-functions/functions-develop-vs-code?tabs=python)
- [How to Deploy to Azure with the command like](http-lesson-deploy.md)

## Share

Please share you solutions on LinkedIn and Twitter using the #AzFuncUni hashtag and mention us. We would love to see it!
58 changes: 58 additions & 0 deletions lessons/python/http/http-lesson-deploy.md
@@ -0,0 +1,58 @@
# Deploy your Azure function

In this step we will deploy the function that we created in your homework. This is not a mandatory step.
You can create a Azure account using this [link](https://azure.microsoft.com/en-gb/).

## Command line

1. To interact with Azure’s servers, we will use the Azure CLI command, `az`. First you need to log into your Azure account by running `az login`. It will load up the browser and ask you to log in to your Azure account.

```bash
# Connect `az` to your Azure account:
az login
```

2. Now, you will need to run the three az commands below. You will need to change is to replace `MYAPPFUNCTIONSTORE` with something globally unique.
The actual name you pick isn’t important – it’s just a place to store the data for your running functions, and won’t be seen by the users.
You will also need to change `MYFIRSTAPP` to something globally unique.

```bash
# Create a resource group:
az group create --name myResourceGroup --location westeurope

# Create a storage account for storing your function data:
az storage account create --name "MYAPPFUNCTIONSTORE" \
--location westeurope --resource-group myResourceGroup \
--sku Standard_LRS

# Create a function app for grouping your functions together:
az functionapp create --resource-group myResourceGroup --os-type Linux \
--consumption-plan-location westeurope --runtime python \
--name "MYFIRSTAPP" --storage-account "MYAPPFUNCTIONSTORE"
```

```bash
Publish Your Function to Azure
func azure functionapp publish MYFIRSTAPP --build remote
```

4. You can check that your function is deployed properly by creating a POST reguest in Postman using `https://MYFIRSTAPP.azurewebsites.net/api/response_text_processing?code=<USE_YOUR_CODE_HERE>` and confirming that the JSON output is being correctly produced.

## Cleaning up
Don't forget to delete the created resource after the workshop ends! You can delete the resource by using the Azure portal.
Go to Resource Groups, click on the resource you want to delete and click delete resource group.

Or by using Azure CLI, just run the following command in the terminal:

```bash
az group delete --name MyResourceGroup
```

## Where to go next?
Now that you’re here, you’ve successfully written your first Python Azure function and can understand what happens when the function works according
to your expectations — and also what happens when it fails. Like most things, however, this barely scratches the surface of the power of Azure Functions. So what now?

For starters, here are just a few ideas:

* You can build a [serverless web application](https://docs.microsoft.com/en-us/azure/architecture/reference-architectures/serverless/web-app).
* Build, train and deploy a [Machine Learning model](https://azure.microsoft.com/en-us/free/machine-learning/search/?&ef_id=Cj0KCQiA1-3yBRCmARIsAN7B4H1RepL2p1tSCOK06GtxfTVeimTE4Ccc1PJIEiZeV9ku_2GMyAh8a6waAmsNEALw_wcB:G:s&OCID=AID2000098_SEM_L0hncz7b&MarinID=L0hncz7b_369039617729_azure%20machine%20learning_e_c__75540808959_kwd-298261819911&lnkd=Google_Azure_Brand&dclid=CMGAtqLA-ecCFZYK4AodYmUNOA).