Giter Site home page Giter Site logo

Comments (2)

smacke avatar smacke commented on June 3, 2024

To create IPyflow variants of all the various subclasses of InteractiveShell, it seems like the cleanest way (but definitely not the easiest to understand) is to do fancy metaclass stuff to patch IPyflowInteractiveShell into the mro. Toy example:

from IPython.terminal.embed import InteractiveShellEmbed
from IPython.terminal.interactiveshell import InteractiveShell
from traitlets import MetaHasTraits


def make_mro_inserter_metaclass(old_class, new_class):
    class MetaMroInserter(MetaHasTraits):
        def mro(cls):
            ret = []
            for clazz in super().mro():
                if clazz is old_class:
                    ret.append(new_class)
                ret.append(clazz)
            return ret
    return MetaMroInserter


class MyInteractiveShell(InteractiveShell):
    def run_cell(self, *args, **kwargs):
        print("run cell")
        return super().run_cell(*args, **kwargs)


class MyInteractiveShellEmbed(InteractiveShellEmbed, metaclass=make_mro_inserter_metaclass(InteractiveShell, MyInteractiveShell)):
    pass

ipshell = MyInteractiveShellEmbed()
ipshell.run_cell("x = 0")  # prints 'run cell' before running the cell

from ipyflow.

smacke avatar smacke commented on June 3, 2024

This would more readily unblock use cases like #134.

from ipyflow.

Related Issues (20)

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.