0

我按照以下说明进行操作:

run() 方法很适合启动本地开发服务器,但您必须在每次更改代码后手动重新启动它。这不是很好,Flask 可以做得更好。如果您启用调试支持,服务器将在代码更改时自行重新加载,并且如果出现问题,它还会为您提供有用的调试器。

我设置:

app.run(debug=True)

但是调试模式似乎仍然没有被激活。可能是什么原因?

这是代码(仅一个文件):

import os
import glob

import pandas as pd
from flask import Flask

from regex_file_cleaner import clean_csv_file

app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
APP_STATIC = os.path.join(APP_ROOT, 'static')


@app.route('/1/1')
def task1():

    array = [['a', 'b', 'c'], ['d', 'e', 'f']]
    result = ' '.join([' '.join(i) for i in array])
    return ("<pre>' '.join([' '.join(i) for i in array])</pre>"
            " applied to:<pre>%s</pre>results in: %s") % (str(array), result)


@app.route('/1/2')
def task2():

    dataframes_collection = []

    os.chdir(APP_STATIC+"/some/path")

    for csv_file in glob.glob("*.csv"):

        country_code = csv_file.split(' ')[1].split('.')[0]
        start, end = clean_csv_file(csv_file)
        df = pd.read_csv(csv_file, sep='\t', header=False, skiprows=start, parse_dates=True)[:end]
        df['Country'] = country_code
        dataframes_collection.append(df)

    concatenated = pd.concat(dataframes_collection, ignore_index=True)

    raport = "<h1>Raw data</h1> %s <h1>Summary of the day.</h1> %s" % (
        concatenated.to_html(),
        concatenated.groupby(
            ['Days'])['Page Impressions', 'Visits', 'Bounces'].sum().to_html(
            justify='right'))

    return raport


@app.route('/')
def hello_world():

    return 'Hello World!'


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

这是我的输出pip freeze

Flask==0.10.1
Jinja2==2.7.3
MarkupSafe==0.23
Werkzeug==0.9.6
backports.ssl-match-hostname==3.4.0.2
certifi==14.05.14
itsdangerous==0.24
matplotlib==1.3.1
nose==1.3.3
numpy==1.8.1
pandas==0.14.1
pyparsing==2.0.2
python-dateutil==2.2
pytz==2014.4
scipy==0.14.0
six==1.7.3
tornado==4.0
wsgiref==0.1.2

控制台输出:

(rocket)➜  demo  python test.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
127.0.0.1 - - [02/Aug/2014 19:50:49] "GET /1/2 HTTP/1.1" 404 -
127.0.0.1 - - [02/Aug/2014 19:50:51] "GET /1/2 HTTP/1.1" 404 -
127.0.0.1 - - [02/Aug/2014 19:50:54] "GET /1 HTTP/1.1" 404 -
127.0.0.1 - - [02/Aug/2014 19:50:56] "GET /1/1 HTTP/1.1" 200 -
 * Detected change in 'test.py', reloading
 * Restarting with reloader
127.0.0.1 - - [02/Aug/2014 19:53:37] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 19:53:41] "GET /1/2 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 19:53:54] "GET /1/2 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 19:53:57] "GET /1/2 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 19:54:06] "GET /1/2 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 19:54:47] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 19:54:54] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 19:55:00] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 19:58:29] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 19:59:15] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 19:59:16] "GET /1/1 HTTP/1.1" 200 -
 * Detected change in '/Users/andi/FLASK/demo/regex_file_cleaner.py', reloading
 * Restarting with reloader
 * Detected change in '/Users/andi/FLASK/demo/regex_file_cleaner.py', reloading
 * Restarting with reloader
 * Detected change in 'test.py', reloading
 * Restarting with reloader
 * Detected change in 'test.py', reloading
 * Restarting with reloader
 * Detected change in 'test.py', reloading
 * Restarting with reloader
 * Detected change in 'test.py', reloading
 * Restarting with reloader
 * Detected change in 'test.py', reloading
 * Restarting with reloader
 * Detected change in 'test.py', reloading
 * Restarting with reloader
 * Detected change in 'test.py', reloading
 * Restarting with reloader
 * Detected change in 'test.py', reloading
 * Restarting with reloader
127.0.0.1 - - [02/Aug/2014 20:00:47] "GET /1/2 HTTP/1.1" 200 -



127.0.0.1 - - [02/Aug/2014 20:02:44] "GET /1/2 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 20:04:38] "GET /1/2 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 20:04:42] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 20:05:06] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 20:05:08] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 20:05:14] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 20:05:17] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 20:05:21] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 20:06:10] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 20:07:02] "GET /1/1 HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2014 20:07:04] "GET /1/2 HTTP/1.1" 200 -
^C%     

更新:

我注意到在主文件中的更改未被识别,但对文件所做的更改regex_file_cleaner.py总是被识别。

4

0 回答 0