Table generate guide - pai-plznw4me/django-initializer GitHub Wiki
Table ์์ฑ ํ์ค ๊ฐ์ด๋
๋ชฉ์ฐจ
table ์กฐ์์ DataFrame from db -> preprocessing -> table html design -> insert base code ์์๋ก ๊ฐ๋ฐํ๋ค.
-
DataFrame from db
-
preprocessing
-
table html design
-
insert base code
โ ๏ธ๊ด๋ จ libarary ๋ html_helper ๋ชจ๋ ์ ์๋์ด ์๋ค.
0.์ด์
html ์ django template ์ ์์ฐ๊ณ python ์ผ๋ก ์ ์ด ํ ๋ ค๋ ์ด์ ๋ ๋ฌด์์ธ๊ฐ? ์ด๋ค ์ฅ์ ์ด ์๋๊ฐ?
- django ์ ์ข ์๋๋ค.
- ํ์คํ ์ฝ๋๋ก ๋ง๋ค๊ธฐ ํ๋ค๋ค. ๋ค๋ฅธ ์ฌ๋์๊ฒ ๊ต์กํ๊ธฐ ํ๋ค๋ค.
- ๊ฐ๋ฐ layer ๊ฐ ๋ณต์กํด์ง๋ค.
- python โ html ์ผ๋ก ์๋ค ๊ฐ๋ค ํ๋ฉด์ ์ฝ๋ฉํ๋ฉด ์ง์ค์ฑ , ํจ์จ์ฑ์ด ๋ง์ด ๋จ์ด์ง๋ค.
- debugging ์ด ์ฉ์ดํ๋ค
- django ์ ํ ํ์์์ด ๋ฐ๋ก html ๋ก ์ ์ฅํด์ ํ์ธ ๊ฐ๋ฅ, ํ๋ณต ์์
1. DataFrame from DB
- ์๋์ ๊ฐ์ด db ์กฐํ๋ฅผ ํํ์
Dataframe์ผ๋ก ๋ณ๊ฒฝํ๋ค.
def index(request):
files = File.objects.all()
files_df = pd.DataFrame.from_records(files.values())
files_html = files_df.to_html()
return HttpResponse(files_html)
- HttpResponse ์ ํ์ฉํด ๋ฐ๋ก html ๋ก ๋ณํํ๋ค.

