I have created a very basic python flask app for posting/getting files via html page that is rendered with render_template() function. Everything is working fine on localhost the problem came when I tried to deploy it to azure cloud. Everything was deployed successfully but when I open the link ( https://timakrest.azurewebsites.net/ ) it says not found. I had the problem on localhost when the render function couldnt find the index.html..
path = str(pathlib.Path(__file__).parent.absolute()) + '\\templates'
app = Flask(__name__, template_folder=path)
@app.route('/')
def upload_form():
return render_template('index.html')
The path is set up for the template folder (it is probably also the default setting of the flask but I wanted to tried..) I tried the dynamic absolute path as mentioned and static one as well but i somehow cannot figure it out..
I also somehow cannot find the python output in the cloud so I am not 100% sure its the.html thing but most probably..
Could you please help me? ^^ thank you.
If you want to deploy the python flask app from Azure Pipeline, you could refer to the following documentation:
https://docs.microsoft.com/en-us/azure/devops/pipelines/ecosystems/python-webapp?view=azure-devops
The first task under Build stage is UsePythonVersion, which specifies the version of Python to use on the build agent. hen we have script-based task that creates a virtual environment and installs dependencies from file (requirements.txt).
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '$(pythonVersion)'
displayName: 'Use Python $(pythonVersion)'
- script: |
python -m venv antenv
source antenv/bin/activate
python -m pip install --upgrade pip
pip install setup
pip install -r requirements.txt
workingDirectory: $(projectRoot)
displayName: "Install requirements"
Next step creates the.zip file that the steps under deploy stage of the pipeline deploys. Then we have the task to upload the artifacts.
- task: ArchiveFiles@2
inputs:
rootFolderOrFile: '$(Build.SourcesDirectory)'
includeRootFolder: false
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/Application$(Build.BuildId).zip'
replaceExistingArchive: true
verbose: # (no value); this input is optional
- publish: $(Build.ArtifactStagingDirectory)/Application$(Build.BuildId).zip
displayName: 'Upload package'
artifact: drop
Important :
When deploying to Azure App Service, be sure to use
includeRootFolder: false
. Otherwise, the contents of the.zip file are put in a folder named s, for "sources," which is replicated on the App Service. The App Service on Linux container then can't find the app code.
n the Deploy stage, we use the deployment
keyword to define a deployment job targeting an environment.
jobs:
- deployment: DeploymentJob
pool:
vmImage: $(vmImageName)
environment: $(environmentName)
strategy:
runOnce:
deploy:
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '$(pythonVersion)'
displayName: 'Use Python version'
- task: AzureWebApp@1
displayName: 'Deploy Azure Web App : {{ webAppName }}'
inputs:
azureSubscription: $(azureServiceConnectionId)
appName: $(webAppName)
package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip
# The following parameter is specific to the Flask example code. You may
# or may not need a startup command for your app.
startUpCommand: 'gunicorn --bind=0.0.0.0 --workers=4 startup:app'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.