Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nbcommands/__version__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

VERSION = (0, 3, 2)
VERSION = (0, 3, 3)
PRERELEASE = None # alpha, beta or rc
REVISION = None

Expand Down
6 changes: 3 additions & 3 deletions nbcommands/_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def cat(ctx, *args, **kwargs):
# Source: https://github.com/jbn/nbmerge
merged, metadata = None, []

for file in kwargs["file"]:
with open(file, "r") as f:
for fin in kwargs["file"]:
with open(fin, "r") as f:
nb = nbformat.read(f, as_version=4)

metadata.append(nb.metadata)
Expand All @@ -35,7 +35,7 @@ def cat(ctx, *args, **kwargs):
merged_metadata.update(meta)
merged.metadata = merged_metadata

if kwargs["output"] is not None:
if kwargs["output"]:
with open(kwargs["output"], "w") as f:
nbformat.write(merged, f, version=4)
else:
Expand Down
36 changes: 8 additions & 28 deletions nbcommands/_grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,13 @@

import re

from ansimarkup import parse as ansi
import click
import nbformat
from colorama import Fore, Style

from . import __version__


def color(s, c, style="bright"):
color_map = {
"black": Fore.BLACK,
"red": Fore.RED,
"green": Fore.GREEN,
"yellow": Fore.YELLOW,
"blue": Fore.BLUE,
"magenta": Fore.MAGENTA,
"cyan": Fore.CYAN,
"white": Fore.WHITE,
}
style_map = {
"dim": Style.DIM,
"normal": Style.NORMAL,
"bright": Style.BRIGHT,
}

return color_map[c] + style_map[style] + s + Style.RESET_ALL


@click.command(name="nbgrep")
@click.version_option(version=__version__)
@click.argument("pattern")
Expand All @@ -38,22 +18,22 @@ def grep(ctx, *args, **kwargs):
"""Search for a pattern in Jupyter notebooks."""
pattern = kwargs["pattern"]

for file in kwargs["file"]:
with open(file, "r") as f:
for fin in kwargs["file"]:
with open(fin, "r") as f:
nb = nbformat.read(f, as_version=4)

for cell_n, cell in enumerate(nb.cells):
for line_n, line in enumerate(cell.source.split("\n")):
search = re.search(pattern, line)
if search is not None:
first, last = search.span()
match = color(line[first:last], "red")
match = ansi(f"<red>{line[first:last]}</red>")
click.echo(
color(":", "cyan").join(
ansi("<cyan>:</cyan>").join(
[
color(file, "white", style="normal"),
color("cell {}".format(cell_n + 1), "green"),
color("line {}".format(line_n + 1), "green"),
ansi(f"<white>{fin}</white>"),
ansi(f"<green>cell {cell_n+1}</green>"),
ansi(f"<green>line {line_n+1}</green>"),
line.replace(pattern, match),
]
)
Expand Down
18 changes: 8 additions & 10 deletions nbcommands/terminal.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
# -*- coding: utf-8 -*-

from colorama import Fore, Style
from ansimarkup import parse as ansi

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.lexers.markup import MarkdownLexer
from pygments.formatters import TerminalTrueColorFormatter

LEXER_MAP = {"markdown": MarkdownLexer(), "code": PythonLexer()}


def display(cells):
output = []

for cell in cells:
execution_count = (
cell["execution_count"] if cell["execution_count"] is not None else " "
)
prompt = (
Fore.GREEN
+ Style.BRIGHT
+ "In [{}]: ".format(execution_count)
+ Style.RESET_ALL
execution_count = cell.get("execution_count") or " "
prompt = ansi(f"<green>In [{execution_count}]</green>: ")
code = highlight(
cell.source, LEXER_MAP[cell.cell_type], TerminalTrueColorFormatter()
)
code = highlight(cell.source, PythonLexer(), TerminalTrueColorFormatter())
output.append(prompt + code)

return output
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
requires = [
"black==19.10b0",
"Click==7.0",
"colorama==0.4.1",
"ansimarkup==1.4.0",
"nbformat==4.4.0",
"Pygments==2.4.2",
]
Expand Down