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>

nendere
  • 21
  • 3
  • Both endpoints are pointing in the same url `@app.route('/')` – Roomm Jun 01 '21 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 – nendere Jun 01 '21 at 14:04

2 Answers2

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?

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>
Roomm
  • 905
  • 11
  • 23