= bootstrap_hdrs()+download_js()+datatable_hdrs() hdrs
Basic
Setup
*hdrs) show(
from ngrok_token import *
= start_ngrok(token) url
ngrok tunnel opened at: https://a223-3-238-95-91.ngrok-free.app
= fast_app(pico=False,hdrs=hdrs)
app,rt = JupyUvi(app) server
Request
req.url
Get current page’s url
@rt
def sdf(req):
return P(req.url)
'/sdf') htmx(url,
req.url_for
Get original
@rt
def sdf(req):
return P(req.url_for('sdf'))
'/sdf') htmx(url,
With param:
# @rt("/api/{data}")
# def this(req,data:str):
# return req.url_for('this',data=data)
# htmx(url,'/api/ssss')
Toasts
toasts options: “info”, “success”, “warning”, “error”
setup_toasts(app)
@rt
def test():
return A('click to trigger toasts',hx_get=tt,target_id='content'),Div(id='content')
@rt
def tt(sess):
return add_toast(sess, "Please upload a csv or excel file", "error")
'/test') htmx(url,
Download file through a link
@app.get
def download_example():
return FileResponse('data/example.csv')
@rt
def test(req):
return A('Download Example File',
=req.url_for('download_example'), # for dynamic route
href='example.csv', # download file name
download )
'/test') htmx(url,
Tooltip
Tooltip
Tooltip (tooltip_content, trigger_content, element='span', **kwargs)
Create a Bootstrap tooltip
Type | Default | Details | |
---|---|---|---|
tooltip_content | FT or str | ||
trigger_content | |||
element | str | span | |
kwargs |
@rt
def test():
return blank(),Tooltip(P('instruction'),'❔')
'/test') htmx(url,
Helptip
Helptip (tooltip_text=' ', trigger_content=i((),{'class': 'bi bi- question-circle-fill'}), **kwargs)
Create a Bootstrap tooltip with help icon as default
Type | Default | Details | |
---|---|---|---|
tooltip_text | str | FT or str | |
trigger_content | FT | i((),{‘class’: ‘bi bi-question-circle-fill’}) | |
kwargs |
@rt
def test():
return blank(),P('Z Score',Helptip((P('sdfsdf'))))
'/test') htmx(url,
Table / Dataframe
= pd.read_csv('data/example.csv') df
df2html
df2html (df, tooltips=None, cls='table table-striped', id=None, **kwargs)
Create an HTML table with Bootstrap styling and tooltips
Type | Default | Details | |
---|---|---|---|
df | |||
tooltips | NoneType | None | dict |
cls | str | table table-striped | |
id | NoneType | None | |
kwargs |
download_table
download_table (*args, **kwargs)
Enable table download through a download button
download_table()
needs to be wrapped in the same Div with the df2html
@rt
def test():
# to show table correctly, always needs a container
return Container(
={'ID': P('ssss')}))
Div(download_table(), df2html(df,tooltips )
'/test') htmx(url,
Download invisible dataframe
send_df
send_df (df, fname, href, button=button((i((),{'class': 'bi bi- download'}), ' Data'),{'class': 'btn btn-outline-secondary btn- sm'}))
download_df
download_df (dataframe, fname)
@app.post
def download_file(dataframe:str, fname:str):
return download_df(dataframe,fname)
@rt
def test(req):
= send_df(df,
download_button ='sdf.csv',
fname=req.url_for('download_file') # function name
href
)return download_button
'/test') htmx(url,
Dynamic table
dynamic_table
dynamic_table (df, id='dynamic_table', cls='table table-striped')
Dynamic data tables; make new id names if multiple tables
import numpy as np
= np.random.rand(20, 10)
matrix = pd.DataFrame(matrix).round(2) df
@rt
def test():
return Container(dynamic_table(df))
'/test') htmx(url,
Pagination
get_pagination
get_pagination (page, total_pages, routes)
Bootstrap-styled pagination with integrated page jump form
Suppose I want a pagination bar for page 3 of 20
The above pagination can be combined with this function:
get_page_index
get_page_index (current_page, total_items, items_per_page=12)
Get start index and end index given the current page number
Suppose I have a list of 100 items, and I want each page have 12 items, so I need to calculate start index and end index given the current page (e.g., 2)
2,100,12) get_page_index(
(12, 24, 9)
Example for a pagination:
= [str(i) for i in range(100)] protein_list
@rt
def test(req, page: int = 1):
# get item index
= get_page_index(page, len(protein_list))
start_idx, end_idx,total_pages
# get items
= protein_list[start_idx:end_idx]
current_proteins = P(','.join(current_proteins))
show_proteins
# get dynamic route
=req.url_for('test')
this_routes
# get pagination item
= get_pagination(page,total_pages,routes=this_routes)
pagination
return show_proteins, pagination,this_routes
'/test') htmx(url,
Modal
Modal content
modal_content
modal_content (title, *args)
'Modal content','sdfsdf') modal_content(
It can be combined with the below to search and trigger modal
modal_form
modal_form (*form_content, post, btn_text='Search', cls=None, target_id='modals-window')
Reference: https://htmx.org/examples/modal-bootstrap/
@rt
def page():
= modal_form(Input(type='text',id='input_text'),post=modal)
modal_send return modal_send
@rt
def modal(input_text:str):
return modal_content('Modal title',input_text)
'/page') htmx(url,
Use anchor link to trigger modal
modal_link
modal_link (text, post, cls=None, target_id='modals-window')
Reference: https://htmx.org/examples/modal-bootstrap/
@rt
def page():
= modal_link('click here',post=modal)
modal_send return modal_send
@rt
def modal():
return modal_content('Modal title','sdf')
'/page') htmx(url,
Modal content
modal_content
modal_content (title, *args)
'Modal content','sdfsdf') modal_content(
It can be combined with the below to search and trigger modal
modal_form
modal_form (*form_content, post, btn_text='Search', cls=None, target_id='modals-window')
Reference: https://htmx.org/examples/modal-bootstrap/
@rt
def page():
= modal_form(Input(type='text',id='input_text'),post=modal)
modal_send return modal_send
@rt
def modal(input_text:str):
return modal_content('Modal title',input_text)
'/page') htmx(url,
Use anchor link to trigger modal
modal_link
modal_link (text, post, cls=None, target_id='modals-window')
Reference: https://htmx.org/examples/modal-bootstrap/
@rt
def page():
= modal_link('click here',post=modal)
modal_send return modal_send
@rt
def modal():
return modal_content('Modal title','sdf')
'/page') htmx(url,
get_spinner
get_spinner (id, cls='d-flex justify-content-center')
id='sdf') get_spinner(
Example:
# @app.post
# def example():
# ...
# send_select = Form(
# Hidden(value=df.to_json(),id='upload_df'),
# Div(select_list,button,cls='row g-2 align-items-center'),
# post=calculate,
# target_id='result',
# hx_indicator='#spinner',
# )
# spinner=get_spinner(id='spinner')
# return send_select, spinner, ...
End
server.stop() kill_ngrok()
ngrok tunnel killed