Beispiel #1
0
def age_seq():
    if request.method == 'GET':
        mpl.rc('font', family='Malgun Gothic')
        mpl.rc('axes', unicode_minus=False)
        menu = {'ho':0, 'da':1, 'ml':0, 'se':0, 'co':1, 'cg':0, 'cr':0, 'st':0, 'wc':0,'cf':0, 'ac':0, 're':0, 'cu':0,'nl':0}
        rows = dm.get_agender_items_by_gubun('stdDay, confCase', '0-9')
        adf = pd.DataFrame(rows, columns=['기준일','0-9'])
        adf = cu.get_daily(adf, '0-9', '0-9세')
        age_dict = {'10-19':'10-19세', '20-29':'20-29세', '30-39':'30-39세', '40-49':'40-49세',
                    '50-59':'50-59세', '60-69':'60-69세', '70-79':'70-79세', '80 이상':'80세이상'}
        for key, value in age_dict.items():
            rows = dm.get_agender_items_by_gubun('stdDay, confCase', key)
            tdf = pd.DataFrame(rows, columns=['기준일', key])
            tdf = cu.get_daily(tdf, key, value)
            adf = pd.merge(adf, tdf, on='기준일')
        adf['기준일'] = pd.to_datetime(adf['기준일'])
        adf.set_index('기준일', inplace=True)

        age_str = request.args.get('age', '20-29세 50-59세 60-69세')
        age_list = age_str.split()
        img_file = os.path.join(current_app.root_path, 'static/img/covid_age_seq.png')
        plt.figure(figsize=(12,8))
        for age in age_list:
            adf[age].plot(grid=True)
        plt.title('연령별 확진자 추이', fontsize=15)
        plt.legend()
        plt.savefig(img_file)
        mtime = int(os.stat(img_file).st_mtime)
        age_str = ', '.join(age for age in age_list)
        age_list = ['0-9세', '10-19세', '20-29세', '30-39세', '40-49세',
                    '50-59세', '60-69세', '70-79세', '80세이상']

        return render_template('covid/age_seq.html', menu=menu, weather=get_weather_main(),
                                mtime=mtime, age_list=age_list, age_str=age_str)

    else:
        age_list = request.form.getlist('age')
        #print(age_list)
        age_str = ' '.join(age for age in age_list)
        return redirect(url_for('covid_bp.age_seq')+f'?age={age_str}')
def age_seq():
    if request.method == 'GET':
        mpl.rc('font', family='Malgun Gothic')
        mpl.rc('axes', unicode_minus=False)

        start_date = request.args.get('startDate', '2020-01-01')
        end_date = request.args.get('endDate',
                                    date.today().strftime('%Y-%m-%d'))
        rows = dm.get_agender_items_by_gubun_with_date('stdDay, confCase',
                                                       '0-9', start_date,
                                                       end_date)
        adf = pd.DataFrame(rows, columns=['기준일', '0-9'])
        adf = cu.get_daily(adf, '0-9', '0-9세')
        age_dict = {
            '0-9': '0-9세',
            '10-19': '10-19세',
            '20-29': '20-29세',
            '30-39': '30-39세',
            '40-49': '40-49세',
            '50-59': '50-59세',
            '60-69': '60-69세',
            '70-79': '70-79세',
            '80 이상': '80세이상'
        }
        for key, value in age_dict.items():
            rows = dm.get_agender_items_by_gubun_with_date(
                'stdDay, confCase', key, start_date, end_date)
            tdf = pd.DataFrame(rows, columns=['기준일', key])
            tdf = cu.get_daily(tdf, key, value)
            adf = pd.merge(adf, tdf, on='기준일')
        adf['기준일'] = pd.to_datetime(adf['기준일'])
        adf.set_index('기준일', inplace=True)

        age_str = request.args.get('age', '20-29세 50-59세 60-69세')
        age_list = age_str.split()
        img_file = os.path.join(current_app.root_path,
                                'static/img/covid_age_seq.png')
        plt.figure(figsize=(12, 7))
        for age in age_list:
            adf[age].plot(grid=True)
        plt.title('연령별 확진자 추이', fontsize=15)
        plt.legend()
        plt.savefig(img_file)
        mtime = int(os.stat(img_file).st_mtime)
        age_str = ', '.join(age for age in age_list)
        age_list = [
            '0-9세', '10-19세', '20-29세', '30-39세', '40-49세', '50-59세', '60-69세',
            '70-79세', '80세이상'
        ]

        return render_template('covid/age_seq.html',
                               menu=menu,
                               weather=get_weather_main(),
                               mtime=mtime,
                               age_list=age_list,
                               age_str=age_str,
                               start_date=start_date,
                               end_date=end_date)

    else:
        start_date = request.form['startDate'] if request.form[
            'startDate'] else '2020-01-01'
        end_date = request.form['endDate'] if request.form[
            'endDate'] else date.today().strftime('%Y-%m-%d')
        age_list = request.form.getlist('age') if request.form.getlist(
            'age') else ['20-29세', '50-59세', '60-69세']
        age_str = ' '.join(age for age in age_list)
        return redirect(
            url_for('covid_bp.age_seq') +
            f'?age={age_str}&startDate={start_date}&endDate={end_date}')