python - With Flask how to show my data from my CSV file (JSON) on a webpage? -
i show content of csv file on simple webpage :
do know flask have >
i quite new on flask,i read tutorial basic flask don't totally it.
nevermind found solution myself :
# -*- coding: utf-8 -*- flask import flask import os app = flask (__name__) #fun var filepath = os.path.join(os.path.dirname(__file__),'out.csv') open_read = open(filepath,'r') page ='' while true: read_data = open_read.readline() page += '<p>%s</p>' % read_data if open_read.readline() == '': break @app.route("/") def index(): return page if __name__ == "__main__": app.run()
you should using tablib that:
# -*- coding: utf-8 -*- flask import flask import tablib import os app = flask (__name__) #fun var dataset = tablib.dataset() open(os.path.join(os.path.dirname(__file__),'out.csv'))) f: dataset.csv = f.read() @app.route("/") def index(): return dataset.html if __name__ == "__main__": app.run()
much cleaner , simpler.
Comments
Post a Comment