toolsos 0.2.0__tar.gz → 0.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {toolsos-0.2.0 → toolsos-0.2.1}/PKG-INFO +1 -1
- {toolsos-0.2.0 → toolsos-0.2.1}/pyproject.toml +1 -1
- {toolsos-0.2.0/src/toolsos → toolsos-0.2.1/src/toolsos/database}/database_connection.py +9 -7
- toolsos-0.2.1/src/toolsos/huisstijl/tables/table_helpers.py +6 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/huisstijl/tables/tables.py +81 -26
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos.egg-info/PKG-INFO +1 -1
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos.egg-info/SOURCES.txt +3 -2
- {toolsos-0.2.0 → toolsos-0.2.1}/README.md +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/setup.cfg +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/__init__.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/cbs_tools.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/create_tables.py +0 -0
- {toolsos-0.2.0/src/toolsos → toolsos-0.2.1/src/toolsos/database}/database_transfer.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/download.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/geo.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/helpers.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/huisstijl/__init__.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/huisstijl/colors.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/huisstijl/graphs/__init__.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/huisstijl/graphs/bargraph.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/huisstijl/graphs/graph_styles.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/huisstijl/graphs/linegraph.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/huisstijl/graphs/piegraph.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/huisstijl/graphs/styler.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/huisstijl/tables/__init__.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/huisstijl/tables/table_styles.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos/polars_helpers.py +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos.egg-info/dependency_links.txt +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos.egg-info/requires.txt +0 -0
- {toolsos-0.2.0 → toolsos-0.2.1}/src/toolsos.egg-info/top_level.txt +0 -0
|
@@ -33,7 +33,7 @@ def get_db_connection_strings(
|
|
|
33
33
|
for dbname, params in db_info.items():
|
|
34
34
|
flush = dbname in reset_pw if reset_pw else False
|
|
35
35
|
|
|
36
|
-
if params["
|
|
36
|
+
if params["password"] == "access_token":
|
|
37
37
|
pw = get_azure_access_token()
|
|
38
38
|
else:
|
|
39
39
|
pw = get_pw_from_keyring(dbname=dbname, user=params["user"], reset_pw=flush)
|
|
@@ -104,11 +104,13 @@ if __name__ == "__main__":
|
|
|
104
104
|
# Examples
|
|
105
105
|
|
|
106
106
|
# Get database connection settings from yaml
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
engine_strings = get_db_connection_strings(
|
|
108
|
+
"src/toolsos/database/database_config.yml"
|
|
109
|
+
)
|
|
110
|
+
print(engine_strings.ruimte_analyse222)
|
|
109
111
|
|
|
110
112
|
# Get database connection settings from yaml and reset password
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
113
|
+
engine_strings = get_db_connection_strings(
|
|
114
|
+
"src/toolsos/database/database_config.yml", reset_pw=["ruimte_analyse222"]
|
|
115
|
+
)
|
|
116
|
+
print(engine_strings.ruimte_analyse222)
|
|
@@ -7,8 +7,7 @@ from typing import Any, Callable, Dict
|
|
|
7
7
|
import numpy as np
|
|
8
8
|
import pandas as pd
|
|
9
9
|
from openpyxl import Workbook
|
|
10
|
-
from openpyxl.styles import
|
|
11
|
-
Side)
|
|
10
|
+
from openpyxl.styles import Alignment, Border, Font, PatternFill, Protection, Side
|
|
12
11
|
from openpyxl.utils import get_column_letter
|
|
13
12
|
|
|
14
13
|
Fmt = list[list[dict[str, Any]]]
|
|
@@ -47,6 +46,36 @@ def set_global_style(style: str) -> None:
|
|
|
47
46
|
STYLES = STYLE_NEW
|
|
48
47
|
|
|
49
48
|
|
|
49
|
+
def cols_to_str(df: pd.DataFrame) -> pd.DataFrame:
|
|
50
|
+
"""Change column names in to string. Multiindex column names are nog changed because
|
|
51
|
+
these are always strings
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
df (pd.DataFrame): Dataframe
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
pd.DataFrame: Dataframe with column names as strings
|
|
58
|
+
"""
|
|
59
|
+
if df.columns.nlevels == 1:
|
|
60
|
+
df.columns = df.columns.astype(str)
|
|
61
|
+
|
|
62
|
+
return df
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def get_max_col_widths(data: pd.DataFrame | np.ndarray) -> list[float]:
|
|
66
|
+
col_widths = []
|
|
67
|
+
if isinstance(data, pd.DataFrame):
|
|
68
|
+
for col in zip(*flatten_multiindex_columns(data)):
|
|
69
|
+
col_widths.append(max(len(e) for e in col))
|
|
70
|
+
else:
|
|
71
|
+
for col in zip(*data):
|
|
72
|
+
col_widths.append(max(len(str(e)) for e in col))
|
|
73
|
+
|
|
74
|
+
col_widths = [col_width * 1.13 for col_width in col_widths]
|
|
75
|
+
|
|
76
|
+
return col_widths
|
|
77
|
+
|
|
78
|
+
|
|
50
79
|
def flatten_multiindex_columns(df):
|
|
51
80
|
column_multi = []
|
|
52
81
|
for level in range(df.columns.nlevels):
|
|
@@ -68,7 +97,18 @@ def df_to_array(df: pd.DataFrame) -> np.ndarray:
|
|
|
68
97
|
return np.vstack([column_names, df.to_numpy()])
|
|
69
98
|
|
|
70
99
|
|
|
71
|
-
def get_cells_to_merge(df: pd.DataFrame) ->
|
|
100
|
+
def get_cells_to_merge(df: pd.DataFrame) -> dict[int : list[int, int]]:
|
|
101
|
+
"""Pandas dataframes sometimes have mutliindex columns. For all but the last level
|
|
102
|
+
a dictionary is created to merge the cells. The last level isn't merged because these
|
|
103
|
+
are these contain unique column names
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
df (pd.DataFrame): Pandas dataframe. If the dataframe has multicolumn indices
|
|
107
|
+
a dictionary containg the cells to merge is returned
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
dict[int: list[int, int]]: Dictionary containg the cells to merge
|
|
111
|
+
"""
|
|
72
112
|
levels = flatten_multiindex_columns(df)[:-1]
|
|
73
113
|
|
|
74
114
|
cells_to_merge = {}
|
|
@@ -338,7 +378,7 @@ def write_worksheet(
|
|
|
338
378
|
title: str | None = None,
|
|
339
379
|
source: str | None = None,
|
|
340
380
|
col_filter: bool | None = None,
|
|
341
|
-
|
|
381
|
+
col_widths: list | None = None,
|
|
342
382
|
cells_to_merge: list[list[int]] | None = None,
|
|
343
383
|
) -> None:
|
|
344
384
|
"""Writing data to worksheet. Used for writing values to cells and formatting the cells
|
|
@@ -379,36 +419,32 @@ def write_worksheet(
|
|
|
379
419
|
filters = ws.auto_filter
|
|
380
420
|
filters.ref = f"A1:{excel_style(len(fmt), len(fmt[0]))}"
|
|
381
421
|
|
|
382
|
-
if autofit_columns:
|
|
383
|
-
_autofit_columns(ws)
|
|
384
|
-
|
|
385
422
|
if source:
|
|
386
423
|
_insert_source(ws, source, arr)
|
|
387
424
|
|
|
425
|
+
if col_widths:
|
|
426
|
+
_set_column_widths(ws, col_widths)
|
|
427
|
+
|
|
388
428
|
if title:
|
|
389
429
|
_insert_title(ws, title)
|
|
390
430
|
|
|
391
431
|
if cells_to_merge:
|
|
392
|
-
_merge_cells(ws, cells_to_merge)
|
|
393
|
-
|
|
432
|
+
_merge_cells(ws, cells_to_merge, title)
|
|
394
433
|
|
|
395
|
-
# def _set_column_width(ws: Any, column_widths: list) -> None:
|
|
396
|
-
# for i, column_number in enumerate(range(ws.max_column)):
|
|
397
|
-
# column_letter = get_column_letter(column_letter)
|
|
398
|
-
# column_width = column_widths[i]
|
|
399
|
-
# ws.column_dimensions[column_letter].width = column_width
|
|
400
434
|
|
|
435
|
+
def _set_column_widths(ws: Any, col_widths: list[int]) -> None:
|
|
436
|
+
for idx, col_width in enumerate(col_widths):
|
|
437
|
+
col_letter = get_column_letter(idx + 1)
|
|
438
|
+
ws.column_dimensions[col_letter].width = col_width
|
|
401
439
|
|
|
402
|
-
def _autofit_columns(ws: Any) -> None:
|
|
403
|
-
column_letters = tuple(
|
|
404
|
-
get_column_letter(col_number + 1) for col_number in range(ws.max_column)
|
|
405
|
-
)
|
|
406
|
-
for column_letter in column_letters:
|
|
407
|
-
ws.column_dimensions[column_letter].auto_fit = True
|
|
408
440
|
|
|
441
|
+
def _merge_cells(ws, cells_to_merge, title: str | None = None) -> None:
|
|
442
|
+
add = 0
|
|
443
|
+
if title:
|
|
444
|
+
add = 1
|
|
409
445
|
|
|
410
|
-
def _merge_cells(ws, cells_to_merge):
|
|
411
446
|
for row_idx, merge in cells_to_merge.items():
|
|
447
|
+
row_idx = row_idx + add
|
|
412
448
|
for start, stop in merge:
|
|
413
449
|
cell = ws.cell(row_idx + 1, start)
|
|
414
450
|
cell.alignment = Alignment(horizontal="center")
|
|
@@ -422,14 +458,15 @@ def _merge_cells(ws, cells_to_merge):
|
|
|
422
458
|
|
|
423
459
|
def _insert_source(ws, source, arr):
|
|
424
460
|
height, width = arr.shape
|
|
425
|
-
cell = ws.cell(
|
|
426
|
-
cell.font = Font(**STYLES["calibri"]["font"])
|
|
461
|
+
cell = ws.cell(height + 1, width, source)
|
|
427
462
|
cell.alignment = Alignment(horizontal="right")
|
|
463
|
+
cell.font = Font(**STYLES["calibri"]["font"])
|
|
428
464
|
|
|
429
465
|
|
|
430
466
|
def _insert_title(ws: Any, title: str) -> None:
|
|
431
467
|
ws.insert_rows(0)
|
|
432
468
|
cell = ws.cell(1, 1, title)
|
|
469
|
+
cell.alignment = Alignment(horizontal="left")
|
|
433
470
|
for t, kwa in STYLES["title_bold"].items():
|
|
434
471
|
setattr(cell, t, LOOKUP[t](**kwa))
|
|
435
472
|
|
|
@@ -455,10 +492,11 @@ def write_table(
|
|
|
455
492
|
blue_border: bool | None = True,
|
|
456
493
|
blue_border_row_ids: int | list[int] | None = None,
|
|
457
494
|
number_format: str = "0.0",
|
|
458
|
-
autofit_columns:
|
|
495
|
+
autofit_columns: str | None = "column_names",
|
|
459
496
|
col_filter: bool | None = False,
|
|
460
497
|
style: str = "old",
|
|
461
498
|
combine_multiindex: bool | int = False,
|
|
499
|
+
column_names_to_string: bool = True,
|
|
462
500
|
):
|
|
463
501
|
"""_summary_
|
|
464
502
|
|
|
@@ -492,6 +530,9 @@ def write_table(
|
|
|
492
530
|
data = {"Sheet1": data}
|
|
493
531
|
|
|
494
532
|
for sheet_name, df in data.items():
|
|
533
|
+
if column_names_to_string == True:
|
|
534
|
+
df = cols_to_str(df)
|
|
535
|
+
|
|
495
536
|
arr = df_to_array(df)
|
|
496
537
|
|
|
497
538
|
blue_rows = []
|
|
@@ -503,6 +544,7 @@ def write_table(
|
|
|
503
544
|
p_ids = []
|
|
504
545
|
cells_to_merge = []
|
|
505
546
|
title_tbl = None
|
|
547
|
+
title_src = None
|
|
506
548
|
|
|
507
549
|
if isinstance(header_row, int):
|
|
508
550
|
blue_rows.extend(list(range(0, header_row + 1)))
|
|
@@ -513,6 +555,12 @@ def write_table(
|
|
|
513
555
|
elif isinstance(title, dict):
|
|
514
556
|
title_tbl = title.get(sheet_name)
|
|
515
557
|
|
|
558
|
+
if source:
|
|
559
|
+
if isinstance(source, str):
|
|
560
|
+
title_src = source
|
|
561
|
+
elif isinstance(title, dict):
|
|
562
|
+
title_src = source.get(sheet_name)
|
|
563
|
+
|
|
516
564
|
if right_align_ids:
|
|
517
565
|
r_align_ids.extend(right_align_ids)
|
|
518
566
|
|
|
@@ -557,6 +605,13 @@ def write_table(
|
|
|
557
605
|
if combine_multiindex:
|
|
558
606
|
cells_to_merge = get_cells_to_merge(df)
|
|
559
607
|
|
|
608
|
+
if autofit_columns == "column_names":
|
|
609
|
+
col_widths = get_max_col_widths(df)
|
|
610
|
+
elif autofit_columns == "all_data":
|
|
611
|
+
col_widths = get_max_col_widths(arr)
|
|
612
|
+
else:
|
|
613
|
+
col_widths = None
|
|
614
|
+
|
|
560
615
|
ws = wb.create_sheet(sheet_name)
|
|
561
616
|
|
|
562
617
|
fmt = cell_formatting(
|
|
@@ -578,9 +633,9 @@ def write_table(
|
|
|
578
633
|
arr=arr,
|
|
579
634
|
fmt=fmt,
|
|
580
635
|
title=title_tbl,
|
|
581
|
-
source=
|
|
636
|
+
source=title_src,
|
|
582
637
|
col_filter=col_filter,
|
|
583
|
-
|
|
638
|
+
col_widths=col_widths,
|
|
584
639
|
cells_to_merge=cells_to_merge,
|
|
585
640
|
)
|
|
586
641
|
|
|
@@ -3,8 +3,6 @@ pyproject.toml
|
|
|
3
3
|
src/toolsos/__init__.py
|
|
4
4
|
src/toolsos/cbs_tools.py
|
|
5
5
|
src/toolsos/create_tables.py
|
|
6
|
-
src/toolsos/database_connection.py
|
|
7
|
-
src/toolsos/database_transfer.py
|
|
8
6
|
src/toolsos/download.py
|
|
9
7
|
src/toolsos/geo.py
|
|
10
8
|
src/toolsos/helpers.py
|
|
@@ -14,6 +12,8 @@ src/toolsos.egg-info/SOURCES.txt
|
|
|
14
12
|
src/toolsos.egg-info/dependency_links.txt
|
|
15
13
|
src/toolsos.egg-info/requires.txt
|
|
16
14
|
src/toolsos.egg-info/top_level.txt
|
|
15
|
+
src/toolsos/database/database_connection.py
|
|
16
|
+
src/toolsos/database/database_transfer.py
|
|
17
17
|
src/toolsos/huisstijl/__init__.py
|
|
18
18
|
src/toolsos/huisstijl/colors.py
|
|
19
19
|
src/toolsos/huisstijl/graphs/__init__.py
|
|
@@ -23,5 +23,6 @@ src/toolsos/huisstijl/graphs/linegraph.py
|
|
|
23
23
|
src/toolsos/huisstijl/graphs/piegraph.py
|
|
24
24
|
src/toolsos/huisstijl/graphs/styler.py
|
|
25
25
|
src/toolsos/huisstijl/tables/__init__.py
|
|
26
|
+
src/toolsos/huisstijl/tables/table_helpers.py
|
|
26
27
|
src/toolsos/huisstijl/tables/table_styles.py
|
|
27
28
|
src/toolsos/huisstijl/tables/tables.py
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|