Giter Club home page Giter Club logo

Comments (2)

boba-and-beer avatar boba-and-beer commented on June 15, 2024

Reproducible code:

"""Get a good progress bar
"""
# from contextlib import nullcontext
from contextlib import AbstractContextManager


class ProgressBar:
    def __call__(self, *args, **kwargs):
        #self.logger.info("WHAT BAR")
        return self.get_bar()(*args, **kwargs)
    
    @staticmethod
    def is_in_ipython():
        """
        Determines if current code is executed within an ipython session.
        """
        is_in_ipython = False
        # Check if the runtime is within an interactive environment, i.e., ipython.
        try:
            from IPython import get_ipython  # pylint: disable=import-error
            if get_ipython():
                is_in_ipython = True
        except ImportError:
            pass  # If dependencies are not available, then not interactive for sure.
        return is_in_ipython

    def is_in_notebook(self) -> bool:
        """
        Determines if current code is executed from an ipython notebook.
        """
        is_in_notebook = False
        if self.is_in_ipython():
            # The import and usage must be valid under the execution path.
            from IPython import get_ipython
            if 'IPKernelApp' in get_ipython().config:
                is_in_notebook = True
        return is_in_notebook
    
    def get_bar(self):
        if self.is_in_notebook():
            from tqdm.notebook import tqdm as notebook_bar
            return notebook_bar
        from tqdm import tqdm as normal_bar
        return normal_bar

class NullProgressBar(AbstractContextManager):
    """Context manager that does no additional processing.

    Used as a stand-in for a normal context manager, when a particular
    block of code is only sometimes used with a normal context manager:

    cm = optional_cm if condition else nullcontext()
    with cm:
        # Perform operation, using optional_cm if condition is True
    """

    def __init__(self, enter_result: int=None):
        self.enter_result = enter_result

    def __enter__(self):
        return self.enter_result

    def __exit__(self, *excinfo):
        pass

    def __iter__(self):
        if self.enter_result is None:
            self.enter_result = 999999999999999
        for i in range(self.enter_result):
            yield i

def progress_bar(*args, show_progress_bar: bool=False, **kwargs):
    try:
        if show_progress_bar:
            print('1')
            return ProgressBar()(*args, **kwargs)
    except:
        print('2')
        return NullProgressBar()
    return NullProgressBar()


if __name__ == "__main__":
    print(type(ProgressBar()(range(5), show_progress_bar = True)))



    # x = 0 
    # for i in progress_bar(range(5)): 
    #     x+=1
    #     print(i)
    #     if x > 10:
    #         break

from relevanceai.

boba-and-beer avatar boba-and-beer commented on June 15, 2024

Solved son./

from relevanceai.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.