title: How to have an output based on an uploaded csv file with the packages Flask?
body: <p>My goal is to have a Flask app who let the user put a csv file and the app return an specific output.</p>
  <p>Here's the output I want, let me show you how I done it in jupyter notebook :</p>
  <pre><code>for i in df['variable1'].unique():
      
      data = df[(df['variable1']  == i)]
      
      data['list'] = data['comms_clean'].apply(lambda x: str(x).split())
  
      top = Counter([item for sublist in data['list'] for item in sublist])
  
      mostcommon = pd.DataFrame(top.most_common(3))
      mostcommon.columns = ['Common Word', 'Count']
      print()
      print('For the classe &quot;{0}&quot; who appears {1} time, here is the 3 most frequent word: '.format(i, data.shape[0]))
      print()
      print(mostcommon.head(3))
      print()
  </code></pre>
  <p>And here's the result :</p>
  <pre><code>    For the classe &quot;classe1&quot; who appears 389 time, here is the 3 most frequent word: 
      
        Common Word  Count
      0  word10       267  
      1  word20       149  
      2  word30       46   
      
      
      For the classe &quot;classe1&quot; who appears 657 time, here is the 3 most frequent word: 
      
        Common Word  Count
      0  word40       234  
      1  word50       89   
      2  word60       34   
      
      
      For the classe &quot;classe3&quot; who appears 250 time, here is the 3 most frequent word: 
      
        Common Word  Count
      0  word70        90   
      1  word80        30   
      2  word90        19   
  
  ...
  </code></pre>
  <p>But I cannot figure it out how to transpose this into Flask.</p>
  <p>Here's my <code>app.py</code> :</p>
  <pre><code>from flask import Flask, render_template, request
  from werkzeug.utils import secure_filename
  import os
  import pandas as pd
  
  ALLOWED_EXTENSIONS = {'csv', 'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
  UPLOAD_FOLDER = 'uploads/'
      
  
  app = Flask(__name__)
  
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  
  def allowed_file(filename):
      return '.' in filename and filename.rsplit('.',1)[1].lower() in ALLOWED_EXTENSIONS
  
  @app.route('/', methods=['GET','POST'])
  def upload_file(filename=None,column=None, data=None):
      if request.method == 'GET':
          render_template('index.html')
  
      if request.method == 'POST':
          if 'file' not in request.files:
              flash(&quot;No file part&quot;)
              return redirect(request.url)
  
          file = request.files['file']
  
          if file.filename == '':
              flash(&quot;No selected file&quot;)
              return redirect(request.url)
  
          if file and allowed_file(file.filename):
              filename = secure_filename(file.filename)
              file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
              file.save(file_path)
              df = pd.read_csv(file_path, delimiter = ';')
              column = list(df)
              data = [list(df[d]) for d in column]
              df_head = df.head()
              
  
              return render_template('index.html', data=data, tables=[df_head.to_html(classes='data')], titles=df.columns.values)
  
      return render_template('index.html')
  
  
  if __name__ == '__main__':
      app.run(debug=True)
  </code></pre>
  <p>And here's my <code>index.html</code> :</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot; /&gt;
      &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
      &lt;title&gt;Upload File&lt;/title&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
      &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot; href=&quot;main.css&quot; /&gt;
      &lt;script src=&quot;main.js&quot;&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      {% if data %}
      {% for table in tables %}
          {{titles[loop.index]}}
          {{ table|safe }}
      {% endfor %}
      {% endif %}
      &lt;h1&gt;Upload new File&lt;/h1&gt;
      &lt;form method=post enctype=multipart/form-data&gt;
        &lt;input type=file name=file&gt;
        &lt;input type=submit value=Upload&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>But this app give me the <code>head()</code> of the csv file uploaded, the specific output I want is way harder to implement so if you could help me, it would be awesome.</p>
  <p>I think I need to create a dataframe with my most frequent word because Flask seems to need a dataframe but I'm not entirely sure.</p>
  <p>Any suggestions ?</p>