๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

๋‹ค์‹œ ๋„์ „.. Flask CSV ๋ถˆ๋Ÿฌ์™€์„œ HTML์— ํ‘œ๋กœ ์‹œ๊ฐํ™”ํ•˜๊ธฐ(4) ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป ๋ฐฑ์—”๋“œ(Back-End)/Node js

๋‹ค์‹œ ๋„์ „.. Flask CSV ๋ถˆ๋Ÿฌ์™€์„œ HTML์— ํ‘œ๋กœ ์‹œ๊ฐํ™”ํ•˜๊ธฐ(4)

์ง•์ง•์•ŒํŒŒ์นด 2022. 11. 30. 13:47
728x90
๋ฐ˜์‘ํ˜•

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” geeksforgeeks ๋‹˜์˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :-)>

https://www.geeksforgeeks.org/convert-csv-to-html-table-using-python-pandas-and-flask-framework/

 

Convert CSV to HTML Table using Python Pandas and Flask Framework - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

 

 

๐ŸŽ… Python Pandas ๋ฐ Flask Framework๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ CSV ํŒŒ์ผ์„ HTML ํ…Œ์ด๋ธ” ๋กœ ๋ณ€ํ™˜

๐ŸŒต ํŒŒ์ผ ๊ตฌ์กฐ

 

๐ŸŒต ์ฝ”๋“œ ๊ตฌํ˜„

  • app.py
# importing flask
from flask import Flask, render_template
  
# importing pandas module
import pandas as pd
  
# flask ๊ฐ์ฒด๋ฅผ app ์— ํ• ๋‹น
app = Flask(__name__)
  
  
# reading the data in the csv file
df = pd.read_csv('test.csv')
df.to_csv('sample_testdata.csv', index=None)
  
  
# route to html page - "table"
# ํ•ด๋‹น ๋ผ์šฐํŒ… ๊ฒฝ๋กœ๋กœ ์š”์ฒญ์ด ์™”์„ ๋•Œ ์‹คํ–‰ํ•  ํ•จ์ˆ˜๋ฅผ ๋ฐ”๋กœ ๋ฐ‘์— ์ž‘์„ฑํ•จ
@app.route('/')
@app.route('/table')
def table():
    # converting csv to html
    data = pd.read_csv('test.csv')
    return render_template('table.html', tables=[data.to_html()], titles=[''])
  
# ํ”Œ๋ผ์Šคํฌ ์„œ๋ฒ„ ๊ตฌ๋™ (์„œ๋ฒ„๋กœ ๊ตฌ๋™ํ•œ IP ์™€ ํฌํŠธ๋ฅผ ์˜ต์…˜์œผ๋กœ ๋„ฃ์–ด์คŒ)
if __name__ == "__main__":
    app.run(host="localhost", port=int("5000"))

 

  • templates/table.html
<!DOCTYPE html>
<html lang="en">
    <head>
        <title> Table </title>              
    </head>
    <body>
        <div align="center">
            <table>
                <h1>
                <!--Displaying the converted table-->
                     {% for table in tables %}
                    <h2>{{titles[loop.index]}}</h2>                            
                    {{ table|safe }}
                    {% endfor %}     
                </h1> 
            </table>
        </div>
    </body>
</html>

python app.py ๋กœ ์‹คํ–‰

 

 

 

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•
Comments