Python Forum
How to use a variable from a flask page to another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use a variable from a flask page to another
#1
Hello so i'm trying to take the variables guild_api,guildname, and guild_name from my flask page /guild/ and be able to also use them on flask game /guild/result/

I was using global variables and it did the trick on my pc but when I loaded it onto my VPS it did not work so now i'm looking for another way
@app.route("/guild/", methods=["POST", "GET"])
def guildcp():
    table = "<h1>Please enter a player name</h1>"
    if request.method == "POST":
        guild_name = request.form['guild']
        guild_api = requests.get('https://api.slothpixel.me/api/guilds/'+guild_name).json()
        if 'guild' in guild_api:
            table = "<h1>Player isn't in a guild</h1>"
        else:
            if 'error' in guild_api:
                table = '<h1>Player not found</h1>'
            else:
                table = "<h1></h1>"
                guildname = guild_api['name']
                return redirect(url_for('guild'))
    return render_template('guild.html', table=Markup(table))

@app.route("/guild/result/")
def guild():
    table = ""
    workbook = xlsxwriter.Workbook('spreadsheets/'+guildname+'.xlsx')
    worksheet = workbook.add_worksheet()
    worksheet.set_column(0, 0, 30)
    worksheet.set_column(1, 0, 30)
    worksheet.set_column(2, 0, 30)
    worksheet.set_column(3, 0, 30)
    worksheet.set_column(4, 0, 30)

    bold = workbook.add_format({'bold': True, 'bg_color': 'gray', 'align': 'center'})
    default = workbook.add_format({'align': 'center'})

    worksheet.write('A1', 'Player Name', bold)
    worksheet.write('B1', 'Guild Rank', bold)
    worksheet.write('C1', 'Total Week Guild XP', bold)
    worksheet.write('D1', 'Join Date', bold)

    name_slot = 1
    rank_slot = 1
    gxp_slot = 1
    join_slot = 1

    for i in range(len(guild_api['members'])):
        uuid = guild_api['members'][i]['uuid']
        mc = requests.get("https://playerdb.co/api/player/minecraft/" + uuid).json()
        name = mc['data']['player']['username']

        player_rank = guild_api['members'][i]['rank']
        name_slot = 1 + name_slot
        total_name_slot = "A"+str(name_slot)

        rank_slot = 1 + rank_slot
        total_rank_slot = "B"+str(rank_slot)

        gxp_slot = 1 + gxp_slot
        total_gxp_slot = "C"+str(gxp_slot)

        join_slot = 1 + join_slot
        total_join_slot = "D"+str(join_slot)

        exp_history = guild_api['members'][i]['exp_history']
        exp_history = sum(exp_history.values())

        if (int(exp_history) >= 0):
            total_gxp_color = '#ff6666'
        if (int(exp_history) >= 10000):
            total_gxp_color = '#ffff66'
        if (int(exp_history) >= 100000):
            total_gxp_color = '#33ff33'
        if (int(exp_history) >= 200000):
            total_gxp_color = '#00cc00'

        join_date = guild_api['members'][i]['joined']
        join_date = join_date/1000
        join_date = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(join_date))

        exp_history = "{:,}".format(sum(guild_api['members'][i]['exp_history'].values()))
        total_gxp_color = workbook.add_format({'bg_color': total_gxp_color, 'align': 'center'})

        worksheet.write(total_name_slot, name, default)
        worksheet.write(total_rank_slot, player_rank, default)
        worksheet.write(total_gxp_slot, exp_history, total_gxp_color,)
        worksheet.write(total_join_slot, join_date, default,)

    workbook.close()
    df = pd.read_excel("spreadsheets/"+guild_api['name']+".xlsx")
    pd.set_option("display.colheader_justify","left")
    table = df.to_html(classes=["table table-striped table-bordered"], table_id="dtTable")

    return render_template('guild.html', table=Markup(table))



if __name__ == "__main__":
    app.run(debug=True )
I have tried calling the functions but it wont work with my flask app
sorry I didn't show any error I just don't know how i'm supposed to do this anyway thanks bye!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask: How to make this web page not reloading? Aguscao 0 1,347 Jun-12-2021, 07:40 AM
Last Post: Aguscao

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020