mirror of
https://github.com/wassname/greater_tables_project.git
synced 2026-06-27 16:15:38 +08:00
Initial commit
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
# Auto detect text files and perform LF normalization
|
||||
* text=auto
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/#use-with-ide
|
||||
.pdm.toml
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Stephen Mildenhall
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,203 @@
|
||||
# table formatting again
|
||||
import pandas as pd
|
||||
# from IPython.display import HTML
|
||||
|
||||
|
||||
def gtqd(df, col_align='', formatters=None, ratio_cols=None, **kwargs):
|
||||
"""Better HTML output for a DataFrame."""
|
||||
table_id = f'T{id(df):x}'[::2].upper()
|
||||
style = []
|
||||
|
||||
def r(x):
|
||||
"""Default ratio format."""
|
||||
try:
|
||||
return f'{x:.1%}'
|
||||
except:
|
||||
return x
|
||||
|
||||
def _float(x):
|
||||
try:
|
||||
return f'{x:,.3f}'
|
||||
except:
|
||||
return x
|
||||
|
||||
def _int(x):
|
||||
try:
|
||||
return f'{x:,.3f}'
|
||||
except:
|
||||
return x
|
||||
|
||||
# see if index col names in formatters?
|
||||
dt = df.dtypes
|
||||
if ratio_cols is None:
|
||||
ratio_cols = []
|
||||
elif not isinstance(ratio_cols, (list, tuple)):
|
||||
ratio_cols = list(ratio_cols)
|
||||
|
||||
float_cols = df.select_dtypes(include='float').columns
|
||||
integer_cols = df.select_dtypes(include='int').columns
|
||||
if formatters is None:
|
||||
formatters = {}
|
||||
|
||||
for c in df.columns:
|
||||
if c not in formatters.keys():
|
||||
# set a default
|
||||
formatters[c] = r if c in ratio_cols else (
|
||||
_float if c in float_cols else (
|
||||
_int if c in integer_cols else
|
||||
lambda x: x)
|
||||
)
|
||||
|
||||
html = df.to_html(table_id=table_id, formatters=formatters, **kwargs)
|
||||
|
||||
if col_align == '':
|
||||
# guess: index l, numeric r rest l
|
||||
idx = 'l' * df.index.nlevels
|
||||
numeric_cols = df.select_dtypes('number').columns
|
||||
rc = ''.join('r' if c in numeric_cols else 'l' for c in df.columns)
|
||||
col_align = idx + rc
|
||||
|
||||
# col no -> lrc -> spell out
|
||||
d = {'l': 'left', 'r': 'right', 'c':'center'}
|
||||
ca = col_align
|
||||
ca = dict(zip(range(1, 1+len(ca)), map(d.get, ca)))
|
||||
style.append('<style>')
|
||||
|
||||
style.append(f'''
|
||||
#{table_id} {{
|
||||
/* border-collapse: collapse;*/
|
||||
width: 100%;
|
||||
/* margin above left below right table*/
|
||||
margin: 0px 0 10px 0;
|
||||
font-family: "Open Sans Condensed", "Arial Narrow", Arial, "Roboto Condensed", sans-serif;
|
||||
font-size: 0.9em;
|
||||
}}
|
||||
|
||||
''')
|
||||
|
||||
|
||||
for i in range(1, 1+len(ca)):
|
||||
style.append(
|
||||
f'#{table_id} tbody td:nth-child({i}){{ text-align: {ca[i]}; }}')
|
||||
style.append(f'#{table_id} th {{ text-align: center;}}')
|
||||
style.append('</style>\n')
|
||||
|
||||
if len(style):
|
||||
style = '\n'.join(style)
|
||||
else:
|
||||
style = ''
|
||||
|
||||
out = f'{style}{html}'
|
||||
|
||||
return out
|
||||
|
||||
|
||||
# def pf(df, *, ratio_cols=None, precision=3, pef_lower=-3, pef_upper=16,
|
||||
# format_index=True):
|
||||
# """Format a DataFrame."""
|
||||
|
||||
# df = df.copy()
|
||||
|
||||
# _ratio_names = ['max_LR', 'gross_LR', 'net_LR', 'ceded_LR', 'LR',
|
||||
# 'COC']
|
||||
|
||||
# if ratio_cols == 'all':
|
||||
# ratio_cols = [i for i in df.columns]
|
||||
|
||||
# elif ratio_cols is not None and type(ratio_cols) != list:
|
||||
# ratio_cols = [ratio_cols]
|
||||
|
||||
# def pef(x):
|
||||
# """Pandas engineering formatter."""
|
||||
# return pd.io.formats.format.EngFormatter(accuracy=2, use_eng_prefix=True)(x)
|
||||
|
||||
# pl = 10.**pef_lower
|
||||
# pu = 10.**pef_upper
|
||||
|
||||
# def nf(x):
|
||||
# """Number formatter."""
|
||||
# try:
|
||||
# if x == int(x):
|
||||
# return f'{x:,.0f}'
|
||||
# elif abs(x - 1) < 1e-3:
|
||||
# return f"1-{1 - x:.3g}"
|
||||
# elif abs(x) < pl or abs(x) > pu:
|
||||
# return pef(x)
|
||||
# elif abs(x) > 1e2:
|
||||
# fmt = f'{{x:,.{precision - 1}f}}'
|
||||
# return fmt.format(x=x)
|
||||
# # return f'{x:,.1f}'
|
||||
# else:
|
||||
# fmt = f'{{x:,.{precision}f}}'
|
||||
# return fmt.format(x=x)
|
||||
# except:
|
||||
# return x
|
||||
|
||||
# def ratio(x):
|
||||
# try:
|
||||
# return f'{x:.1%}'
|
||||
# except:
|
||||
# return x
|
||||
|
||||
# def integer(x):
|
||||
# return f'{x:,d}'
|
||||
|
||||
# # convert into string
|
||||
# col_list = [f'{c}' for c in df.columns]
|
||||
|
||||
# if ratio_cols is None:
|
||||
# ratio_cols = [c for c in col_list if c in _ratio_names]
|
||||
# if len(ratio_cols) == 0:
|
||||
# ratio_cols = None
|
||||
# if ratio_cols is not None:
|
||||
# col_list = list(set(col_list) - set(ratio_cols))
|
||||
|
||||
# number_cols = df.select_dtypes(include='number').columns
|
||||
|
||||
# index_cache = None
|
||||
# if format_index:
|
||||
# index_cache = df.index.names
|
||||
# df = df. reset_index(drop=False)
|
||||
|
||||
# float_cols = df.select_dtypes(include='float').columns
|
||||
# integer_cols = df.select_dtypes(include='int').columns
|
||||
|
||||
# for c in df:
|
||||
# # if df.dtypes[c] in (int, float)
|
||||
# if c in ratio_cols:
|
||||
# df[c] = df[c].map(ratio)
|
||||
# elif c in float_cols:
|
||||
# df[c] = df[c].map(nf)
|
||||
# elif c in integer_cols:
|
||||
# df[c] = df[c].map(integer)
|
||||
# else:
|
||||
# print(f'Col {c} not treated')
|
||||
|
||||
# if format_index and index_cache is not None:
|
||||
# df = df.set_index(index_cache)
|
||||
|
||||
# # align number columns
|
||||
# # method 1
|
||||
# # sdf = (
|
||||
# # df.style
|
||||
# # .applymap(lambda x: 'text-align: right;', subset=number_cols
|
||||
# # )
|
||||
# # )
|
||||
|
||||
# # Define styles for specific columns
|
||||
# # styles = [
|
||||
# # {'selector': f'td.col{i}', 'props': [('text-align', 'right')]} # Apply to specific columns
|
||||
# # for i in number_cols
|
||||
# # ]
|
||||
|
||||
# # # Apply styles
|
||||
# # styled_df = df.style.set_table_styles(styles)
|
||||
|
||||
# # display(styled_df)
|
||||
|
||||
|
||||
# # Generate table HTML with inline CSS
|
||||
# table_html = df.to_html(index=True, classes="dataframe")
|
||||
|
||||
|
||||
# return HTML(table_html)
|
||||
@@ -0,0 +1,13 @@
|
||||
[build-system]
|
||||
requires = ["setuptools", "wheel"] # Tools needed to build the package
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "greater_tables"
|
||||
version = "1.0.0"
|
||||
description = "Perfect tables from pandas"
|
||||
authors = [{name = "Stephen J Mildehall", email = "mynl@me.com" }]
|
||||
readme = "README.md"
|
||||
license = { text = "BSD 3-Clause" }
|
||||
requires-python = ">=3.10"
|
||||
dependencies = ["pandas"]
|
||||
Reference in New Issue
Block a user