0

i''m working with a dataframe, i need to do some operations on it and display a table on the web page. the first one is working, the rest don't. what should i do? UPDATE changing the routes also doesn't work

@app.route('/')
def about():
    return render_template("about.html", result=df.to_html(header='true'))


@app.route('/')
def problem():
    data = df[['attempts', 'activity']]
    data = df.groupby('activity').sum().sort_values('attempts', ascending=False)
    return render_template("about.html", data=data.to_html(header='true'))

and here's the html part of it

  <section id="table">
      <h2>table</h2>
      <p class="lead" > {{result | safe}}</p>
  </section>

  <section id="problems">
      <h2>problems</h2>
      <p class="lead" >{{data | safe}}</p>
  </section>

2
  • Both endpoints are pointing in the same url @app.route('/') Commented Jun 1, 2021 at 13:49
  • what should i write there? it's a single page website where you press the button and it scrolls to the section. writing @app.route('/problems') does nothing Commented Jun 1, 2021 at 14:04

2 Answers 2

0

I think you should loop over the dataframe and then try to display it.

 <section id="problems">
  <h2>problems</h2>
  {% for d in data%}
  <p class="lead" >{{d | safe}}</p>
  {% endfor %}
  </section>

Check out this answer on another similar question. Hope this one will help you. How to show a pandas dataframe into a existing flask html table?

Sign up to request clarification or add additional context in comments.

Comments

0

You have defined two endpoints with the same route, so when you go to "/" you only load one pandas. If you want to show both dataframes you should try something like:

@app.route('/')
def about_and_problem():
    data = df[['attempts', 'activity']]
    data = df.groupby('activity').sum().sort_values('attempts', ascending=False)
    return render_template("about.html",about_dataframe=df.to_html(header='true'), problem_dataframe=data.to_html(header='true'))

And then in the html:

 <section id="table">
      <h2>table</h2>
      <p class="lead" > {{about_dataframe | safe}}</p>
  </section>

  <section id="problems">
      <h2>problems</h2>
      <p class="lead" >{{problem_dataframe | safe}}</p>
  </section>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.