๊ทธ๋ผ ์ด๋ ๊ฒ ๋ฐ๋ก table ์ด ๋์จ๋ค.
2. Table Preprocessing
table ์ ์กฐ์ ์ ์ด ํ๋ค.
2.1 ํน์ ์ด๋ง ๋ณด์ฌ์ฃผ๊ณ ์ถ์๋
-
field ํญ๋ชฉ์ forms ์์ ๊ด๋ฆฌํ๋ค.
-
์๋์ ๊ฐ์ด Form ์ ๋ง๋ค๊ณ Form.Meta.fields ์ ํตํด ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค
-
class FileShowForm(forms.ModelForm): class Meta: model = File fields = ['id', 'name', # 'ext', 'size', # 'upload_date', 'creation_date', 'version', 'project', 'filecontent', 'authority'] widgets = { 'creation_date': forms.widgets.DateInput(attrs={'type': 'date'}) } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) -
์ดํ
def index(request):
files = File.objects.all()
show_fields = FileShowForm.Meta.fields
files_df = pd.DataFrame.from_records(files.values())
target_files_df = files_df.loc[:, show_fields] #<-- ์ฌ๊ธฐ๋ฅผ ๋ณด์
files_html = target_files_df.to_html()
return HttpResponse(files_html)
3. Table ๋์์ธ
4. insert base code ์์๋ก ๊ฐ๋ฐํ๋ค.
Table ์์ฑ ๋ณ๊ฒฝ
table ๋ด ํ๊ทธ ์ฝ์
wrap_with_tag ์ ํ์ฉ
Table codeing
import pandas as pd
from bs4 import BeautifulSoup
import numpy as np
def save_as_html(path, html):
"""
์ฃผ์ด์ง HTML ํ๊ทธ๋ฅผ ํ์ผ๋ก ์ ์ฅํฉ๋๋ค.
๋งค๊ฐ๋ณ์:
- path (str): HTML ๋ด์ฉ์ ์ ์ฅํ ํ์ผ ๊ฒฝ๋ก์
๋๋ค.
- tags (str): ์ ์ฅํ HTML ํ๊ทธ ๋๋ ๋ด์ฉ์
๋๋ค.
์์:
save_as_html('output.html', '<p>Hello, World!</p>')
"""
with open(path, 'w', encoding='utf-8') as f:
f.write(html)
def wrap_with_tag(input_string, tag_name, **attr):
"""
์ฃผ์ด์ง ๋ฌธ์์ด์ ์ง์ ํ HTML ํ๊ทธ๋ก ๊ฐ์ธ๋ ํจ์์
๋๋ค.
Parameters:
- input_string (str): ๊ฐ์ ๋ฌธ์์ด์
๋๋ค.
- tag_name (str): ์์ฑํ ํ๊ทธ์ ์ด๋ฆ์
๋๋ค.
- **attr: ์์ฑํ ํ๊ทธ์ ๋ค๋ฅธ ์์ฑ์ ์ง์ ํ๋ ํค์๋ ์ธ์๋ค์
๋๋ค.
Returns:
str: HTML ํ๊ทธ๋ก ๊ฐ์ธ์ง ๋ฌธ์์ด์
๋๋ค.
Example:
wrap_with_tag('<p>Hello, World!</p>', 'a', href='https://example.com', class_='link')
"""
# BeautifulSoup์ผ๋ก HTML ํ์ฑ
soup = BeautifulSoup(input_string, 'html.parser')
# <a> ํ๊ทธ ์์ฑ
target_tag = soup.new_tag(name=tag_name, **attr)
# ์๋์ ๋ฌธ์์ด์ <a> ํ๊ทธ ์์ผ๋ก ์ฎ๊ธฐ๊ธฐ
target_tag.append(soup.new_string(input_string))
# ์๋์ ๋ฌธ์์ด์ <a> ํ๊ทธ๋ก ๋์ฒด
soup.contents = [target_tag]
# ๊ฒฐ๊ณผ ๋ฐํ
return str(soup)
def add_css():
pass
def add_js():
pass
def html_load(basepath):
"""
์ฃผ์ด์ง ํ์ผ ๊ฒฝ๋ก์์ HTML ํ์ผ์ ์ฝ์ด์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ํจ์์
๋๋ค.
Parameters:
- basepath (str): ์ฝ์ด์ฌ HTML ํ์ผ์ ๊ฒฝ๋ก์
๋๋ค.
Returns:
str: HTML ํ์ผ์ ๋ด์ฉ์ ๋ด์ ๋ฌธ์์ด์
๋๋ค.
Example:
html_content = html_load('path/to/your/file.html')
"""
with open(basepath, 'r', encoding='utf-8') as f:
html_content = f.read()
return html_content
def bs4_load(base_path):
"""
์ฃผ์ด์ง ํ์ผ ๊ฒฝ๋ก์์ HTML ํ์ผ์ ์ฝ์ด์ BeautifulSoup ๊ฐ์ฒด๋ก ํ์ฑํ์ฌ ๋ฐํํ๋ ํจ์์
๋๋ค.
Parameters:
- base_path (str): ์ฝ์ด์ฌ HTML ํ์ผ์ ๊ฒฝ๋ก์
๋๋ค.
Returns:
BeautifulSoup: HTML์ ํ์ฑํ BeautifulSoup ๊ฐ์ฒด์
๋๋ค.
Example:
soup = bs4_load('path/to/your/file.html')
"""
html_str = html_load(base_path)
soup = BeautifulSoup(html_str, 'html.parser')
return soup
if __name__ == '__main__':
arr = np.arange(12).reshape(4, 3)
df = pd.DataFrame(arr)
df = df.applymap(lambda x: wrap_with_tag(str(x), tag_name='a', href='http://naver.com'))
# ์
๋ด html ์ฝ๋๋ฅผ html๋ก ์ธ์ํ๋ค. escape=True ์ด๋ฉด ๋ฌธ์์ด๋ก ์ธ์์ ํ๋ค.
table_html = df.to_html(escape=False)
# base html ์ ๋ถ๋ฌ์ต๋๋ค.
base_soup = bs4_load('../base_template/bootstrap.html')
# base html ๋ด table์ ์ฝ์
ํฉ๋๋ค.
body_tag = base_soup.find('body')
first_script_tag = body_tag.find('script')
first_script_tag.insert_before(table_html)
# html ์ ์ ์ฅํฉ๋๋ค.
save_as_html('temp.html', base_soup.prettify(formatter=None))
# formatter ์ None ์ผ๋ก ํ๋ฉด ํน์ ๊ธฐํธ๋ค์ด ๋ $gt ์ด๋ฐ ์์ผ๋ก ๋์จ๋ค.