Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: notebook setup #164

Merged
merged 3 commits into from
Nov 24, 2024
Merged

feat: notebook setup #164

merged 3 commits into from
Nov 24, 2024

Conversation

PatrickAlphaC
Copy link
Member

@PatrickAlphaC PatrickAlphaC commented Nov 24, 2024

Now, users can enter a .ipynb notebook and run:

Cell 1

from moccasin import setup_notebook

setup_notebook()

Cell 2

from moccasin.config import get_active_network

active_network = get_active_network()
eth_usd = active_network.manifest_named("eth_usd_price_feed")

And it'll auto-pull in their config files, change the python environment, and such.

@PatrickAlphaC
Copy link
Member Author

PatrickAlphaC commented Nov 24, 2024

It might be good for moccasin to "auto-detect" you're in a notebook, like here:

https://github.com/vyperlang/titanoboa/blob/f2c883b50f3f72a417f2b9785146874016898968/boa/ipython.py#L42-L43

and here:

https://github.com/vyperlang/titanoboa/blob/f2c883b50f3f72a417f2b9785146874016898968/boa/ipython.py#L42-L43

With techniques:

https://stackoverflow.com/questions/15411967/how-can-i-check-if-code-is-executed-in-the-ipython-notebook

AI Suggestion:

# In moccasin/__init__.py
def _is_running_in_ipython():
    try:
        from IPython import get_ipython
        return get_ipython() is not None
    except ImportError:
        return False

# Automatically setup if we're in IPython
if _is_running_in_ipython():
    setup_notebook()

or:

def setup_for_interactive():
    try:
        # Check if we're in IPython/Jupyter
        ipython = get_ipython()
        
        if ipython is None:
            # Regular Python
            return
            
        if 'IPKernelApp' in ipython.config:
            # We're in a Jupyter notebook
            setup_notebook_specific_features()
        else:
            # We're in regular IPython shell
            setup_ipython_specific_features()
            
    except ImportError:
        # Regular Python
        pass

Which is like... sort of gross

@PatrickAlphaC PatrickAlphaC marked this pull request as ready for review November 24, 2024 23:02
@PatrickAlphaC
Copy link
Member Author

I like this for now, I'm adding it

@PatrickAlphaC PatrickAlphaC merged commit c85c815 into main Nov 24, 2024
2 checks passed
@PatrickAlphaC PatrickAlphaC deleted the feat/notebook-support branch November 24, 2024 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant