A very early rough implementation for Python Shiny of my Dash Dataframe Tables package. See A Live Demo App Here.
This enhanced_from_dataframe
function creates bootstrap tables with some automatic features like links and conditional formatting.
- Automatic Links It will automatically generate
ui.tags.A
wrappers around a column from a matched column in the same dataframe. The hyperlink column must match the column_name + a specific suffix. In the example to the right, the (hidden) link column isCompany_HREF
, using the default suffix. - Conditional Formatting Criteria can either be a list of tuples
(match_list,style_dict)
or acallable
that returns the style dict if the condition is met. This allows for more complex condition formatting. - Any Callable can wrap a cell Pass a callable to the function to customize the content of any column. Turn it into a button, link, wrap it in any UI elements you can create in Shiny.
This is invoked with render.ui and outputting to a ui output:
def server(input, output, session):
@output
@render.ui
def result():
return enhanced_from_dataframe(
df,
markdown_columns=['markdown_example'],
cell_style_dict=cell_style_dict,
columns=['Company', 'Date', 'Value', 'Value2', 'markdown_example'])
The code below generates the conditional formatting you see. You can also add a specific bootstrap class by putting a class
key in the "style" dictionary.
def color_positive(val):
if val > 0:
return {'class': 'table-success'}
elif val < 0:
return {'class': "table-danger"}
cell_style_dict = {
'Company': [
(['Yahoo', 'Apple'], {
"style": 'font-weight: bold'
}),
(['Oracle'], {
'class': 'table-danger'
}),
],
'Value2':
lambda x: {
"style": 'background-color: #7FFFD4'
} if x > 10 else {
}
'Date':
lambda x: {
'class': 'table-info'
} if x.weekday() in [4, 6] else {},
'Value':
color_positive
}
This code has a callable wrapper for the Company2 column:
def wrap_company(row, col_name):
return experimental.ui.tooltip(
ui.tags.button(row[col_name], ),
f"This is a tool tip for {row[col_name]} that shows the link {row['Company_HREF']}"
)
![Screenshot 2023-08-19 at 7 56 56 PM](https://private-user-images.githubusercontent.com/13702392/261824572-1beb0669-4d65-4640-aa27-ede19a2f4d44.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTIwNDg0NzcsIm5iZiI6MTcxMjA0ODE3NywicGF0aCI6Ii8xMzcwMjM5Mi8yNjE4MjQ1NzItMWJlYjA2NjktNGQ2NS00NjQwLWFhMjctZWRlMTlhMmY0ZDQ0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA0MDIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNDAyVDA4NTYxN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTgzMDdhMTAzYTZlMTNmYzcwNWYxNTUyZjU4YTBjMDc2MzFjNTUwY2IwZDBjYjcwYTc2ZThjNjcxNjExNzlkZWMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.4q-gb8k50-b7ZweSi8-XIGb8VLru_zPKHBLRoanpiZo)