# @cylynx/pymotif

> jupyter widget bindings for the motif library

Latest version **0.0.6** (published 2021-10-15) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install @cylynx/pymotif
pnpm add @cylynx/pymotif
yarn add @cylynx/pymotif
bun add @cylynx/pymotif
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2021-10-15 |
| First published | 2021-05-21 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 10 |
| Unpacked size | 2.8 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 140 |
| Author | Cylynx |
| Maintainers | timothy-lynx, cylynx-yinghua |
| Keywords | jupyter, jupyterlab, jupyterlab-extension, widgets |

## Links

- npm: https://www.npmjs.com/package/@cylynx/pymotif
- Repository: https://github.com/Cylynx/motif.gl
- Issues: https://github.com/Cylynx/motif.gl/issues
- npm.io page: https://npm.io/package/@cylynx/pymotif

## Dependencies (10)

- [react](https://npm.io/package/react.md) ^16.8.6
- [baseui](https://npm.io/package/baseui.md) ^9.90.0
- [react-dom](https://npm.io/package/react-dom.md) ^16.8.6
- [react-redux](https://npm.io/package/react-redux.md) ^7.2.3
- [@cylynx/motif](https://npm.io/package/@cylynx/motif.md) ^0.0.14
- [react-hook-form](https://npm.io/package/react-hook-form.md) ^7.3.4
- [styletron-react](https://npm.io/package/styletron-react.md) ^5.2.7
- [@reduxjs/toolkit](https://npm.io/package/@reduxjs/toolkit.md) ^1.5.0
- [@jupyter-widgets/base](https://npm.io/package/@jupyter-widgets/base.md) ^1.1.10 || ^2.0.0 || ^3.0.0 || ^4.0.0
- [styletron-engine-atomic](https://npm.io/package/styletron-engine-atomic.md) ^1.4.5

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.0.6 (latest) — 2021-10-15
- 0.0.5 — 2021-08-28
- 0.0.4 — 2021-07-22
- 0.0.3 — 2021-07-14
- 0.0.2 — 2021-07-12
- 0.0.1 — 2021-06-16
- 0.0.1-beta.4 — 2021-06-14
- 0.0.1-beta.3 — 2021-05-25
- 0.0.1-beta.2 — 2021-05-21
- 0.0.1-beta.1 — 2021-05-21

## README

# Pymotif

A Python package that lets you plot Motif graphs within Jupyter Notebook / Jupyter Lab:

![Pymotif basic usage](img/pymotif-basic.png)

It's that easy to get started!

# Features

- Seamless integration into existing Jupyter workflows
- Multiple data import options
- Programmatic graph manipulation
- Easy code sharing and reuse

# Installation

You can install using `pip` (we recommend using virtual environments):

```
pip install pymotif
```

And it should work. In some cases, you may also need to install and enable Jupyter extensions:

```
# Jupyter Lab
jupyter labextension install @jupyter-widgets/jupyterlab-manager

# For Jupyter Lab <= 2, you may need to install the extension manually
jupyter labextension install @cylynx/pymotif

# For Jupyter Notebook <= 5.2, you may need to enable nbextensions
jupyter nbextension enable --py [--sys-prefix|--user|--system] pymotif
```

# Demo

Demo notebooks can be found in the `examples` folder. For a start, check out `examples/introduction.ipynb`, which gives a quick overview of the available functionality!

# Motif Class

As shown above, using Motif in Jupyter involves importing and instantiating the `Motif` class from `pymotif`.

## Instantiation

```python
from pymotif import Motif

motif = Motif()
motif.plot()    # or just 'motif'
```

`Motif()` accepts various instantiation parameters (refer to Motif's `__init__` method for updated information):

```
All parameters are optional.
Only one graph import (json_path, nx_graph, neo4j_graph, or csv_path) can be passed each time.

json_path: str
    Path to a local JSON file containing the graph data.
    If this is used, all other params will be ignored.

nx_graph: nx.Graph
    A networkx graph to be rendered

neo4j_graph: neo4j.graph.Graph
    A neo4j graph to be rendered, obtained from the neo4j.Result.graph() method.
    Ref: https://neo4j.com/docs/api/python-driver/current/api.html#graph

csv_path: str
    Path to a local CSV edgelist file

style: dict
    The rendered graph's style. Its format depends on Motif's StyleOptions interface:
    https://github.com/cylynx/motif.gl/blob/c79ba6549407979a4ec0214cc6c7c7d0f2a3be41/packages/motif/src/redux/graph/types.ts#L206

title: str
    The rendered graph's title
```

Other params are ignored when using JSON files because the file itself may also contain pre-defined styles, titles, or other settings.

### Example Usage

```python
# import a csv file and set a title
motif = Motif(csv_path=<YOUR CSV PATH>, title='my first csv import')

# import a json file. as mentioned above, using json ignores all other params
motif = Motif(json_path=<YOUR JSON PATH>, title='ignored parameter')

# import a networkx graph and arrange it in a grid layout
style = {'layout': {'type': 'grid'}}
motif = Motif(nx_graph=<YOUR NETWORKX GRAPH>, style=style)
```

## Attributes

There is only one class attribute for now:

```
state: dict
    There are 2 possible keys: data, style.
    Data is a list of graph data describing what will be rendered in the widget.
    Style is a dict describing how the graphs will be rendered.

    Follows the TLoadFormat interface defined in Motif's types.ts:
    https://github.com/cylynx/motif.gl/blob/master/packages/motif/src/redux/graph/types.ts#L283
```

### Example Usage

```python
m = Motif(<YOUR PARAMS>)

# check graph's initial state
m.state

# stuff happens
...

# sanity check
m.state
```

This may be useful for debugging your graph objects at various points in time throughout your analysis.

## Methods

```python
def add_graph(self, **kwargs):
    """
    Adds another graph to an existing Motif widget.
    Takes the same parameters as __init__.
    If provided, graph settings here will overwrite those set previously (e.g. style).
    """


def set_style(self, style: dict, overwrite=False):
    """
    Allows updating the style of an existing widget.

    ------------
     Parameters
    ------------
    style: dict
        The rendered graph's style
    overwrite=False:
        If True, overwrites all existing styles with the passed 'style' param.
        If False, merges 'style' param with existing styles
    """


def plot(self):
    """ Plots the graphs' current state as a Jupyter widget """
```

### Example Usage

```python
# create a new graph
m = Motif(<YOUR PARAMS>)

# add another previously-saved graph from a JSON file
m.add_graph(json_path=<YOUR JSON PATH>)

# adjust and overwrite the combined graphs' style
m.set_style(style=<YOUR STYLE>, overwrite=True)

# plot the combined graph
m.plot()
```

# Development

This section contains instructions for developing Pymotif locally.

For a more thorough walkthrough check out the official Jupyter widgets guide:  
https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Custom.html

## Create a new conda environment with the dependencies

To create the environment, execute the following command:

```
conda create -n motif -c conda-forge jupyterlab nodejs python
```

Then activate the environment with:

```
conda activate motif
```

## Build and install the widget for development

Since the widget contains a Python part, you need to install the package in editable mode:

```
npm run pymotif:build // In root directory to link it with monorepo setup
python -m pip install -e .
```

If you are using JupyterLab:

```
jupyter labextension develop --overwrite .
```

If you are using the Classic Notebook:

```
jupyter nbextension install --sys-prefix --symlink --overwrite --py pymotif
jupyter nbextension enable --sys-prefix --py pymotif
```

To continuously monitor the project for changes and automatically trigger a rebuild, start Jupyter in watch mode:

```
jupyter lab --watch
```

And in a separate session, begin watching the source directory for changes:

```
npm run pymotif  // In root directory to link it with monorepo setup
```

After a change wait for the build to finish and then refresh your browser and the changes should take effect.

If you make a change to the python code then you will need to restart the notebook kernel to have it take effect.

## Publishing

1. Update the version in package.json
2. Relase the `@cylynx/pymotif` packages:

```
npm login
npm run pymotif:publish
```

3. Bundle the python package: `python setup.py sdist bdist_wheel`
4. Update the version in `pymotif/_version.py`
5. If frontend version dependency has changed, update `pymotif/_frontend.py`
6. Publish the package to PyPI:

```
pip install twine
twine upload dist/pymotif*
```

---
_Source: https://npm.io/package/@cylynx/pymotif · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
