コード例 #1
0
ファイル: views.py プロジェクト: yash312312/FlaskApp
def patna_route():
    if not table_exists('patna_data'):
        session['city'] = 'patna'
        return redirect(url_for('form_shop'))
    if 'sd' in session:
        session.pop('sd', None)
    if 'ed' in session:
        session.pop('ed', None)
    if request.method == 'POST':
        if request.form['btn'] == 'add':
            session['city'] = 'patna'
            session['state'] = 'bihar'
            city_t = pd.read_sql_table('patna_data', con=db)
            session['date'] = '%s' % city_t.columns[1]
            return redirect(url_for('form_product'))
        if request.form['btn'] == 'delete':
            product_to_delete = request.form.get('delete_product')
            delete_product('patna', product_to_delete)
    city_t = pd.read_sql_table('patna_data', con=db)
    city_t.replace(to_replace=[None], value='0', inplace=True)
    city_t.replace(to_replace=np.nan, value='0', inplace=True)
    state_t = pd.read_sql_table('bihar_data', con=db)
    product_in_city = set(z[9:-6] for z in city_t['Shops and Units'][1:])
    revenue = 0
    city_t2 = total_revenue('patna', 'bihar')

    if request.method == 'POST':
        if request.form['btn'] == 'revenue':
            if 'sd' in session:
                session.pop('sd', None)
            if 'ed' in session:
                session.pop('ed', None)
            d1 = request.form.get('start_date')
            d2 = request.form.get('end_date')
            session['sd'] = d1
            session['ed'] = d2
            if not d1 or not d2:
                revenue = 'Illegal Date'
            elif pd.Timestamp(session['sd']) > pd.Timestamp(session['ed']):
                revenue = 'Illegal Date'
            else:
                revenue = calc_revenue(city_t2, d1, d2)
    return render_template('patna.html',
                           tables=[
                               city_t.to_html(classes='table table-bordered',
                                              col_space=150,
                                              index=False,
                                              table_id='dataTable',
                                              justify='center'),
                               city_t2.to_html(classes='table table-bordered',
                                               col_space=150,
                                               index=False,
                                               table_id='dataTable',
                                               justify='center')
                           ],
                           titles=city_t.columns.values,
                           listOfPro=product_in_city,
                           revenue=revenue,
                           sd=session['sd'] if 'sd' in session else None,
                           ed=session['ed'] if 'ed' in session else None)
コード例 #2
0
ファイル: views.py プロジェクト: yash312312/FlaskApp
def state_input_route():
    if 'state_name' in session:
        state = session['state_name']
        if not table_exists('%s_data' % state):
            cursor.execute(
                "CREATE TABLE %s_data (name VARCHAR(255) , cost DOUBLE)" %
                state)
            connection.commit()
        temp = pd.read_sql_table('%s_data' % state, con=db)
        if request.method == 'POST':
            product_name = request.form.get('product_name')
            product_cost = request.form.get('product_cost')

            if product_name and product_cost and product_name not in list(
                    temp['name']):
                error = 'Invalid Values'
                cursor.execute('''INSERT into %s_data (name, cost)
                                             values ('%s', %s)''' %
                               (state, product_name, product_cost))
                connection.commit()
            print('''INSERT into %s_data (name, cost) values ('%s', %s)''' %
                  (state, product_name, product_cost))
            temp = pd.read_sql_table('%s_data' % state, con=db)
        return render_template('state_input.html',
                               tables=[
                                   temp.to_html(classes='table table-bordered',
                                                col_space=150,
                                                index=False,
                                                table_id='product_table',
                                                justify='center')
                               ],
                               titles=temp.columns.values,
                               state=state.upper())
    else:
        return redirect(url_for('home'))
コード例 #3
0
ファイル: views.py プロジェクト: yash312312/FlaskApp
def city_input_route():
    if 'city_name' in session:
        city_data = None
        city = session['city_name']
        if table_exists('%s_data' % city):
            city_data = pd.read_sql_table('%s_data' % city, con=db)
            city_data.replace(to_replace=[None], value='0', inplace=True)
            city_data.replace(to_replace=np.nan, value='0', inplace=True)
        else:
            session['city'] = city
            return redirect(url_for('form_shop'))
        # print(city_data)
        if request.method == 'POST':
            if request.form['btn'] == 'upload':
                f = request.files['city_data']
                f.save(
                    os.path.join(app.config['UPLOAD_FOLDER'],
                                 '%s_data' % city))
                city_data = pd.read_excel(
                    os.path.join(app.config['UPLOAD_FOLDER'],
                                 '%s_data' % city))
                city_data.set_index('Shops and Units', inplace=True)
                city_data = update_data(city_data)

                city_data.to_sql(name='%s_data' % city,
                                 con=db,
                                 if_exists='replace',
                                 index_label='Shops and Units')
                city_data.reset_index(inplace=True)

        return render_template('city_input.html',
                               tables=[
                                   city_data.to_html(
                                       classes='table table-bordered',
                                       col_space=150,
                                       index=False,
                                       table_id='product_table',
                                       justify='center')
                               ],
                               titles=city_data.columns.values,
                               city=city.upper())
    else:
        return redirect(url_for('home'))
コード例 #4
0
ファイル: views.py プロジェクト: yash312312/FlaskApp
def form_shop():
    if 'city' in session:
        if (request.method == 'POST'):
            sdate = request.form.get('launch_date')
            edate = request.form.get('end_date')
            initial = request.form.get('initial_shops')
            growth = request.form.get('growth_rate')
            max_shop = request.form.get('max_shop')
            q = request.form.get('quarterly')
            new_df = add_product('Shops', sdate, edate, int(initial),
                                 int(growth), int(max_shop), q)
            save_excel(new_df, session['city'])
            new_df.to_sql(con=db,
                          name='%s_data' % session['city'],
                          if_exists='append',
                          index_label='Shops and Units')
            return redirect(url_for('city_input_route'))
    else:
        return redirect(url_for('home'))

    return render_template('form_shop.html')
コード例 #5
0
ファイル: views.py プロジェクト: yash312312/FlaskApp
def form_product():
    if 'city_name' in session:
        if (request.method == 'POST'):
            sdate = request.form.get('launch_date')
            edate = request.form.get('end_date')
            initial = request.form.get('initial_units')
            growth = request.form.get('growth_rate')
            max_unit = request.form.get('max_units')
            q = request.form.get('quarterly')
            name = request.form.get('product_name')
            new_df = add_product(name, sdate, edate, int(initial), int(growth),
                                 int(max_unit), q)
            new_df.to_sql(con=db,
                          name='%s_data' % session['city_name'],
                          if_exists='append',
                          index_label='Shops and Units')
            temp = pd.read_sql_table('%s_data' % session['city_name'], con=db)
            temp.set_index('Shops and Units', inplace=True)
            save_excel(temp, session['city_name'])
            return redirect(url_for('city_input_route'))
        return render_template('form_product.html', listOfPro=['X', 'Y', 'Z'])
    return redirect(url_for('home'))
コード例 #6
0
ファイル: views.py プロジェクト: yash312312/FlaskApp
def home():
    if request.method == 'POST':
        if request.form['btn'] == 'chosen_city':
            session['city_name'] = request.form.get('city_list')
            return redirect(url_for('city_input_route'))
    return render_template('index.html')