Created
August 14, 2015 02:53
-
-
Save sujayy1983/6a1152bf46e348d06820 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Description: | |
Using pandas display 200 lines of csv at a time. Csv for example could be an import from a database. | |
''' | |
""" | |
If you plan to test this with a sample csv then you may use the following code to generate csv: | |
file = open("large.csv", "w") | |
count = 0 | |
file.write("Index , col1, col2, col3, col4, col5, col6, col7, col8\n") | |
for x in range(1,10000): | |
file.write(str(x) + ", col1-" + str(x) + ", col2-" + str(x) + ", col3-" + str(x) + ", col4-" + str(x) + ", col5-" + str(x) + ", col6-" + str(x) + ", col7-" + str(x) + ", col8-" + str(x) + "\n") | |
file.close() | |
"" | |
__author__ = 'Sujayyendhiren Srinivasamurthi' | |
__email__ = 'sujayy1983@gmail.com' | |
@app.route("/read") | |
@app.route("/read/<pgNum>") | |
def read_data(pgNum=None): | |
df = pd.read_csv('large.csv') | |
#Excluding one row that is table header | |
totalRows = len(df.index) -1 | |
minPgNum = 0 | |
maxPgNum = 0 | |
line = """<a href="http://%s:5000/read/%s">%s</a>        """ | |
template = """<ul class="pagination"> %s </ul>""" | |
if(pgNum == None): | |
pgNum = 1 | |
minPgNum = 0 | |
if totalRows > 200: | |
maxPgNum = 200 | |
else: | |
maxPgNum = totalRows | |
else: | |
pgNum = int(pgNum)-1 | |
if pgNum*200 >= 0 and ((pgNum+1)*200)+1 <= totalRows: | |
minPgNum = pgNum * 200 | |
maxPgNum = (pgNum+1)* 200 | |
else: | |
pgNum = 0 | |
minPgNum = 0 | |
maxPgNum = 200 | |
buildLines = line %('192.168.0.6', pgNum , 'prev') + line %('192.168.0.6', pgNum+2 , 'next') | |
return template %(buildLines) + "<br>" + df[ minPgNum:maxPgNum].to_html(index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment