Giter Site home page Giter Site logo

microsoft / python-sample-vscode-flask-tutorial Goto Github PK

View Code? Open in Web Editor NEW
324.0 32.0 4.6K 52 KB

Sample code for the Flask tutorial in the VS Code documentation

Home Page: https://code.visualstudio.com/docs/python/tutorial-flask

License: MIT License

Python 29.20% CSS 10.35% HTML 34.17% Dockerfile 26.27%

python-sample-vscode-flask-tutorial's People

Contributors

cwebster-99 avatar dependabot[bot] avatar feyghub avatar halderex avatar harshsahu97 avatar hyunchel avatar jhamukesh998 avatar jlintusaari avatar kraigb avatar microsoft-github-policy-service[bot] avatar microsoftopensource avatar msftgits avatar richaagrawa avatar swedishmike avatar timpreble avatar tonybaloney avatar uchenkadicode avatar yashsahugtx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-sample-vscode-flask-tutorial's Issues

Specify Browser - Firefox & flask

How can I define the browser to launch with?

from this I am using.

{
            "name": "Python: Flask (0.11.x or later)",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "microblog"                
            },
            "args": [
                "run",
                "--no-debugger",
                // "--no-reload"      // Remove to auto-reload modified pages
            ]
        },

If I want to use firefox or the forefox extension for vscode as my default development browser how do I add it to the launch configuration?

Specifically this extension.
https://marketplace.visualstudio.com/items?itemName=firefox-devtools.vscode-firefox-debug

Which has as its launch code

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch localhost",
            "type": "firefox",
            "request": "launch",
            "reAttach": true,
            "url": "http://localhost/index.html",
            "webRoot": "${workspaceFolder}"
        }
    ]
}
``

However I cannot seem to get both flask and firefox together in the same debug configuration.

pytest fails with exit code 5

I just created the starter pipeline like described here. The pipeline fails with a warning and a result of 5.

PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0.
Add 'junit_family=xunit1' to your pytest.ini file to keep the current format in future versions of pytest and silence this warning.

Installing collected packages: py, wcwidth, attrs, zipp, importlib-metadata, pluggy, more-itertools, six, pyparsing, packaging, pytest, pytest-azurepipelines
Successfully installed attrs-19.3.0 importlib-metadata-1.6.1 more-itertools-8.3.0 packaging-20.4 pluggy-0.13.1 py-1.8.1 pyparsing-2.4.7 pytest-5.4.3 pytest-azurepipelines-0.8.0 six-1.15.0 wcwidth-0.2.4 zipp-3.1.0
##[warning]The 'junit_family' default value will change to 'xunit2' in pytest 6.0.
Add 'junit_family=xunit1' to your pytest.ini file to keep the current format in future versions of pytest and silence this warning.
============================= test session starts ==============================
platform linux -- Python 3.6.10, pytest-5.4.3, py-1.8.1, pluggy-0.13.1
rootdir: /home/vsts/work/1/s
plugins: azurepipelines-0.8.0
collected 0 items
No Result Found to Publish '/home/vsts/work/1/s/test-output.xml'.
Skipping uploading of coverage data.

=============================== warnings summary ===============================
/opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site-packages/_pytest/junitxml.py:417
  /opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site-packages/_pytest/junitxml.py:417: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0.
  Add 'junit_family=xunit1' to your pytest.ini file to keep the current format in future versions of pytest and silence this warning.
    _issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/latest/warnings.html
----------- generated xml file: /home/vsts/work/1/s/test-output.xml ------------
============================== 1 warning in 0.02s ==============================

##[error]Bash exited with code '5'.

Tutorial doesn't work

I've forked this tutorial and attempted to deploy the Flask app via Github Actions, but I'm still seeing the default Microsoft app.
When SSH'ing into the machine, I can see that there's an archive with the contents of my repository under.
/home/site/wwwroot
The archive never ends up being decompressed, is that expected? Looking at the logs, decompression is never even attempted:

{
      "log_time":"2021-06-15T02:30:22.6898161Z",
      "id":"",
      "message":"Copied the compressed output to '/home/site/wwwroot'",
      "type":0,
      "details_url":null
   },
   [...]
   {
      "log_time":"2021-06-15T02:30:22.7293111Z",
      "id":"",
      "message":"Removing existing manifest file",
      "type":0,
      "details_url":null
   },
  [... nothing about un-taring ... ]

Breakpoints are not working

I'm new to Python Flask development and VS Code. I've been trying to create an app by referring the below tutorials.

https://code.visualstudio.com/docs/python/tutorial-flask

https://github.com/Microsoft/python-sample-vscode-flask-tutorial\

https://code.visualstudio.com/docs/python/debugging

Repo for reference: https://github.com/iamshivprasad/analytics

