|
| 1 | +#################################################################### |
| 2 | +# |
| 3 | +# https://stackoverflow.com/questions/25777037/how-can-i-left-justify-text-in-a-pandas-dataframe-column-in-an-ipython-notebook |
| 4 | +# |
| 5 | +#################################################################### |
| 6 | + |
| 7 | +import mplfinance as mpf |
| 8 | +import pandas as pd |
| 9 | +import textwrap |
| 10 | + |
| 11 | +vk = mpf.plotting._valid_plot_kwargs() |
| 12 | + |
| 13 | +df = (pd.DataFrame(vk).T.head(18)).drop('Validator',axis=1) |
| 14 | + |
| 15 | +df['Kwarg'] = df.index.values |
| 16 | +df['Default'] = ["'"+d+"'" if isinstance(d,str) else str(d) for d in df['Default']] |
| 17 | + |
| 18 | +df = df[['Kwarg','Default','Description']] |
| 19 | +df = df.head(5).append(df.tail(7)) |
| 20 | + |
| 21 | +# df.sort_index(inplace=True) |
| 22 | + |
| 23 | +df |
| 24 | + |
| 25 | +print('===========================') |
| 26 | + |
| 27 | +print(df) |
| 28 | + |
| 29 | +print('===========================') |
| 30 | + |
| 31 | +def make_left_formatter(maxwidth): |
| 32 | + wm3 = maxwidth-3 |
| 33 | + w = maxwidth |
| 34 | + def left_formatter(value): |
| 35 | + if not isinstance(value,str): |
| 36 | + return f'{value:<}' |
| 37 | + elif value[0:maxwidth] == '-'*maxwidth: |
| 38 | + return f'{value:<{w}.{w}s}' |
| 39 | + #elif len(value) > maxwidth and value[0:maxwidth] != '-'*maxwidth: |
| 40 | + elif len(value) > maxwidth: |
| 41 | + return f'{value:<{wm3}.{wm3}s}...' |
| 42 | + else: |
| 43 | + return f'{value:<{w}.{w}s}' |
| 44 | + return left_formatter |
| 45 | + |
| 46 | +WRAPLEN=55 |
| 47 | + |
| 48 | +def df_wrapcol(df,wrap_column=None,wrap_length=None): |
| 49 | + |
| 50 | + if wrap_column is None: return df |
| 51 | + if wrap_length is None: return df |
| 52 | + |
| 53 | + index = [] |
| 54 | + columns = {} |
| 55 | + for col in df.columns: |
| 56 | + columns[col] = [] |
| 57 | + nonwrapcols = [col for col in df.columns if col != wrap_column] |
| 58 | + |
| 59 | + for ix in df.index: |
| 60 | + row = df.loc[ix,] |
| 61 | + |
| 62 | + swrap = str(row[wrap_column]) |
| 63 | + tw = textwrap.wrap(swrap,wrap_length) if not swrap.isspace() else [' '] |
| 64 | + |
| 65 | + columns[wrap_column].append(tw[0]) |
| 66 | + index.append(str(ix)) |
| 67 | + for col in nonwrapcols: |
| 68 | + columns[col].append(row[col]) |
| 69 | + |
| 70 | + if len(tw) > 1: |
| 71 | + for r in range(1,len(tw)): |
| 72 | + columns[wrap_column].append(tw[r]) |
| 73 | + index.append(str(ix)+'.'+str(r)) |
| 74 | + for col in nonwrapcols: |
| 75 | + columns[col].append(' ') |
| 76 | + |
| 77 | + return pd.DataFrame(columns,index=index) |
| 78 | + |
| 79 | + |
| 80 | +df = df_wrapcol(df,wrap_column='Description',wrap_length=WRAPLEN) |
| 81 | +print('===========================') |
| 82 | +print('dfnew1=',df) |
| 83 | + |
| 84 | + |
| 85 | +# print('===========================') |
| 86 | +# df.columns = [ ' '+col for col in df.columns ] |
| 87 | + |
| 88 | +dividers = [] |
| 89 | +for col in df.columns: |
| 90 | + dividers.append('-'*int(df[col].str.len().max())) |
| 91 | +dfd = pd.DataFrame(dividers).T |
| 92 | +dfd.columns = df.columns |
| 93 | +dfd.index = pd.Index(['---']) |
| 94 | + |
| 95 | +print('===========================') |
| 96 | + |
| 97 | +df = dfd.append(df) |
| 98 | + |
| 99 | +fmts = {'Kwarg': make_left_formatter(df['Kwarg'].str.len().max()+1), |
| 100 | + 'Description': make_left_formatter(WRAPLEN), |
| 101 | + 'Default': make_left_formatter(8), |
| 102 | + } |
| 103 | +s = df.to_string(formatters=fmts,index=False,justify='left') |
| 104 | + |
| 105 | +print('\n ',s.replace('\n','\n ')) |
| 106 | + |
| 107 | +print('===========================') |
| 108 | + |
0 commit comments