ChappyHova
ChappyHova

Reputation: 33

Links Within DataFrame (Flask)

I'm currently trying to create buttons within one of the columns on my DataFrame and then using .to_html to print it as a DataTable within a web page. I've managed to make the buttons clickable by using escape=False in the to_html function but I cannot get the links to go to the right place.

Below is my current attempt, I'm pretty sure it's something to do with the quotation marks.

def add_url(data):
    data['second name'] = "<a href='{{ url_for('player', player_id=1) }}'>Click This Button Here</a>"

It feels like I've tried 6 million combinations of quotation marks but when I look at the html that's generated it always seems to have random quotation marks and not where I want them to be, as shown below.

HTML Code

Here is my function that it should hook up to, it works perfectly if I put the code directly into the .html file, I just can't get it to work from the .py file within the DataFrame.

@app.route('/player/<int:player_id>')
def player(player_id):
    data = load_df('Data/players_pickle/'+str(player_id)+'.pickle')
    return render_template('player.html', data=data.to_html(escape=False))

It should eventually look like this DataTable

Any help would be much appreciated as I've been struggling with this for far too long, feels like I'm trying the same combinations over and over again!

Update:

I've continued trying different combinations of quotations but it must have something to do with encoding when it goes from the .py to the html.

All I get in the address bar when I click the link is

http://127.0.0.1:5000/%7B%7B%20url_for('player',%20player_id=1)%20%7D%7D

It's obviously replacing spaces, curly braces etc with unicode and no matter what I try it keeps doing it. Could it be because it's within a Dataframe and it's just no possible when using to_html() for it to be as it should be?

Upvotes: 3

Views: 897

Answers (1)

Mark
Mark

Reputation: 125

Try:

data['second name'] = '''<a href="{{ url_for('player', player_id=1) }}">Click This Button Here</a>'''

Upvotes: 1

Related Questions