Skip to content

Commit

Permalink
feat: notebook setup (#164)
Browse files Browse the repository at this point in the history
* feat: notebook setup

* fix: make it so users can rerun the setup

* feat: juypter notebook support
  • Loading branch information
PatrickAlphaC authored Nov 24, 2024
1 parent 22511bf commit c85c815
Show file tree
Hide file tree
Showing 7 changed files with 506 additions and 5 deletions.
30 changes: 30 additions & 0 deletions docs/source/how-tos/juypter_notebooks.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Juypter Notebooks (IPython)
###########################

If you want to run a Jupyter notebook with all the setup a script has (named contracts, ``sys.path`` adjustments, etc), you can use the following code:

.. code-block:: python
from moccasin import setup_notebook
setup_notebook()
Then, you can work with the notebook as you would normally.

.. code-block:: python
from moccasin.config import get_active_network
active_network = get_active_network()
eth_usd = active_network.manifest_named("eth_usd_price_feed")
If you update your config while the notebook is running, you can reload the config with:

.. code-block:: python
from moccasin.config import get_config
config = get_config()
# This command
config.reload()
1 change: 1 addition & 0 deletions docs/source/toctree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Moccasin
Contract Verification <how-tos/verify_contracts.rst>
Stateless Fuzzing <how-tos/stateless_fuzzing.rst>
Stateful Fuzzing <how-tos/stateful_fuzzing.rst>
how-tos/juypter_notebooks.rst


.. toctree::
Expand Down
19 changes: 19 additions & 0 deletions moccasin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from pathlib import Path

from moccasin import __main__

Expand All @@ -11,5 +12,23 @@ def version() -> str:
return __main__.get_version()


def setup_notebook(path: str | Path | None = None):
from pathlib import Path

from moccasin._sys_path_and_config_setup import (
_set_sys_path,
_setup_network_and_account_from_config_and_cli,
get_sys_paths_list,
)
from moccasin.config import get_or_initialize_config

normalized_path: Path = Path(path) if path is not None else Path.cwd()
config = get_or_initialize_config(normalized_path)

# Set up the environment (add necessary paths to sys.path, etc.)
_set_sys_path(get_sys_paths_list(config))
_setup_network_and_account_from_config_and_cli()


if __name__ == "__main__":
main()
13 changes: 13 additions & 0 deletions moccasin/_sys_path_and_config_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ def get_sys_paths_list(config: Config) -> List[Path]:
]


def _set_sys_path(paths: List[Path]):
str_paths = [str(p) for p in paths]
anchor2 = os.environ.get("PYTHONPATH")
python_path = anchor2
if python_path is None:
python_path = ":".join(str_paths)
else:
python_path = ":".join([*str_paths, python_path])
os.environ["PYTHONPATH"] = python_path
# add these with highest precedence -- conflicts should prefer user modules/code
sys.path = str_paths + sys.path


@contextlib.contextmanager
def _patch_sys_path(paths: List[Path]) -> Iterator[None]:
str_paths = [str(p) for p in paths]
Expand Down
3 changes: 3 additions & 0 deletions moccasin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,9 @@ def _load_config(self, config_path: Path, pyproject_path: Path | None = None):
def _load_env_file(self):
load_dotenv(dotenv_path=self.project_root.joinpath(self.dot_env))

def reload(self):
self.__init__(self.project_root)

def get_config_path(self) -> Path:
return self.config_path

Expand Down
8 changes: 5 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "moccasin"
version = "0.3.4"
version = "0.3.5"
description = "Pythonic smart contract development framework using Titanoboa"
authors = [
{ name = "PatrickAlphac", email = "[email protected]" },
Expand All @@ -11,12 +11,14 @@ dependencies = [
"python-dotenv>=1.0.1",
"titanoboa-zksync>=0.2.8",
"tqdm>=4.66.5",
"tomlkit>=0.13.2", # For preserving comments when writing to toml
"tomlkit>=0.13.2",
# For preserving comments when writing to toml
"tomli-w>=1.0.0",
"pytest-cov>=5.0.0",
"uv>=0.4.15",
"pytest-xdist>=3.6.1",
"vyper==0.4.0", # We will want to get rid of this hard-coded dependency in 0.3.4 of moccasin
"vyper>=0.4.0",
"ipykernel>=6.29.5",
]
readme = "README.md"
requires-python = ">= 3.11, <= 3.13"
Expand Down
Loading

0 comments on commit c85c815

Please sign in to comment.