0

So, what I want to do is read 2 csv files, send them over my app.py and then print the results in a new HTML using Flask. Problem is, I can't find what I'm doing wrong. To be more specific I'm sending my CSV files through my app.py and I get either internal errors (500) or server errors (404) one after the other and I don't have a syntax error so it MUST be logical. So, what can I do to fix that because I'm at square one and my gut says that is as trivial as they come.

app.py:

from flask import Flask, render_template, request
import numpy 
import pandas
import jinja2
app = Flask(__name__)

#Create a route to a file which is called "/" and uses both GET and POST
@app.route('/', methods=['GET', 'POST'])
#define function send
def results():
    #retrieve data from form where we used POST
    if request.method == 'POST':
        table1=request.files['table1']
        table2=request.files['table2']
        #define function
        def new_t(t1,t2):
           #Combine t1 and t2
           t3=np.hstack((t1,t2))
           return(t3)
        results=new_t(t1,t2)
        #sends input to the template results.html with the inherited value "results" 
        return render_template('/results.html', results=results)
#in case there was a mistake we are redirecting the user to index.html
return render_template('/index.html')

index.html:

<!DOCTYPE html>
<html>
    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" 
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 
integrity="sha384- 
BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" 
crossorigin="anonymous">

   <body>
   <h1>Give to your CSVs: </h1>
   <!-- Create a form that sends data to our server (in this case local) using POST method -->
   <form method="POST" action="/results" enctype="multipart/form-data" >
    <!-- Style our input using Bootstrap CSS -->
        <div class="form-group">
            <!-- Create an input with type text so we can type our age in text form -->
            <b>Choose Table1 (CSV format):</b>
            <input type="file"  name="table1">
            <b>Choose Table2 (CSV format):</b>
            <input type="file"  name="table2">
        </div>
        <!-- Create and style our submit button-->
        <input class="btn btn-primary" type="submit" value="submit">
    </form>
   </body>
</html>

results.html

<!DOCTYPE html>
<html>
  <header>
   </header>
   <body>
    <!-- Print out the age that we typed which was sent from our app.py -->
     <h1>Results: </h1>
       <table>
       {{ results|safe }}
       </table>
     </body>
 </html>
  • can you put the full app.py please? I'm not really understanding if your app.py is wrong or you just didn't introduce all the information! – Carlo 1585 Jul 25 '18 at 11:49
  • @Carlo1585 ok, I feel bad about this because you saying "can you put the full app.py please?" means that there are things that I have forgot write...because that is my app.py (I just changed the variables to table1 and table2). But basically the whole idea is to create a simple flask (app.py) that takes as input two CSV files (a symmetrical table and a column), process them (combine them using numpy) and then output the results to either a new page or to the same page (I'm currently working on outputting the results to another page) – Konan O Brien Jul 25 '18 at 13:16
  • you have to initialize the flask app and run it with something like that were you can set-up the IP address, port etc...."if __name__ == '__main__': app.run(host='0.0.0.0', port=1024, threaded=True)" plus you can't have 2 return, or you make and if/else or try/except and in one case you return something and in another case something else. sorry if the answer is a bit messy but in the comment is difficult to show code – Carlo 1585 Jul 25 '18 at 13:24
  • @Carlo1585 Can you provide a source(book, documentation, git repo, etc) where I can understand that example, because I will have to do a documentation later. – Konan O Brien Jul 25 '18 at 14:12
  • I use to use the official one..... http://flask.pocoo.org/docs/1.0/ – Carlo 1585 Jul 25 '18 at 15:10

0 Answers0