Cloned folder structure is:

  • Development
    • .vscode
      • launch.json
    • analyticspyengine
      • analytics_modules
      • controllers
      • datainterfaces
      • utils
      • init.py
      • analyticsservices.py
      • webapp.py
        .
        .
        .

I am able to launch the program successfully with below settings in launch.json.

 {
    "name": "Python: Flask",
    "type": "python",
    "request": "launch",
    "module": "flask",
    "env": {
        "FLASK_APP": "analyticspyengine.webapp",
        "FLASK_DEBUG": true,
        "FLASK_ENV": "development"
    },
    "args": [
        "run"
    ],
    "jinja": true
}

Content of webapp.py

#!/usr/bin/python3
import sys
from . import app   
from . import analyticsservices

Content of analyticspyengine/init.py

#!/usr/bin/python3
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__)))

from flask import Flask
app = Flask(__name__) 

I'm facing the following problems. Completely helpless and frustrated!!!

  1. Execution is not hitting any breakpoints

  2. After launching the app with the above settings, I'm not able to stop the service. i.e, Even after stopping the debugger, the app seems to be running. I can see the requests are being served through terminal output and Postman. The process seems to be alive until VS Code is closed.

Is it an issue? Could someone shed some light?

issue in __init__.py

I build image via dockerfile

When container start i see error in container.

Traceback (most recent call last):
File "./hello_app/init.py", line 1, in
from flask import Flask # Import the Flask class
ModuleNotFoundError: No module named 'flask'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. GAME OVER ***

Tutorial doesn't work out-of-the-box

I've forked this tutorial and attempted to deploy the Flask app via Github Actions, but I'm still seeing the default Microsoft app.

When SSH'ing into the machine, I can see that there's an archive with the contents of my repository under /home/site/wwwroot:

ls /home/site/wwwroot
hostingstart.html oryx-manifest.toml output.tar.gz

The archive never ends up being decompressed, is that expected? Looking at the logs, decompression is never even attempted:

{
      "log_time":"2021-06-15T02:30:22.6898161Z",
      "id":"",
      "message":"Copied the compressed output to '/home/site/wwwroot'",
      "type":0,
      "details_url":null
   },
   [...]
   {
      "log_time":"2021-06-15T02:30:22.7293111Z",
      "id":"",
      "message":"Removing existing manifest file",
      "type":0,
      "details_url":null
   },
  [... nothing about un-taring ... ]

Is the tutorial out of date, or is the deployment tooling broken?

python-flask container example not compatible with AAD express settings

Thanks for the python-sample-vscode-flask-tutorial. Successfully followed the tutorial, and also deployment of the resulting container (following e.g. Deploy Python using Docker containers).

Observed behaviour:

The example Azure web app for containers works as expected when AAD with express settings is not enabled in the app. When AAD express is turned on, the app is correctly redirecting to login (https://login.microsoftonline.com) when not authenticated, but when redirected back after successful login, the app returns HTTP ERROR 500.

Expected behaviour:

The Azure python-sample-vscode-flask-tutorial container example works with express settings in App service AAD enabled, or the README.md contains references/information regarding changes needed for express web app AD service settings to work.

How to reproduce:

  • Download this example repository.
  • Upload and build corresponding container using Azure Container Registry.
  • Create an "Azure Web app for Containers", and link it to the container. Add the WEBSITES_PORT=5000 application settings as described in the Dockerfile.
  • The app works as expected at this step.
  • Turn on basic Azure App Service Authentication / Authorization (e.g. using Azure AD) using Express settings.
  • Unauthenticated users are now still as expected redirected to login for authentication, however on redirect back to the app the server now returns HTTP ERROR 500.

PEP8 and other issues

Please fix them both in the example project here and in the online tutorial. :)

PEP8

  • spaces around the = of keywords args - PEP8 forbids spaces there
  • inconsistent indentation (some function bodies are indented with 8 spaces)
  • local imports for no reason (datetime in hello_there) - not directly a PEP8 violation but local stdlib imports don't really make sense

Flask stuff

  • please consider removing the example with the hardcoded HTML. some people WILL think it's OK to do that even if you mention that usually templates are used. also, that hardcoded-html example is vulnerable to XSS.
  • href="/static/site.css" - use href="{{ url_for('static', filename='site.css')}}"
  • message="Hello there, " + name + "!", - usually you would pass just the name to the template and then build the message in there. So I would change this, and also do the strftime call in the template (ideally someone would use e.g. flask-babel but that's out of the scope of a simple tutorial)

Note: I'm one of the flask maintainers, and I can assure you that people WILL assume things are good / ok / the way to go when part of a tutorial. I've seen my share of bad practices from code people posted in our IRC channel.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.