Abdelkrim
Abdelkrim

Reputation: 2158

FLASK : plotly : Error: module 'index' has no attribute 'app.server'

the context

I am debugging an application using Visual Studio Code (VSCode).

Breakpoints ARE NOT hit!

I would like VSCode to stop on my breakpoints when necessary

**What is the correct configuration for launch.json to hit the breakpoints? **

Thank you for the time you are investing helping me!

the hierarchy of the project

  • launch.json
  • index.py See [4]
  • app.py See [3]
  • pages
    • index.py
    • transactions.py

the issue : Error: module 'index' has no attribute 'app.server'

The Error message is displayed after clicking on 'Start debugging > F5' = Error: module 'index' has no attribute 'app.server'

I tried dozens of ways to set the "FLASK_APP": "index:app.server" but they generate diverse error messages :

for information : gunicorn command (working)

here is a command available in azure-pipelines.yml running the plotly app :

attached files

[1] launch.json - non working

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Flask",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "index:app.server",
                "FLASK_ENV": "development",
                "FLASK_DEBUG": "1",
                "FLASK_RUN_PORT": "8052"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "jinja": true
        }
    ]
}

[2] launch.json - working but breakpoints are not hit

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${workspaceRoot}\\index.py",
            "console": "integratedTerminal"
        }
    ]
}

[3] webapp.py

# -*- coding: utf-8 -*-
import dash

app = dash.Dash(
    __name__, meta_tags=[{"name": "viewport",
                          "content": "width=device-width, initial-scale=1"}]
)
server = app.server
app.config.suppress_callback_exceptions = True

index.py - root of the application

# -*- coding: utf-8 -*-
import dash_html_components as html
import dash_core_components as dcc
from webapp import app
from dash.dependencies import Input, Output
from pages import (
    transactions, index)


# Describe the layout/ UI of the app
app.layout = html.Div([
    dcc.Location(id="url", refresh=False),
    html.Div(id="page-content")
])


# Update page
@app.callback(Output("page-content", "children"),
              [Input("url", "pathname")])
def display_page(pathname):
    if pathname == "/dash/index":
        return index.layout
    if pathname == "/dash/transactions":
        return transactions.layout
    else:
        return index.layout


if __name__ == "__main__":
    app.run_server(debug=True, port=8051)

Upvotes: 1

Views: 603

Answers (1)

Brett Cannon
Brett Cannon

Reputation: 16070

Your [1] example isn't working because you set FLASK_APP to index:app.server which tries to find an attribute named app.server on the index module. Attribute names can't have a dot (you can verify this by importing that module and trying out getattr(index, "app.server")). You should be able to make FLASK_APP simply say index to have it work.

See the Flask documentation on app discovery for more details.

Upvotes: 1

Related Questions