Flask将Pandas DataFrame返回给AJAX - Errno 10054

时间:2015-11-05 23:24:25

标签: python ajax pandas flask dataframe

我正在尝试创建一个简单的Web应用程序,它将在用户在我创建的表单中定义股票代码后返回历史股票数据。我的方法是提供一个表单,用户可以在其中指定库存符号并使用AJAX将该文本发送回服务器。一旦在服务器端收到文本,我就会使用Pandas功能从Yahoo Finance中检索历史股票数据。

我能够在服务器上接收文本,检索历史库存数据,但是当我尝试将其发送回我的A​​JAX函数时,我收到以下错误消息:

Exception happened during processing of request from ('127.0.0.1', 54959)
----------------------------------------
127.0.0.1 - - [06/Nov/2015 06:12:07] "POST /get_stock_data HTTP/1.1" 200 -
Traceback (most recent call last):
  File "C:\Python27\lib\SocketServer.py", line 599, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Python27\lib\SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Python27\lib\SocketServer.py", line 657, in __init__
    self.finish()
  File "C:\Python27\lib\SocketServer.py", line 716, in finish
    self.wfile.close()
  File "C:\Python27\lib\socket.py", line 279, in close
    self.flush()
  File "C:\Python27\lib\socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10054] An existing connection was forcibly closed by the remote host

我尝试了许多不同的方法来发送数据,但我总是收到错误。据我所知,使用DataReader功能检索库存数据时会导致问题。如果我排除我可以来回检索和发送任何内容。

HTML:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">

        <script type=text/javascript>$SCRIPT_ROOT = {{ request.script_root|tojson|safe }};</script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="{{ url_for('static', filename='js/jquery-ui.min.js') }}"></script>
        <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/styles.css') }}">                   

    </head>


    <body>
        <div>
            <form>
                <input type="text" value="Symbol" name="Symbol" id="symbol">
                <br>
                <input type="submit" value="Submit">
            </form>

            <div id="container"></div>

            <script>
                $(function () {
                    $('form').bind('submit', function() {
                        $.ajax( {
                            type: 'post',
                            url: '/get_stock_data',
                            data: $('form').serialize(),
                            dataType: "json",
                            success: function result) {
                                console.log("success")                          
                            },

                            error: function() {
                                console.log("failure has occured")
                            }
                        })
                    });
                });

            </script>   
        </div>

    </body>
</html>

烧瓶中:

from flask import Flask, render_template, request, jsonify

import pandas.io.data as web

app = Flask(__name__)

def getStockData(symbol):
    stock = web.DataReader(symbol, 'yahoo')
    return stock

@app.route('/')
def main():
    return render_template('main.html')

@app.route('/get_stock_data', methods=['GET', 'POST'])
def get_symbol():
    sym = request.form['Symbol']

    stock = getStockData(sym)

    return jsonify(result=stock)


if __name__ == '__main__':
    app.run(threaded=True)

任何人都可以解释这个错误的含义以及我如何解决它?任何帮助表示赞赏。

0 个答案:

没有答案