Disable tqdm within context and refresh tqdm when exits. Useful when writing to standard output stream
(cls, file=None, nolock=False)
| 725 | @classmethod |
| 726 | @contextmanager |
| 727 | def external_write_mode(cls, file=None, nolock=False): |
| 728 | """ |
| 729 | Disable tqdm within context and refresh tqdm when exits. |
| 730 | Useful when writing to standard output stream |
| 731 | """ |
| 732 | fp = file if file is not None else sys.stdout |
| 733 | |
| 734 | try: |
| 735 | if not nolock: |
| 736 | cls.get_lock().acquire() |
| 737 | # Clear all bars |
| 738 | inst_cleared = [] |
| 739 | for inst in getattr(cls, '_instances', []): |
| 740 | # Clear instance if in the target output file |
| 741 | # or if write output + tqdm output are both either |
| 742 | # sys.stdout or sys.stderr (because both are mixed in terminal) |
| 743 | if hasattr(inst, "start_t") and (inst.fp == fp or all( |
| 744 | f in (sys.stdout, sys.stderr) for f in (fp, inst.fp))): |
| 745 | inst.clear(nolock=True) |
| 746 | inst_cleared.append(inst) |
| 747 | yield |
| 748 | # Force refresh display of bars we cleared |
| 749 | for inst in inst_cleared: |
| 750 | inst.refresh(nolock=True) |
| 751 | finally: |
| 752 | if not nolock: |
| 753 | cls._lock.release() |
| 754 | |
| 755 | @classmethod |
| 756 | def set_lock(cls, lock): |