Also how do I POST arguments from HTML to python? Would I just include a
methods=["GET","POST"]
into my decorator and call it in my .html code
action="" class="form-inline" method="post"
You must be logged in to post. Please login or register an account.
For the second question, I believe I found the answer but would like some verification from you if that's the right path. http://stackoverflow.com/questions/11556958/sending-data-from-html-form-to-python-flask
-tonyphoang 9 years ago
You must be logged in to post. Please login or register an account.
What's the use of the form? I don't see any code that handles uploads, what's the form doing?
Your entire question is answered by various tutorial here: https://pythonprogramming.net/dashboard/#tab_webdev
For form input, look at the https://pythonprogramming.net/flask-get-post-requests-handling-tutorial/
For graphs, I've done that too: https://pythonprogramming.net/adding-js-plugins-flask-highcharts-example/
Otherwise, not sure what kind of arguments you're trying to push, but it's all there.
-Harrison 9 years ago
You must be logged in to post. Please login or register an account.
Thanks for the quick reply.
What I'm trying to do is pass arguments from HTML to python to control launch a script. When the script is completed, it will output a csv file.
I would like to have some type of management system that automatically categorizes the csv.
-tonyphoang 9 years ago
You must be logged in to post. Please login or register an account.
I believe I know how to get the POST working
my __init__.py
# Define a route for the default URL, which loads the form @app.route('/') def form(): return render_template('form_submit.html')
# Define a route for the action of the form, for example '/hello/' # We are also defining which type of requests this route is # accepting: POST requests in this case @app.route('/hello/', methods=['POST']) def hello(): name=request.form['yourname'] email=request.form['youremail'] return render_template('form_action.html', name=name, email=email)
my form_submit.html
<html> <head> <title>Handle POST requests with Flask</title> <link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}"> </head> <body> <div id="container"> <div class="title"> <h1>POST request with Flask</h1> </div> <div id="content"> <form method="post" action="{{ url_for('hello') }}"> <label for="yourname">Please enter your name:</label> <input type="text" name="yourname" /><br /> <label for="youremail">Please enter your email:</label> <input type="text" name="youremail" /><br /> <input type="submit" /> </form> </div> <div class="title"> <h1>Flask code</h1> </div> </div> </div> </body> </html>
You must be logged in to post. Please login or register an account.
I don't believe I phrased my question correctly.
What I want to do is have a content manager for a folder containing .csv files. Upon clicking on a button (relating to that .csv) it would display the .csv using high charts.
So far I've gotten the .csv file to be displayed as clickable links my __init__.py
import glob import re
@app.route('/results/') def results(): try: data = glob.glob('/var/www/FlaskApp/FlaskApp/csv/*.csv') convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)] data.sort(key=alphanum_key) data.reverse() #reverse sorting datanames=[] for dirpath in data: datanames.append(os.path.split(dirpath)[-1]) return render_template("results.html", datanames=datanames) except Exception as e: return render_template("500.html", error = str(e))
my results.html
{% extends "header.html" %} {% block body %}
<body>
<p>Results</p>
{% for c in datanames %} <li><a href="{{c}}">{{c}}</a></li> {% endfor %}
</body> {% endblock %}
I'm unfamiliar with how to integrate high charts to open up a .csv file?
-tonyphoang 9 years ago
You must be logged in to post. Please login or register an account.
Highcharts wont open and automatically graph a CSV for you. You have to convert your CSV data to an acceptable JSON. Open in python, convert to a list of lists, then you can convert to json with the json module. Then pass that json object to highcharts to graph.
-Harrison 9 years ago
You must be logged in to post. Please login or register an account.
I'll give that a try and report back.
I would like to try and display my .csv data upon clicking on a button.
I also don't know what my .html would look like to pip just the csv into it. Something involving {{}}
-tonyphoang 9 years ago
You must be logged in to post. Please login or register an account.
One option is to load the csv with the Pandas module. from_csv, then you can immediately generate a new object using to_html .... which creates an html table. Then pass that object to your template with the |safe filter, and there's your table.
-Harrison 9 years ago
You must be logged in to post. Please login or register an account.
I've had some success displaying a csv file's content.
__init__.py
@app.route('/results/<path:csv_filename>/') def display_csv(csv_filename): try: #connects dirpat of 'results_data' folder to 'csv_filename' results_data_dirpath = '/var/www/FlaskApp/FlaskApp/results_data/' complete_dirpath = results_data_dirpath+csv_filename
#reads csv into a list open_csv = open(complete_dirpath,'r') read_csv = csv.reader(open_csv) csv_content = list(read_csv) open_csv.close()
{% extends "header.html" %} {% block body %} <body class="body">
<h3><p>The complete dirpath to the csv below:</p></h3> <p>{{complete_dirpath}}</p><br> <h3><p>The contents of the csv below:</p></h3> <p>{{csv_content}} </p> </div>
high charts below: <div id={{ chartID|safe }} class="chart" style="height: 500px; width: 500px"></div> <script> var chart_id = {{ chartID|safe }} var series = {{ series|safe }} var title = {{ title|safe }} var xAxis = {{ xAxis|safe }} var yAxis = {{ yAxis|safe }} var chart = {{ chart|safe }} </script>
highcharts above </body> {% endblock %}
-tonyphoang 9 years ago
You must be logged in to post. Please login or register an account.
Just got it working.
It turned out it was outputting the csv file as strings [['1','1'],['2','2']]
Just need to convert it to int
csv_content_int=[map(int,x) for x in csv_content]
to convert it to [[1,1],[2,2]] which can be fed into high charts pretty easily.
-tonyphoang 9 years ago
You must be logged in to post. Please login or register an account.
Looks like I spoke too soon. I tried to add in the URL converter and high charts doesn't show up.
-tonyphoang 9 years ago
You must be logged in to post. Please login or register an account.
I ended up ditching highcharts for pygal and it's working perfectly.
-tonyphoang 9 years ago
You must be logged in to post. Please login or register an account.