azure web应用部署_使用Visual Studio Code将Python应用程序部署到Azure Functions

azure web应用部署

In this article, we are going to build a small python application and deploy it to Azure Functions. The development and the deployment will be done using the Visual Studio (VS) Code. As you might be aware, VS Code is one of the most widely used and preferred code editors for programmers. It is a cross-platform tool, which means you can install this tool on any operating system of your choice, starting from Windows, Linux, or Mac OS.

在本文中,我们将构建一个小型python应用程序并将其部署到Azure Functions。 开发和部署将使用Visual Studio(VS)代码完成。 您可能已经知道,VS Code是程序员使用最广泛且首选的代码编辑器之一。 它是一个跨平台的工具,这意味着您可以从Windows,Linux或Mac OS开始在您选择的任何操作系统上安装此工具。

What makes the VS Code so unique and popular is that there are tons of custom extensions that are developed by the community. These extensions are leveraged and can be extended the default behavior of the code editor as you want.

使得VS Code如此独特和流行的原因在于,社区开发了大量的自定义扩展。 利用这些扩展,可以根据需要扩展代码编辑器的默认行为。

应用程序开发的先决条件
(Pre-requisites for app development
)

Assuming that this article is for a beginner, I will start by listing out all the possible pre-requisites that you need to have in order to deploy your code to Azure Functions from Visual Studio Code.

假定本文是针对初学者的,我将首先列出将代码从Visual Studio Code部署到Azure Functions所需的所有可能的先决条件。

  • Python executable – First things first, since in this article, we are going to learn how to deploy python apps, we need to have the python executable installed in our system. You can head over to Python可执行文件 –首先,由于在本文中,我们将学习如何部署python应用程序,因此我们需要在系统中安装python可执行文件。 您可以转到https://www.python.org/ and download Python for your operating system https://www.python.org/并下载适用于您的操作系统的Python
  • Visual Studio Code – Yes, as we are going to use Visual Studio Code as the code editor and build our app using the same, you need to get this installed and running on your machine as well. Visual Studio Code is a cross-platform tool, and you can get it installed on any operating system. Download Visual Studio Code by visiting Visual Studio Code –是的,因为我们要使用Visual Studio Code作为代码编辑器并使用该应用程序构建我们的应用程序,因此您还需要在计算机上安装并运行它。 Visual Studio Code是一个跨平台工具,您可以将其安装在任何操作系统上。 通过访问http://code.visualstudio.com/ http://code.visualstudio.com/下载Visual Studio Code。
  • Python Extension for Visual Studio Code – By default, when you install Visual Studio Code, you do not have the option to execute python files directly. You need to install an additional extension provided by Microsoft to support working with python files. Download this extension from the marketplace by visiting 适用于Visual Studio Code的Python扩展 –默认情况下,安装Visual Studio Code时,您没有选择直接执行python文件的选项。 您需要安装Microsoft提供的其他扩展程序,以支持使用python文件。 通过访问https://marketplace.visualstudio.com/items?itemName=ms-python.python https://marketplace.visualstudio.com/items?itemName=ms-python.python从市场上下载此扩展程序。
  • Azure Functions Extension for Visual Studio Code – In order to be able to deploy your code to Azure Functions, you need to install the extension as well. You can install the same by visiting 用于Visual Studio Code的Azure Functions扩展 –为了能够将代码部署到Azure Functions,还需要安装扩展。 您可以通过访问https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions安装相同的文件
  • Azure Functions Core Tools – In order to debug and run your functions locally, you need to set up Azure Functions Core Tools as well. You can follow how to install this tool from the Azure Functions核心工具 –为了在本地调试和运行函数,您还需要设置Azure Functions核心工具。 您可以从official documentation 官方文档中了解如何安装此工具
  • A valid Azure Account – You also need to have an Azure account up and running for this. Visit 有效的Azure帐户 –为此,您还需要启动并运行一个Azure帐户。 访问https://portal.azure.com to create your account https://portal.azure.com创建您的帐户

Once you are done with installing all the pre-requisites as mentioned above, you should be good to start with it. Now, the Visual Studio Code looks something like this.

