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

ENH: Add options to not include external links, and self references #16

Merged
merged 2 commits into from
Aug 11, 2019
Merged
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ To use Graphviz to generate the visualization, add the `--use-graphviz` option:
~/envs/zkviz/bin/zkviz --notes-dir ~/Notes --use-graphviz
```

By default, zkviz will draw a node for every reference found in the files
provided, even if the referenced zettel does not exist, and even if a zettel
refers to itself. You can change that behavior in two ways. The `--only-list`
option tells zkviz to draw links only to zettels that have been provided to it.
In the example below, only links between the two zettels will be shown:

```sh
~/envs/zkviz/bin/zkviz --only-list "20190810190224 Note 1.md" "20190810190230 Note 2.md"
```

The other way to change the behavior is to disable self-reference links using
the `--no-self-ref` option.

## Using zkviz with Keyboard Maestro

The `keyboard-maestro` folder includes a [Keyboard Maestro](https://www.keyboardmaestro.com)
Expand Down
53 changes: 44 additions & 9 deletions zkviz/zkviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,49 @@ def parse_zettels(filepaths):
return documents


def create_graph(zettels, graph):
def create_graph(zettels, graph, include_self_references=True,
only_listed=False):
"""
Create of graph of the zettels linking to each other.

Parameters
----------
zettels : list of dictionaries
include_self_references : bool, optional
Include links to the source document. Defaults to True.
only_listed : bool, optional
Only include nodes in the graph it's actually one of the zettels.
Default is False.

Returns
-------
graph : nx.Graph

"""

for doc in zettels:
graph.add_node(doc['id'], title=doc['title'])
for link in doc['links']:
graph.add_edge(doc['id'], link)
# Collect IDs from source zettels and from zettels linked
zettel_ids = set()
link_ids = set()
for zettel in zettels:
zettel_ids.add(zettel['id'])
link_ids.update(zettel['links'])

if only_listed:
ids_to_include = zettel_ids
else:
ids_to_include = zettel_ids | link_ids

for zettel in zettels:
graph.add_node(zettel['id'], title=zettel['title'])
for link in zettel['links']:
if link not in ids_to_include:
continue
if include_self_references or (zettel['id'] != link):
# Truth table for the inclusion conditional
# IS_SAME IS_DIFF (Is different ID)
# INCLUDE T T
# DON'T INCLUDE F T
graph.add_edge(zettel['id'], link)
return graph


Expand Down Expand Up @@ -91,10 +116,15 @@ def parse_args(args=None):
parser.add_argument('--output', default='zettel-network',
help='name of output file. [zettel-network]')
parser.add_argument('--pattern', action='append',
help=('pattern to match notes. You can repeat this argument to'
' match multiple file types. [*.md]'))
help=('pattern to match notes. You can repeat this argument to'
' match multiple file types. [*.md]'))
parser.add_argument('--use-graphviz', action='store_true', default=False,
help='Use Graphviz instead of plotly to render the network.')
help='Use Graphviz instead of plotly to render the network.')
parser.add_argument('--no-self-ref', action='store_false', default=True,
dest='include_self_references',
help='Do not include self-references in a zettel.')
parser.add_argument('--only-listed', action='store_true', default=False,
help='Only include links to documents that are in the file list')
parser.add_argument('zettel_paths', nargs='*', help='zettel file paths.')
args = parser.parse_args(args=args)

Expand Down Expand Up @@ -135,7 +165,12 @@ def main(args=None):
from zkviz.plotly import NetworkPlotly
graph = NetworkPlotly()

graph = create_graph(zettels, graph)
graph = create_graph(
zettels,
graph,
include_self_references=args.include_self_references,
only_listed=args.only_listed,
)
graph.render(args.output)


Expand Down