0

I want to be able to upload a file to a web application and have each column of the file plotted against the values in the first column. In this particular example, y and error should be plotted against t, yielding two curves.

t     
0.0000   
1.0000    
1.2300    

y
1.2345
0.9871
0.5545

error
 1.4E-4
-4.9E-3
 8.2E-3

I managed to get the upload part working properly but am lost on how I could use the info from a file to plot a curve.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF_8">
    <title>Uploader</title>
</head>
   <body>
<h1>Upload your file</h1>
      <form id = "upload-form" action = "{{ url_for('upload') }}" method = "POST"
         enctype = "multipart/form-data">
         <input type = "file" name = "file" accept = "file/*" multiple/>
         <input type = "submit" value ="upload"/>
      </form>

   </body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Uploaded</title>
</head>
<body>
Uploaded Successfully
</body>
</html>
import os
from flask import Flask,render_template,request
from bokeh.plotting import figure
from bokeh.io import output_file,show



app = Flask(__name__)


APP_ROOT = os.path.dirname(os.path.abspath(__file__))

fg = figure(x_axis_label ="x",y_axis_label = "y")
x = [1,2,3,4]
y = [1,2,3,4]

fg.circle(x,y)

@app.route("/")
def index():
    return render_template("view.html")

@app.route("/upload", methods=['POST'])
def upload():
    target = os.path.join(APP_ROOT,'files/')
    print(target)

    if not os.path.isdir(target):
        os.mkdir(target)

    for file in request.files.getlist("file"):
        print(file)
        filename = file.filename
        destination = "/".join([target,filename])
        print(destination)
        file.save(destination)
    lines = file.read().split('\n')  # a list, each element is a line
    for line in lines:
        x_raw, y_raw, error_raw = line.split(' ')  # split the line into three, assign them to variabes
        x, y, error = float(x_raw), float(y_raw), float(error_raw)  # parse each part into a floating point number

    return render_template("uploaded.html")


if __name__ == "__main__":
    app.run(debug=True)
Haldean Brown
  • 12,411
  • 5
  • 43
  • 58
YawdMan
  • 13
  • 1
  • 8
  • 7
    Hey, please be mindful with deleting questions when people have already invested a lot of time and effort in helping you. You're denying those people a chance for recognition of the work they put in. – Pekka Feb 27 '17 at 08:30

1 Answers1

3

You can get the contents of a uploaded file as a string without saving it with file.read()

Read file data without saving it in Flask

Then parse the string. If your file were like this:

0.0000 1.2345 1.4E-4
1.0000 0.9871 -4.9E-3
1.2300 0.5545 8.2E-3

You could use this to extract the values:

lines = file.read().split('\n') # a list, each element is a line
for line in line:
    x_raw, y_raw, error_raw = line.split(' ')  # split the line into three, assign them to variabes
    x, y, error = float(x_raw), float(y_raw), float(error_raw) # parse each part into a floating point number

string.split() works like:

>>> "a,b,c;d,e,f".split(';')
['a,b,c', 'd,e,f']
Community
  • 1
  • 1
Jesvin Jose
  • 22,498
  • 32
  • 109
  • 202
  • i got what you are doing but not sure how to implement it exactly into the code. Would it be right before the return statement in my upload function? – YawdMan Feb 26 '17 at 18:47
  • when i implement the code and upload the file i get: TypeError: a bytes-like object is required, not 'str' – YawdMan Feb 26 '17 at 18:52
  • @YawdMan please update the code in the question and provide the full error stacktrace – Jesvin Jose Feb 26 '17 at 18:56
  • just updated it im sorry im still somewhat new to this this is the error i got after uploading a file to test it.... builtins.TypeError TypeError: a bytes-like object is required, not 'str' – YawdMan Feb 26 '17 at 19:11
  • when i add that it says : AttributeError: 'bytes' object has no attribute 'encode' This is the actual question for the problem if im not explaining it correct sorry: You want to upload such a data file to a web application and have the each column, from the second one, plotted against the the values in the first column. In the particular example, y and error should be plotted against t, yielding two curves. The web application may have one field: the name of the file to upload. Search for constructions on how to upload a files and write this application. – YawdMan Feb 26 '17 at 19:25
  • try decode() instead of encode. Sorry, I am familiar with only Python 2 – Jesvin Jose Feb 26 '17 at 19:31
  • Its cool man no worries... i got this error tho: ValueError: not enough values to unpack (expected 3, got 1) Im wondering if its something wrong with my html code. Should i add like a script tag and div tag? – YawdMan Feb 26 '17 at 19:39