Search code examples
pythonaltair

Value error - to use altair_viewer renderer, you must install the altair viewer


The following basic code throws the error in vscode.


import pandas as pd
import altair as alt

alt.renderers.enable("altair_viewer")


food_cost_df = pd.read_csv("data_annotation/data_analyses/data/2DEY_kitchen_costs.csv")

print(food_cost_df)

chart = alt.Chart(food_cost_df, height= 500, width=500).mark_bar().encode(
    alt.X("ITEM"),
    alt.Y("COST"))
chart.show()

The conda env, named 'data_ann_env', was built and maintained locally in the vscode project folder and is activated in both the terminal and via the vscode Selected Interpreter. Python version 3.11.7, conda version 23.10.0, altair family (obtained from the conda list):

  • altair 5.0.1
  • altair_data_server 0.4.1
  • altair_viewer 0.4.0

What conflict am I running into here or ...?

import urllib.request, json 
with urllib.request.urlopen("https://pypi.org/pypi/altair_viewer/json") as url:
    print(json.loads(url.read().decode())['info']['version'])
with urllib.request.urlopen("https://pypi.org/pypi/altair/json") as url:
    print(json.loads(url.read().decode())['info']['version'])

which pointed out I could update to altair 5.4.0. which I did and still the error persists.

I tested a few things:

  • I built a pip venv and manually installed the libraries which threw the same error - prompting me to install altair_viewer
  • I built a new conda enviroment with python 3.9.18 and manually installed libraries and still the error persisted
  • lastly I activated a library from a different project and as expect the script worked without any errors.

Solution

  • altair_viewer is only compatible with altair 4. If you are using it for offline charts, this functionality is in vl-convert. If you are using it to show charts from a terminal, you can follow this issue https://github.com/altair-viz/altair/issues/2866. If there is another use case, let us know by opening a new issue