0

Was hoping for some guidance in how to create a nicely styled table I can then view in my web browser. So far I have web scraped a page to create a .csv file and then using pandas in python I have created a table from my csv file, the code below shows my pandas code which creates my table. This code works and in my terminal I can see the results show the table in code format but I want too be able to see my table in a browser and style it nicely. I am thinking of using flask to do this but I cant get this too work, any advice would be greatly appreciated. The flask code I have written is also below but is wrong.

import pandas as pd

columns = ['cap', 'title', 'price']
df = pd.read_csv('asdawhiskey.csv', names=columns)

print(items.to_html())


set FLASK_APP=AsdaWhiskeyTable.py
$env: FLASK_APP="AsdaWhiskeyTable.py"
from flask import *
import pandas as pd

app = Flask(__name__)


@app.route("/tables")
def show_tables():
    data = pd.read_csv('asdawhiskey.csv')


data.set_index(['Name'], inplace=True)
data.index.name=None
asda = data.loc[data.product=='whiskey']
return render_template('view.html', tables=[items.to_html()],
                       titles = ['na', 'Asda Whiskey'])

if __name__ == "__main__":
    app.run(debug=True)

1 Answer 1

1

It sounds like you already have a dataframe and just want a way to convert it to html. The example below creates a toy dataframe and writes it to an html file. The pandas.DataFrame.to_html documentation contains additional arguments to further tailor the output. Hope this helps.

# import libraries
import pandas as pd
import numpy as np

# create dataframe of random items
df = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))

# convert dataframe to html
html = df.to_html()

# Option to print output to terminal
# print(html)

# open file and write
file = open("table.html", 'w')
file.write(html)
file.close
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.