Giter Club home page Giter Club logo

ipyvega's Introduction

IPython Vega

PyPI npm version Build Status

IPython/Jupyter notebook module for Vega 5, and Vega-Lite 4. Notebooks with embedded visualizations can be viewed on GitHub and nbviewer. If you use JupyterLab (not the notebook), you don't need to install this extension since JupyterLab comes with built-in support for Vega and Vega-Lite.

Available on pypi and Conda Forge as vega.

Install and run

Python Package Index

To install vega and its dependencies from the Python Package Index using pip, use the following commands:

pip install jupyter pandas vega
pip install --upgrade notebook  # need jupyter_client >= 4.2 for sys-prefix below
jupyter nbextension install --sys-prefix --py vega
jupyter nbextension enable --py --sys-prefix vega

Conda Forge

If you use Conda, you probably already have the latest versions of the notebook and pandas installed. To install vega extension run:

conda install vega

Usage

Once the package is installed, run

jupyter notebook

to launch the Jupyter notebook server, and use vega within the notebook. See the example notebooks for Vega-Lite and Vega.

To run the notebooks yourself, you need to get the file cars.json.

Developers

This project uses Poetry. If you prefer a local virtual environment, run poetry config virtualenvs.in-project true first. Install requirements: poetry install.

Then activate the virtual environment with poetry shell.

Symlink files instead of copying files:

jupyter nbextension install --py --symlink --sys-prefix vega
jupyter nbextension enable --py --sys-prefix vega

Run kernel with jupyter notebook. Run the tests with pytest vega.

To rebuild the JavaScript continuously, run yarn watch.

How to make a release

  • Update the JavaScript dependencies by changing package.json (e.g. with ncu).
  • Run yarn.
  • Update the version number in pyproject.toml (with poetry version [VERSION]), package.json, _frontend.py, and __init__.py
  • Rebuild the JavaScript with yarn build.
  • Make sure that everything still works (launch notebook and widgets and try the examples).
  • Add a git tag.
  • git push --tags.
  • Run npm publish to update https://www.npmjs.com/package/jupyter-vega.
  • Then run poetry publish --build to update https://pypi.python.org/pypi/vega.

The Conda feedstock for this package is at https://github.com/conda-forge/vega-feedstock. It should update automatically but we may need to merge a pull request with the updates.

Visual Regression Tests

ipyvega uses the same technical solution as ipywidgets for visual regression testing (i.e. Galata).

Therefore, the instructions provided for ipywidgets visual regression tests apply here.

Currently:

  • ipyvega uses ui-tests/tests/notebooks/vega.ipynb notebook for testing
  • reference images are in the ui-tests/tests/vega.test.ts-snapshots/ directory.

ipyvega's People

Contributors

brettcannon avatar chmp avatar cpsievert avatar cscheid avatar dependabot-preview[bot] avatar dependabot[bot] avatar domoritz avatar ellisonbg avatar jakevdp avatar jdfekete avatar maartenbreddels avatar stonebig avatar tjni avatar toddrme2178 avatar xtianpoli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ipyvega's Issues

Cannot configure vl width

It seems like the config cannot be overridden.

VegaLite({
  "mark": "point",
  "encoding": {
    "y": {"type": "quantitative","field": "Acceleration"},
    "x": {"type": "quantitative","field": "Horsepower"},
    "color": {"type": "nominal","field": "Origin"}
  },
  "config": {
    "cell": {
      "width": 100
    }
  }
}, df)

Implement vincent functionalities

https://github.com/wrobstory/vincent is a python to vega translator. We should have a simple way for users to create a vl visualizations from an ipython notebook.

Something like

import vega
vega.vegalite.create({
{
  "marktype": "point",
  "encoding": {
    "x": {"name": "Acceleration","type": "Q"},
    "y": {"name": "Horsepower","type": "Q"},
  }
}
})

Document how to update JS dependencies

I gather that something reads package.json and then updates index.js/index.js.map. We need the commands that do this to be documented somewhere in the package (perhaps in the README?)

Note vega and vegalite versions at the Python level

I think it would be useful to have the versions of the javascript packages available at some level, e.g.

from vega import VegaLite
print(VegaLite.library_version)

This would facilitate, e.g. raising a warning in Altair if someone has incompatible versions installed.

Print supresses display of vega-lite figure

I've encounterd a problem with pyvega and the Python print function.
The following example works, hence the figure is displayed correctly in the notebook:

import altair
altair.Chart(altair.load_dataset('movies')).encode(x='Production_Budget', y='IMDB_Rating')

If I print anything in this same cell, the figure output disappears:

import altair
print('foo')
altair.Chart(altair.load_dataset('movies')).encode(x='Production_Budget', y='IMDB_Rating')

Is this expected behavior?
Niels

