使用Python自動產生HTML及Bootstrap
4 min readJul 19, 2019
HTML
安裝
pip install markyp-html
使用
from markyp_html import meta, style, webpage
from markyp_html.text import h1, p
from markyp_html.inline import strong
page = webpage(
h1("markyp-html"),
strong(p("Hello World!")),
p("This page was generated using Python and markyp-html."),
page_title="markyp-html demo page",
head_elements=[style("h1 {color:red;}\np {color:blue;}")],
metadata=[
meta.author("Website Author"),
meta.charset("UTF-8"),
meta.description("markyp-html demo"),
meta.keywords("markyp-html,markup,Python,HTML"),
meta.viewport("width=device-width, initial-scale=1.0")
]
)
# Get the actual HTML markup.
html = str(page) # or page.markup
print(html)
Bootstrap 4
安裝
pip install markyp-bootstrap4
使用
from markyp_html import webpage
from markyp_html.forms import form
from markyp_bootstrap4 import req
from markyp_bootstrap4.layout import container, one, col, margin, offset
from markyp_bootstrap4.buttons import b_button
from markyp_bootstrap4.forms import form_group, form_check, form_check_label, input_, text
def login():
return form(
form_group(
text.h5("Email"),
input_.email(placeholder="Enter your email address")
),
form_group(
text.h5("Password"),
input_.password(placeholder="Enter your password")
),
form_check(
input_.checkbox(),
form_check_label("Remember Me"),
class_=margin(bottom=2)
),
b_button.primary("Sign In", type="submit")
)
page = webpage(
container(
one(
login(),
md=6,
class_=offset(md=3)
)
),
page_title="markyp-bootstrap4 example",
head_elements=[
req.bootstrap_css,
*req.all_js
]
)
print(page)