一旦完成了上述所有前提条件的安装,就应该从头开始。 现在,Visual Studio代码看起来像这样。

Functions Icon Added in VS Code

As you can see, an icon for using the Azure Services appears on the left-hand pane. Use this panel to sign-in to your Azure account to proceed forward.

如您所见,用于使用Azure服务的图标出现在左侧窗格中。 使用此面板登录到您的Azure帐户以继续进行操作。

在本地创建项目 (Creating the project locally)

Now that we are able to set up our environment for development let us go ahead and create a new project. Start by clicking on the New Project icon under Azure Functions in the panel and browse a directory to start with.

现在我们已经能够建立我们的开发环境,让我们继续进行并创建一个新项目。 首先,单击面板中“ Azure功能”下的“新建项目”图标,然后浏览要开始的目录。

Create new project

Once you have specified the folder for the project, the next step is to choose the programming language that you want to start with. For the sake of this tutorial, I am going to go ahead and choose Python.

为项目指定文件夹后,下一步就是选择您要开始使用的编程语言。 为了本教程的缘故,我将继续选择Python

Choosing the Language

In the next step, you need to tell the Visual Studio Code, wherein the binary file for the Python. In this step, the VS Code will create a virtual environment for the project and run the python file inside it.

在下一步中,您需要告诉Visual Studio Code,其中Python的二进制文件。 在此步骤中,VS Code将为项目创建一个虚拟环境,并在其中运行python文件。

Selecting Python Interpreter

In the next step, you will be prompted with a choice to select a template to begin with. Templates help you to do the heavy lifting in your application, and you just need to focus on your code. It is absolutely fine if you want to write your own code from scratch, but to keep things simpler and sweet, I am going to select the HTTP Trigger template.

在下一步中,系统将提示您选择开始的模板。 模板可以帮助您完成应用程序中的繁重工作,而您只需要关注代码即可。 如果您想从头开始编写自己的代码,那绝对没问题,但是为了使事情变得简单而甜美,我将选择HTTP触发器模板。

Selecting the template

Now, we have selected our environment and also defined a template that we would like to use. The next step is to add a name to our function. Please note that your Azure Function on the Azure portal will also be created using this name.

现在,我们选择了环境并定义了我们要使用的模板。 下一步是为我们的功能添加名称。 请注意,您还将在Azure门户上使用此名称创建Azure功能。

Function Name

The penultimate step here is to select the authorization level for your Azure function. Although in a production application, there will be securities and authentication involved, for our tutorial, we will go with the Anonymous authentication.

倒数第二步是选择Azure功能的授权级别。 尽管在生产应用程序中会涉及证券和身份验证,但对于我们的教程,我们将使用匿名身份验证。

Selecting Authentication Level

Once the app is created, you can see something like this on your screen.

创建应用程序后,您可以在屏幕上看到类似的内容。

Function created

在本地调试功能 (Debugging the function locally)

Now that our function is ready, we can debug it locally to test if it is working as expected. To debug, click on the debug icon, and select Debug (Attach to Python Functions) or press F5 on your keyboard.

现在我们的功能已经准备就绪,我们可以在本地调试它以测试它是否按预期工作。 要进行调试,请单击调试图标,然后选择“调试”(“ 附加到Python函数” )或按键盘上的F5。

Debug the function locally

As soon as you start debugging, you can see the terminal window is displayed, and the function is hosted locally on a URL. You can open this URL on a browser window. Click to open this URL on the browser.

一旦开始调试,就可以看到显示了终端窗口,并且该函数在本地托管在URL上。 您可以在浏览器窗口中打开此URL。 单击以在浏览器中打开此URL。

URL for localhost

In the browser, you can see something like this.

在浏览器中,您可以看到类似这样的内容。

Function response

You can modify the URL and append “?name=aveek” at the end of the query to test if it is working fine.

您可以修改URL并在查询末尾附加“ ?name = aveek ”,以测试其是否正常运行。

Function response

Now that the function is running as expected on our local, we can go ahead and deploy it on Azure Functions.

现在,该功能已在我们的本地上按预期运行,我们可以继续将其部署在Azure Functions上。