Serve data from server extension

Instead of embedding the data as serialized json in the notebook, the data should be served from the kernel (through a server extension) if that's possible.

@ellisonbg could you tell us whether this is possible and how we roughly would go about this?

Bug: jupyter nbextension install attempts to create directories outside virtualenv

The following installation step

jupyter nbextension install --py vega

fails when run inside virtualenv while attempting to modify global distribution directories

(mcmc)madanh@madanh ~/.virtualenvs/mcmc $ jupyter nbextension install --py vega
Installing /home/madanh/.virtualenvs/mcmc/lib/python3.4/site-packages/vega/static -> jupyter-vega
Traceback (most recent call last):
  File "/home/madanh/.virtualenvs/mcmc/bin/jupyter-nbextension", line 11, in <module>
    sys.exit(main())
  File "/home/madanh/.virtualenvs/mcmc/lib/python3.4/site-packages/jupyter_core/application.py", line 267, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/home/madanh/.virtualenvs/mcmc/lib/python3.4/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/madanh/.virtualenvs/mcmc/lib/python3.4/site-packages/notebook/nbextensions.py", line 900, in start
    super(NBExtensionApp, self).start()
  File "/home/madanh/.virtualenvs/mcmc/lib/python3.4/site-packages/jupyter_core/application.py", line 256, in start
    self.subapp.start()
  File "/home/madanh/.virtualenvs/mcmc/lib/python3.4/site-packages/notebook/nbextensions.py", line 678, in start
    self.install_extensions()
  File "/home/madanh/.virtualenvs/mcmc/lib/python3.4/site-packages/notebook/nbextensions.py", line 657, in install_extensions
    **kwargs
  File "/home/madanh/.virtualenvs/mcmc/lib/python3.4/site-packages/notebook/nbextensions.py", line 225, in install_nbextension_python
    destination=dest, logger=logger
  File "/home/madanh/.virtualenvs/mcmc/lib/python3.4/site-packages/notebook/nbextensions.py", line 126, in install_nbextension
    ensure_dir_exists(nbext)
  File "/home/madanh/.virtualenvs/mcmc/lib/python3.4/site-packages/ipython_genutils/path.py", line 167, in ensure_dir_exists
    os.makedirs(path, mode=mode)
  File "/home/madanh/.virtualenvs/mcmc/lib/python3.4/os.py", line 227, in makedirs
    makedirs(head, mode, exist_ok)
  File "/home/madanh/.virtualenvs/mcmc/lib/python3.4/os.py", line 237, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/usr/local/share/jupyter'

Export VL chart

When you are done with your exploration, you probably want to have a visualization in a separate cell. We should make this process simple.

Installation failure

When performing a conda install, I'm seeing the following. Any thoughts on what might be going wrong?

% conda install vega --channel conda-forge
Fetching package metadata .........
Solving package specifications: ..........

Package plan for installation in environment /Users/cpd/anaconda/envs/defaultenv:

The following NEW packages will be INSTALLED:

    vega: 0.4.2-py27_1 conda-forge

Proceed ([y]/n)?

Linking packages ...
Install Jupyter notebook extensions

Usage

    jupyter nbextension install path/url

This copies a file or a folder into the Jupyter nbextensions directory. If a URL
is given, it will be downloaded. If an archive is given, it will be extracted
into nbextensions. If the requested files are already up to date, no action is
taken unless --overwrite is specified.

Options
-------

Arguments that take values are actually convenience aliases to full
Configurables, whose aliases are listed on the help line. For more information
on full configurables, see '--help-all'.

--quiet
    Minimal output
--symlink
    Create symlink instead of copying files
-s
    Create symlink instead of copying files
--user
    Install to the user's IPython directory
--debug
    Extra output
--overwrite
    Force overwrite of existing files
--prefix=<Unicode> (InstallNBExtensionApp.prefix)
    Default: ''
    Installation prefix
--destination=<Unicode> (InstallNBExtensionApp.destination)
    Default: ''
    Destination for the copy or symlink
--nbextensions=<Unicode> (InstallNBExtensionApp.nbextensions_dir)
    Default: ''
    Full path to nbextensions dir (probably use prefix or user)

To see all available configurables, use `--help-all`

Examples
--------

    jupyter nbextension install /path/to/myextension

[InstallNBExtensionApp] CRITICAL | Bad config encountered during initialization:
[InstallNBExtensionApp] CRITICAL | Unrecognized flag: '--py'
Error: Error: post-link failed for: conda-forge::vega-0.4.2-py27_1

Allow PNG-only mode

