Skip to content

Instantly share code, notes, and snippets.

@why-not
Last active April 15, 2022 22:54
Show Gist options
  • Save why-not/fde9f71de2b7c3e1f7fbdbd504608684 to your computer and use it in GitHub Desktop.
Save why-not/fde9f71de2b7c3e1f7fbdbd504608684 to your computer and use it in GitHub Desktop.
print multiplication tables for children, with color formatting if needed. (python, pandas)
import pandas as pd
import numpy as np
def style_dataframe(df, maxvals=None):
result = df.style
result.set_properties(**{'text-align': 'center'})
result.set_properties(**{'font-family': 'monospace'})
result.set_properties(**{'font-weight': 'bold'})
result.set_table_styles([
{'selector' : 'td', 'props' : [('border', '1px solid lightblue')]},
{'selector' : 'th', 'props' : [('border', '1px solid lightblue')]},
{'selector' : 'th', 'props' : [('padding', '1em')]},
{'selector' : 'td', 'props' : [('padding', '1em')]},
{'selector' : '', 'props' : [('border-collapse', 'collapse')]}])
result.format(precision=2)
result.background_gradient(cmap='GnBu', axis=None)
# result.bar(subset=["8", "9"], color='#FFA07A')
result = result.render()
result = result.replace('\n', '')
return result
# modify here to skip some items in the table to make the child guess.
def mul(x):
return x.name * x.index
# output is html, open/print the output from a browser
with open('multiply.html', 'w') as mfile:
df = pd.DataFrame(np.random.randn(10, 10), columns=range(1, 11))
df.index += 1 # make index start from 1 instead of 0,
# kids don't need to know the horrors of
# the real world at this age :D
df = df.apply(mul)
mfile.write(style_dataframe(df))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment