title: Trying to pass csv from pandas to d3.js using flask
body: <p>Below is my flask code. I am connecting to a sqlite database , getting some data and making a pandas dataframe and converting it to <strong>CSV</strong> and trying to pass it to d3.js</p>
  
  <pre><code> import sqlite3
   import pandas as pd
   from flask import *
   app = Flask(__name__)
   @app.route("/new_graph",methods = ['GET','POST'])
   def new_graph():
      connection = sqlite3.connect('recruit.db')
      query = "select * from customers"
      //bunch of dataframe calculations using pandas
  
      data = final_df.to_csv(sep=',')
      print (data)
      return render_template('new_display.html',data=data)
  
  if __name__ == "__main__":
      app.run(app.run( port=8069),debug=True)
  </code></pre>
  
  <p>Also tried,</p>
  
  <pre><code>@app.route("/new_graph",methods = ['GET','POST'])
  def new_graph():
      if request.method == 'POST':
          connection = sqlite3.connect('recruit.db')
          query = "select * from customers"
      //bunch of dataframe calculations using pandas
  
          data = final_df.to_csv(sep=',')
          return data
      return render_template('new_display.html')
  </code></pre>
  
  <p>Below is my <strong>html</strong>:</p>
  
  <pre><code>    &lt;!DOCTYPE html&gt;
  
  &lt;body&gt;
  &lt;script src="https://d3js.org/d3.v4.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  
  
  d3.csv("/new_graph", function(data) {  //recommended way of passing csv 
    console.log(data[0]);
  });
  
   &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>When I run this, it runs fine and the csv gets printed:</p>
  
  <pre><code> 127.0.0.1 - - [02/May/2017 17:52:12] "GET /new_graph HTTP/1.1" 200 -
  ,Home_Owners,Home_renters,Ethinicity
  0,1229,24,Asian
  1,9856,584,Black
  2,793,11,Chinese
  3,8681,351,Hispanic
  4,256,6,American Indian
  5,141,5,Japanese
  6,23855,449,Other
  7,4,0,Portuguese
  8,39284,611,White
  
  127.0.0.1 - - [02/May/2017 17:52:17] "GET /new_graph HTTP/1.1" 200 -
  </code></pre>
  
  <p>But <strong>console.log</strong> prints a weird output:</p>
  
  <pre><code>  Object {&lt;!DOCTYPE html&gt;: ""}
  </code></pre>
  
  <p>Unable to figure out , where I am going wrong.</p>