Because many python-generated vega plots result in data being embedded into the notebook, notebook size can quickly grow beyond what the browser can handle (see vega/altair#249). Would there be a way to optionally enable PNG-only mode, where on save (or perhaps on render) the live output is replaced by the PNG and removed? I think the JS could be tweaked to do this, but it's not clear to me how this could be easily enabled/disabled from the Python side.

cc/ @ellisonbg

Python 3 support?

I might be doing something wrong here, but I couldn't import vega using Python 3.5.

ImportError                               Traceback (most recent call last)
<ipython-input-1-4f3fd77079e7> in <module>()
----> 1 import vega

/.../lib/python3.5/site-packages/vega/__init__.py in <module>()
----> 1 import tool
      2 import polestar
      3 import voyager
      4 import vegalite

ImportError: No module named 'tool'

Does it currently support only Python 2? Relative imports don't work like that in Python 3, if I remember correctly, you'd need to use import .tool. I can try to work on a Python 3 patch if needed. Any thoughts?

Persistent spec

Store the vl spec from polestar in the notebook file and load it if it is present.

Allow ``VegaLite`` to accept either dict or JSON string?

This came up in our chat โ€“ I think it's a good idea.

Could look something like this:

class Vegalite(object):
    def __init__(self, spec):
        try:
            spec = json.loads(spec)
        except TypeError:
            pass
        if 'data' not in spec:
            raise KeyError('No data provided with spec')
        self.spec = utils.update(spec, DEFAULTS, overwrite=False)

Advantage is more flexible API & ability to out-source dataframe formatting stuff to Pandas; the disadvantage is that in some situations we'd add an extra string->dict->string cycle.

Static PNG render of vegalite

For vegalite visualizations to be seen on sites like nbviewer or github, we need to have static PNG versions of them embedded into the notebooks output. It looks like it is possible to get a base64 encoded PNG from the SVG on the page. Then we would just need to use the output area JS API to put that into the cell. I will try to get an example working that does something like this.

Polestar with notebook configuration

I think the easiest integration of polestar with IPython notebooks is to have a configuration that disables the dataset selector and somehow listens to data changes from the kernel. We should implement a postMessage like interface. regardless, we have to make sure that we keep things contained so it works in jupyter and colab.

"nbconvert"ed interactive Vega plots

Using the nbconvert tool, it would be nice be able to export notebooks containing Vega plots in html (and rst) format. For static plots it just works, but for good reasons all the interactive features of a plot disappear. Links to the d3 and vega javascript libraries need to be added to the HTML template that is passed to nbconvert, and some magic to preserve the plot specification also need to happen in the preprocessing and conversion steps.

Are you able to offer advice on how to write such magic? By calling the private functions _generate_html and _generate_js on the vega.vega.Vega object I can get the relevant snippets of code, but my knowledge about Jupyter and nbconvert is not deep enough to understand how I can make them survive through to the output HTML.

Vega lite integration

Call this way:

from vega import vegalite
vegalite.display(spec)

Then display the visualization.

stacked area chart not rendering correctly via VegaLite.display()

The test case below works fine on http://vega.github.io/vega-editor/?mode=vega-lite but not in a notebook on a freshly built checkout of origin/master. Specifically, the larger of the two areas draws without normalization.

--------------- test case ----------
from vega import VegaLite
data = [
    {'count': 1532, 'date': '2016-07-05', 'label': 'group2'},
    {'count': 4712, 'date': '2016-07-05', 'label': 'group1'},
    {'count': 1736, 'date': '2016-07-06', 'label': 'group2'},
    {'count': 4970, 'date': '2016-07-06', 'label': 'group1'},
    {'count': 1960, 'date': '2016-07-07', 'label': 'group2'},
    {'count': 5248, 'date': '2016-07-07', 'label': 'group1'},
    {'count': 2002, 'date': '2016-07-08', 'label': 'group2'},
    {'count': 5360, 'date': '2016-07-08', 'label': 'group1'},
    {'count': 1969, 'date': '2016-07-09', 'label': 'group2'},
    {'count': 4548, 'date': '2016-07-09', 'label': 'group1'},
    {'count': 1931, 'date': '2016-07-10', 'label': 'group2'},
    {'count': 4631, 'date': '2016-07-10', 'label': 'group1'},
    {'count': 2316, 'date': '2016-07-11', 'label': 'group2'},
    {'count': 5817, 'date': '2016-07-11', 'label': 'group1'}]
graphic = {
    "mark": "area",
    "encoding": {
        "y": {"aggregate": "sum", "field": "count", "type": "quantitative"},
        "x": {"field": "date", "type": "temporal"},
    "color": {"field":"label", "type":"nominal", "scale": {"range": ["#e6aa06", "#1f77b4"]}}
    },
    "config": {"mark": {"stacked": "normalize"}}
}
VegaLite(graphic, data).display()
-----------------

Update the README

I haven't looked, but after #32 is merged, we should probably update the README:

  • Provide links to the demo notebooks on GitHub and nbviewer.
  • Start to promote that it works on GitHub and nbviewer.
  • Audit the installation and dev instructions.
  • Update screenshots for vega+vegalite.

Rename to ipython-vega

Consistent repo name, package name, folder names.

Repo name should be ipython-vega, all other names should be vega.

vega.vegalite
vega.polestar
vega.voyager

Refactor to use Jupyter's custom MIME type based rendering

In JupyterLab we are moving to rendering custom outputs using custom MIME types and functions that get registered to rendering those MIME types. I have that working well for JupyterLab, and am working on a version for the classic notebook. This will dramatically simplify the code base and lead to only the actual vega/vegalite JSON spec being stored in the notebook (rather than a bunch of JS code). Will also enable us to get these things working on nbviewer.

This will result in a complete refactor of basically everyfile in this repo...can I hold the lock for a few days?

@domoritz @jakevdp

Release script

  • change the version numbers in the package.json, index.py
  • tag a version
  • push to pypi and conda

Enable copying PNG more easily

I end up sharing a lot of rendered graphs on Slack which has the handy ability to upload images from the clipboard. With matplotlib visualizations there's a vanilla rendered that I can right-click and say 'copy image' and then just paste it into slack which works great. However with vega I have to add the intermediate step of saving the graph as a png first which really slows things down.

I was wondering if it might be possible to add another option beside "Export as PNG" like "Copy as PNG" that would do the same operation but plop the results in the clipboard?

use case: vincent replacement, using python

Per Dominik's request, I'm attaching my use case, and hope to work with the authors to make this use case work.

My workflow is simulation -> JSON -> pandas -> ? -> vega. Currently the ? is vincent, but vincent is not Vega-2.0-compliant and the author, who did a great job on it, isn't currently maintaining it. My simulation generates a lot of data; pandas is exactly the right tool (in python) for slicing and dicing that data; my hope is this package will fill in the ? where vincent is currently kind-of doing the job.

I am less interested in the ipython case and more interested in just using plain python as a batch script, but I think I will try out ipython since that is a design goal for y'all. I will also have suggestions on how to write good docs/examples that make it easy for others to use your tool. (This is a place where vincent could improve. What I will suggest is that for a given plot type, you explicitly say what it expects as an argument in terms of a pandas DataFrame, for instance, "GroupedBar: expects a pandas DataFrame where the rows are a set of measurements that are grouped together (in the GroupedBar 'group'), and each column is one individual measurement").

It is also my hope that I can generate both web plots (via D3, in the web browser) as well as print plots (SVG, PDF, etc.).

Thanks for writing this!

VegaLite erroneously requires data to be specified at the top level

The following spec should produce a valid plot, and does when converted to JSON and fed to vega-lite directly:

spec = {'layers': [{'config': {'mark': {'color': '#1f77b4'}},
   'data': {'values': [{'x': 0, 'y': 0}, {'x': 1, 'y': 1}]},
   'encoding': {'x': {'field': 'x', 'type': 'quantitative'},
    'y': {'field': 'y', 'type': 'quantitative'}},
   'mark': 'line'},
  {'config': {'mark': {'color': '#ff7f0e'}},
   'data': {'values': [{'x': 0, 'y': 1}, {'x': 1, 'y': 0}]},
   'encoding': {'x': {'field': 'x', 'type': 'quantitative'},
    'y': {'field': 'y', 'type': 'quantitative'}},
   'mark': 'point'}]}

When used with this package, though, it results in an error:

from vega import VegaLite
VegaLite(spec)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-9af8065cc04d> in <module>()
      1 from vega import VegaLite
----> 2 VegaLite(spec)

/Users/jakevdp/anaconda/lib/python3.5/site-packages/vega/base.py in __init__(self, spec, data)
     21         """Initialize the visualization object."""
     22         spec = utils.nested_update(copy.deepcopy(self.DEFAULTS), spec)
---> 23         self.spec = self._prepare_spec(spec, data)
     24 
     25     def _prepare_spec(self, spec, data):

/Users/jakevdp/anaconda/lib/python3.5/site-packages/vega/vegalite.py in _prepare_spec(self, spec, data)
     22 
     23     def _prepare_spec(self, spec, data):
---> 24         return prepare_spec(spec, data)
     25 
     26 

/Users/jakevdp/anaconda/lib/python3.5/site-packages/vega/utils.py in prepare_spec(spec, data)
     91         # Data is either passed in spec or error
     92         if 'data' not in spec:
---> 93             raise ValueError('No data provided')
     94     else:
     95         # As a last resort try to pass the data to a DataFrame and use it

ValueError: No data provided

Currently the package assumes that data must appear at the top level; with the new Layer options, this is no longer strictly required.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.