无法显示实时数据,同时Python Web App正在生成错误“内部服务器错误”

2024-06-25 05:34:39 发布

您现在位置:Python中文网/ 问答频道 /正文

应用程序正在实时接收数据(使用警报进行检查),而要在卡上显示数据,它会给出错误Internal Server Error

同样的代码作为一个独立的html页面运行良好,但不是在flask python web应用程序中

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css"
          integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ"
          crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
          crossorigin="anonymous">
    <title>Cosmos DB Change Feed</title>
    <style>

        .fade-enter-active {
            transition: all 1.5s ease;
        }

        .fade-enter, .fade-leave-to {
            opacity: 0;
        }
    </style>
</head>
<body>
    Welcome new
    <div class="container" id="app">
        <div class="row">
            <div v-for="flight in flights" class="col-md-6 col-lg-4 col-xl-3" style="margin: 16px 0px;">
                <div class="card">
                    <div class="card-body">
                        <h4 class="card-title">{{ flight.from }} <i class="fas fa-plane"></i> {{ flight.to }}</h4>
                        <transition name="fade" mode="out-in">
                            <h4 class="card-subtitle mb-2" :key="flight.price">${{ flight.price }}</h4>
                        </transition>
                    </div>

                </div>
            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
    <script src="https://unpkg.com/@aspnet/signalr@1.0.2/dist/browser/signalr.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    <script>

        const apiBaseUrl = 'http://localhost:7071'
        const axiosConfig = {}
        const data = {
            flights: []
        }
        const app = new Vue({
            el: '#app',
            data: data
        })
        getFlights().then(function (flights) {
            flights.forEach(flightUpdated)
        }).then(getConnectionInfo).then(function (info) {
            let accessToken = info.accessToken
            const options = {
                accessTokenFactory: function () {
                    if (accessToken) {
                        const _accessToken = accessToken
                        accessToken = null
                        return _accessToken
                    } else {
                        return getConnectionInfo().then(function (info) {
                            return info.accessToken
                        })
                    }
                }
            }

            const connection = new signalR.HubConnectionBuilder()
                .withUrl(info.url, options)
                .build()

            connection.on('flightUpdated', flightUpdated)

            connection.onclose(function () {
                console.log('disconnected')
                setTimeout(function () { startConnection(connection) }, 2000)
            })
            startConnection(connection)

        }).catch(console.error)

        function startConnection(connection) {
            console.log('connecting...')
            connection.start()
                .then(function () { console.log('connected!') })
                .catch(function (err) {
                    console.error(err)
                    setTimeout(function () { startConnection(connection) }, 2000)
                })
        }

        function getFlights() {
            return axios.post(`${apiBaseUrl}/api/GetFlights`, null, axiosConfig)
                .then(function (resp) { return resp.data })
                .catch(function () { return {} })
        }

        function getConnectionInfo() {
            return axios.post(`${apiBaseUrl}/api/SignalRInfo`, null, axiosConfig)
                .then(function (resp) { return resp.data })
                .catch(function () { return {} })
        }

        function flightUpdated(updatedFlight) {
            const flight = data.flights.find(f => (f.to === updatedFlight.to && f.from === updatedFlight.from))
            //const flight = data.flights.find(f =>f.id === updatedFlight.id)
            if (flight) {
               // alert(updatedFlight.price);
                //Vue.set(flight, 'from', updatedFlight.from)
                //  Vue.set(flight, 'to', updatedFlight.to)
                Vue.set(flight, 'price', updatedFlight.price)
            } else {
               //  alert(updatedFlight.price);
                data.flights.push(updatedFlight)
            }
        }
    </script>
</body>
</html>

预期结果是卡片格式的航班详细信息(从、到、价格)。我相信这是由于{{flight.to}}语法,我不知道如何替换它


Tags: todivdatareturnscriptfunctionconnectionprice
1条回答
网友
1楼 · 发布于 2024-06-25 05:34:39

如果应用程序出现内部服务器错误,则可能是在生产模式下运行。 在您的开发机器上, 如果您正在使用app.run到应用,请更改

app.run()

app.run(debug=True) 

但是,如果您正在使用

flask run

然后,根据您的系统,您将需要添加一个环境变量,有关它的信息将通过其他配置很好地解释

Flask Environment Configuration

当您启用调试模式时,内置调试器将准确地告诉您在哪里遇到问题

相关问题 更多 >