将该功能部署到Azure (Deploying the function to Azure)

To deploy your code to the Function App, click on the Deploy icon, as shown in the figure below.

要将代码部署到Function App,请单击Deploy图标,如下图所示。

Deploy to Azure

You will be prompted with the following two options. I am going to choose the first option with basic settings.

系统将提示您以下两个选项。 我将选择具有基本设置的第一个选项。

Creating the Function App

In the following steps, provide a unique name of the Function App that you want and specify the version of Python. You might also need to provide the name of the region under which you would like the resources to be created in Azure. It might take some time to build up the resources on the cloud, and you can grab some coffee while the function is being deployed.

在以下步骤中,提供所需的Function App的唯一名称并指定Python的版本。 您可能还需要提供要在Azure中创建资源的区域的名称。 在云上建立资源可能需要一些时间,并且在部署功能时可以喝点咖啡。

Function App Name

You can see that the function has been deployed successfully and up for our use. Head over and click the URL that you can see and open it on the browser.

您可以看到该功能已成功部署并且可以使用。 转到并单击您可以看到的URL,然后在浏览器中将其打开。

Function App Deployed

As you might have guessed, the function is now deployed to Azure and works as expected.

您可能已经猜到了,该功能现在已部署到Azure并按预期工作。

Functions Work well

结论 (Conclusion)

In this article, we have seen how to develop python applications using Visual Studio Code locally. I have also explained how we can deploy the same applications to Azure Functions directly from VS Code editor. This is a great way to quickly deploy your app to the cloud without having to leave the code editor. You can easily build your apps locally and verify them. Once these apps are verified in local, you can quickly publish it to the cloud with the help of just a few clicks. This really helps to save a lot of time for the developers and help them focus on the code rather than deploying the changes.

在本文中,我们已经看到了如何使用Visual Studio Code在本地开发python应用程序。 我还解释了如何直接从VS Code编辑器将相同的应用程序部署到Azure Functions。 这是一种无需离开代码编辑器即可将应用程序快速部署到云的好方法。 您可以轻松地在本地构建应用并进行验证。 这些应用在本地验证后,只需单击几下即可将其快速发布到云中。 这确实有助于为开发人员节省大量时间,并帮助他们专注于代码而不是部署更改。

翻译自: https://www.sqlshack.com/deploy-python-apps-to-azure-functions-using-visual-studio-code/

azure web应用部署

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Visual Studio Code是一款轻量级的集成开发环境(IDE),它支持多种编程语言和版本控制系统。如果你想在Visual Studio Code使用TFVC连接Azure DevOps,可以按照以下步骤进行设置: 1. 安装Visual Studio Code:首先,你需要下载并安装Visual Studio Code。你可以从官方网站(https://code.visualstudio.com/)下载适用于你的操作系统的安装程序。 2. 安装Azure Repos扩展:打开Visual Studio Code后,在侧边栏的扩展面板中搜索并安装"Azure Repos"扩展。这个扩展提供了与Azure DevOps集成所需的功能。 3. 登录Azure DevOps:在Visual Studio Code中,按下Ctrl+Shift+P(或者Cmd+Shift+P)打开命令面板,然后输入"Azure Repos: Sign in"并选择该命令。按照提示登录到你的Azure DevOps帐户。 4. 克隆TFVC存储库:在Visual Studio Code中,按下Ctrl+Shift+P(或者Cmd+Shift+P)打开命令面板,然后输入"Azure Repos: Clone TFVC Repository"并选择该命令。输入你要克隆的TFVC存储库的URL,并选择本地存储库的位置。 5. 连接到Azure DevOps:在Visual Studio Code中,按下Ctrl+Shift+P(或者Cmd+Shift+P)打开命令面板,然后输入"Azure Repos: Connect to Team Foundation Server"并选择该命令。输入你的Azure DevOps服务器的URL,并选择你要连接的项目。 6. 进行版本控制操作:现在,你可以在Visual Studio Code使用TFVC进行版本控制操作了。你可以查看、检出、提交和合并代码等。 希望以上步骤对你有所帮助!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值