Skip to content

Commit

Permalink
docs: add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Nov 1, 2024
1 parent 7e565d5 commit b4d9a9d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 15 additions & 0 deletions docs/userguides/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,18 @@ Out[3]: '0.00040634 ETH'
In [4]: %bal 0xE3747e6341E0d3430e6Ea9e2346cdDCc2F8a4b5b
Out[4]: '0.00040634 ETH'
```

## Executing Code

You can also use the `ape console` to execute programs directly from strings.
This is similar to the `python -c|--code` option except it will display the output cell.
Anything available in `ape console` is also available in `ape console --code`.

```shell
ape console -c 'project.name'
Out[1]: 'my-project'
ape console -c 'x = 3\nx + 1'
Out[1]: 4
ape console -c 'networks.active_provider.name'
Out[1]: 'test'
```
8 changes: 5 additions & 3 deletions src/ape_console/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@


def _code_callback(ctx, param, value) -> list[str]:
breakpoint()
# NOTE: newlines are escaped in code automatically, so we
# need to de-escape them. Any actually escaped newlines
# will still be escaped.
value = value.replace("\\n", "\n").replace("\\t", "\t").replace("\\b", "\b")
return value.splitlines()


Expand Down Expand Up @@ -195,5 +198,4 @@ def _launch_console(
def _execute_code(code: list[str], **ipython_kwargs):
shell = InteractiveShell.instance(**ipython_kwargs)
for line in code:
result = shell.run_cell(line)
click.echo(result.result)
shell.run_cell(line)

0 comments on commit b4d9a9d

Please sign in to comment.