Last active
January 17, 2021 19:55
-
-
Save robdmc/611b57470ca1522c4f3c496a7c967a84 to your computer and use it in GitHub Desktop.
Visidata Monstrosity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def syncSaveSheets(givenpath, *vsheets, confirm_overwrite=False): | |
| """ | |
| Copy of saveSheets function, but without async stuff | |
| """ | |
| filetype = givenpath.ext or options.save_filetype | |
| savefunc = getattr(vd, 'save_' + filetype, None) or vd.fail('no function to save as type %s' % filetype) | |
| if givenpath.exists() and confirm_overwrite: | |
| confirm("%s already exists. overwrite? " % givenpath.given) | |
| if not givenpath.given.endswith('/'): # forcibly specify save individual files into directory by ending path with / | |
| return vd.execAsync(savefunc, givenpath, *vsheets) | |
| # more than one sheet; either no specific multisave for save filetype, or path ends with / | |
| # save as individual files in the givenpath directory | |
| try: | |
| os.makedirs(givenpath, exist_ok=True) | |
| except FileExistsError: | |
| pass | |
| if not givenpath.is_dir(): | |
| vd.fail(f'cannot save multiple {filetype} sheets to non-dir') | |
| # get save function to call | |
| for vs in vsheets: | |
| p = Path((givenpath / vs.name).with_suffix('.'+filetype)) | |
| savefunc(p, vs) | |
| #vd.execAsync(savefunc, p, vs) | |
| return | |
| def gui(): | |
| import pandas as pd | |
| from pandasgui import show | |
| import datetime | |
| import multiprocessing | |
| import time | |
| now = datetime.datetime.now() | |
| path_str = f"/tmp/{now.strftime('__visidata_gui_%Y-%m-%d_%H.%M.%S')}_pid.{os.getpid()}__.csv" | |
| path = visidata.path.Path(path_str) | |
| sheet = vd.sheet | |
| syncSaveSheets(path, sheet) | |
| is_done = False | |
| for nn in range(10): | |
| time.sleep(1) | |
| try: | |
| df = pd.read_csv(path_str) | |
| if len(df) == len(sheet): | |
| is_done = True | |
| break | |
| except: | |
| pass | |
| if is_done: | |
| try: | |
| show(df) | |
| finally: | |
| os.unlink(path_str) | |
| status('Returned from Pandasgui') | |
| else: | |
| status('Pandasgui timed out') | |
| BaseSheet.addCommand('gG', 'pandasgui', 'gui()') |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alright. I changed this to basically copy from the numpy saver, and it's much better.