The Graph Package

Cells

The ‘atom’ of GraphBook is the Cell.

class graphbook.graph.cell.Cell(contents: bytes)[source]

A Cell is just a mechanism for capturing some information, and pairs some binary content with a unique ID and a type. This superclass provides the content and ID, but subclasses must set the type.

dup() → graphbook.graph.cell.Cell[source]

Return a duplicate of this cell with a different ID.

execute() → str[source]

Return the results of executing this cell.

classmethod from_obj(obj: Dict[str, str]) → graphbook.graph.cell.Cell[source]

Parse an object as a Cell.

is_executable() → bool[source]

Return True if this cell can be executed.

render(decoder=None) → str[source]

Return the contents of the cell suitable for display.

to_obj() → Dict[str, str][source]

Return a dictionary of the cell suitable for serialising.

class graphbook.graph.cell.MicroSchemeCell(contents: bytes)[source]

A MicroSchemeCell supports a small Scheme language; note that this is missing many features from a full scheme, but serves as an illustrative language for basic prototyping of ideas. It is based on a TextCell, as the program source is just plain text.

Caveats:

  • function definitions must be in the format (define fun (lambda ...)).

  • strings aren’t a separate type.

execute() → str[source]

Execute the uScheme code in the cell. Each call executes the program in a clean environment.

is_executable()[source]

Return True if this cell can be executed.

class graphbook.graph.cell.TextCell(contents: bytes)[source]

TextCells store unformatted plain text, rendered as UTF-8.

execute() → str[source]

Return the results of executing this cell.

classmethod from_obj(obj: Dict[str, str]) → graphbook.graph.cell.TextCell[source]

Parse an object as a Cell.

is_executable() → bool[source]

Return True if this cell can be executed.

render(decoder=<function _decode>) → str[source]

Return the contents of the cell suitable for display.

to_obj()[source]

Return a dictionary of the cell suitable for serialising.

Nodes

Nodes serve as pages in a notebook. They’re a list of cells, along with links and other metadata.

class graphbook.graph.node.Node(title: str)[source]

Nodes serve as pages in a notebook. They’re a list of cells, along with links to other nodes.

add(cell: graphbook.graph.cell.Cell) → None[source]

Append cell to this node’s cell list.

classmethod from_obj(obj)[source]

Parse obj as a node.

insert(cell: graphbook.graph.cell.Cell, index: int)[source]

Insert cell at the given index.

Register a link to another node.

remove(index: Optional[int] = None) → None[source]

Remove the cell at the given index.

render() → str[source]

Render all the nodes.

tag(tag: str) → None[source]

Add a tag to this node.

to_obj()[source]

Return a dictionary of the node suitable for serialising.

untag(tag: str) → None[source]

Remove a tag from this node.

Notebooks

A Notebook is the top-level GraphBook abstraction.

class graphbook.graph.notebook.NodeEntry(id: str, title: str, tags: Iterable[str], links: Iterable[str])[source]

A NodeEntry contains metadata about a node that’s useful for a frontend to use in place of a full node.

to_obj() → Dict[str, Any][source]

Convert a NodeEntry to an object.

class graphbook.graph.notebook.Notebook(path: str = 'graphbook')[source]

A Notebook points to a directory of Nodes.

noder(node_id) → Optional[graphbook.graph.node.Node][source]

Read the node with the given node ID from disk. This will also update the internal caches as necessary.

nodew(_node: graphbook.graph.node.Node) → None[source]

Write the node to disk. This will update the internal caches as necessary.

scan() → int[source]

Rebuild the internal caches by scanning the notebook directory.

select(text: str = '', cased: bool = False, and_tags: Optional[List[str]] = None) → List[graphbook.graph.notebook.NodeEntry][source]

Return the list of titles for all nodes; if text is not empty, nodes whose titles start with or contain text will be returned.