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

Choropleth_mapbox and FigureWidget - 'mapbox._derived' Value Error #2570

Open
macadology opened this issue Jun 15, 2020 · 21 comments
Open

Choropleth_mapbox and FigureWidget - 'mapbox._derived' Value Error #2570

macadology opened this issue Jun 15, 2020 · 21 comments
Labels
bug something broken P3 not needed for current cycle

Comments

@macadology
Copy link

When plotting a choropleth_mapbox using plotly express and containing it within a figure widget, I am encountering a ValueError when I drag the plot around. The error does not affect the interactivity of the plot, but it does create a huge error log in the output. I thought about trying go.add_choroplethmapbox(), but realized that the arguments are different from the plotly express version.

I have included a sample code below.

Plotly [offline]
jupyter core : 4.6.3
jupyter-notebook : 6.0.3
ipywidgets : 7.5.1
traitlets : 4.3.3
plotly : 4.8.1

import plotly.express as px
import plotly.graph_objs as go

df = px.data.election()
geojson = px.data.election_geojson()

fig = px.choropleth_mapbox(df, geojson=geojson, color="Bergeron",
                           locations="district", featureidkey="properties.district",
                           center={"lat": 45.5517, "lon": -73.7073},
                           mapbox_style="carto-positron", zoom=9)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
figwid = go.FigureWidget(fig)
display(figwid)
ValueError: 
Invalid property path 'mapbox._derived' for layout
@macadology
Copy link
Author

macadology commented Jun 16, 2020

Added a temporary hack in basedatatypes.py to skip the exception specifically for mapbox._derived and it seems to work for now.

if not BaseFigure._is_key_path_compatible(key_path_str, self.layout):
                if key_path_str == "mapbox._derived":
                    return
                raise ValueError(
                    """
Invalid property path '{key_path_str}' for layout
""".format(
                        key_path_str=key_path_str
                    )
                )

@n1rvanana
Copy link

n1rvanana commented Jul 27, 2020

Hello!
Have you solved the issue?
I'm a newbee in coding, would you answer, is your temporary solution should be placed in a specific place of basedatatypes.py? I've inserted it below and got the same error.

@macadology
Copy link
Author

When editing basedatatypes.py, look for the code snippet

if not BaseFigure._is_key_path_compatible(key_path_str, self.layout):
     raise ValueError(...

Before the error is raised, create a conditional check that skips that error if key_path_str == "mapbox._derived" as I have done above. Note that this is obviously a hack that will no longer work when you install a new version of plotly.

@cjroberts
Copy link

Hello, just to note that I also have the same issue with a scatter mapbox plot. Using add_scattermapbox does not seem to help. Here is a minimal reproducer:

import plotly.graph_objects as go
import numpy as np
lons = np.random.random(1000) * 360.0
lats = np.random.random(1000) * 180.0 - 90.0
z = np.random.random(1000) * 50.0
fig = go.FigureWidget()
fig.add_scattermapbox(lon=lons, lat=lats, marker={'color': z})
fig.update_layout({'mapbox': {'style': 'stamen-terrain'}})

@sanskarvijpuria
Copy link

I have the same issue.
The workaround by @macadology seems to be temporary.

@Jmete
Copy link

Jmete commented Nov 22, 2020

I have the same issue too.
Is there anyway to suppress the warning at least without altering the .py file mentioned above? Is there some callback we can listen for that is triggering it?

@DougRzz
Copy link

DougRzz commented Dec 4, 2020

Just run this to do @macadology 's edit.

basedatatypesPath = os.path.join(os.path.dirname(os.__file__),'site-packages','plotly','basedatatypes.py')

# read basedatatypes.py
with open(basedatatypesPath, 'r') as f:
    lines = f.read()

find = 'if not BaseFigure._is_key_path_compatible(key_path_str, self.layout):'

replace = """if not BaseFigure._is_key_path_compatible(key_path_str, self.layout):
                if key_path_str == "mapbox._derived":
                    return"""

# add new text
lines = lines.replace(find,replace)

# overwrite old 'basedatatypes.py'
with open(basedatatypesPath, 'w') as f:   
    f.write(lines)

@pjireland
Copy link

I'm also having the same issue. Are there any updates on when this will be addressed?

@briochh
Copy link

briochh commented Jun 29, 2021

Just bumping this. Seeing the same behavior for @cjroberts min example.
plotly 5.1.0
ipywidgets 7.6.3
notebook 6.4.0
Is this a bug or user error? If user error, does anyone have any information on what we are missing?

@nicolaskruchten
Copy link
Contributor

No one on our development team has ever been able to reproduce this behaviour starting from a clean environment, so we don't know what to recommend. If someone can provide reproducible instructions for replicating this behaviour, I can investigate further. Some folks have reportedly achieved success by un/re-installing plotly and rebooting Jupyter.

@macadology
Copy link
Author

Yay, glad to hear back from the development team.

I tried it again from a clean environment too and I encountered the same error. See below for my setup using conda and pip. For convenience, I didn't specify a particular version but instead rely on pip to resolve the latest version.

conda create -n myenv python=3.9
conda activate myenv
pip install plotly
pip install pandas
pip install ipython
pip install ipywidgets
pip install jupyter
pip install notebook

When running the min code in a jupyter notebook environment, the error message appears whenever I interact with the plot. Below is a screenshot of the actual error. Is it possible the development team tested the code outside of the notebook environment? Let me know if I should create a mybinder notebook to reproduce the error.

image
...more error messages...
image

@nicolaskruchten
Copy link
Contributor

Thanks for this. Yes, a replicable environment like Binder would be a great help :)

@macadology
Copy link
Author

macadology commented Jun 29, 2021

Here you go.
https://github.com/macadology/plotly_binder
https://mybinder.org/v2/gh/macadology/plotly_binder/master?filepath=%2Fmin_code.ipynb

Once the plot is generated, moving it or interacting with it will cause error messages to appear in the output cell below the plot.

@giswqs
Copy link

giswqs commented Dec 26, 2021

I am having the same issue with Python 3.8, notebook 6.1.4, plotly 5.5.0.

plotly

@janhurst
Copy link

also seeing this with python 3.9 plotly 5.10 making the scattarmapbox unusable for us.

where does the mapbox._derived property come from?
image

@giswqs
Copy link

giswqs commented Sep 28, 2022

Run the following function as a temporary fix.

def fix_widget_error():
    """
    Fix FigureWidget - 'mapbox._derived' Value Error.
    Adopted from: https://github.com/plotly/plotly.py/issues/2570#issuecomment-738735816
    """
    import shutil
    import pkg_resources

    pkg_dir = os.path.dirname(pkg_resources.resource_filename("plotly", "plotly.py"))

    basedatatypesPath = os.path.join(pkg_dir, "basedatatypes.py")

    backup_file = basedatatypesPath.replace(".py", "_bk.py")
    shutil.copyfile(basedatatypesPath, backup_file)

    # read basedatatypes.py
    with open(basedatatypesPath, "r") as f:
        lines = f.read()

    find = "if not BaseFigure._is_key_path_compatible(key_path_str, self.layout):"

    replace = """if not BaseFigure._is_key_path_compatible(key_path_str, self.layout):
                if key_path_str == "mapbox._derived":
                    return"""

    # add new text
    lines = lines.replace(find, replace)

    # overwrite old 'basedatatypes.py'
    with open(basedatatypesPath, "w") as f:
        f.write(lines)

@janhurst
Copy link

@giswqs unfortunately I cannot... the environment I am using does not permit writing to site-packages

@giswqs
Copy link

giswqs commented Sep 28, 2022

@janhurst Sorry to hear that! This issue has been open for 2.5 years now. I have been waiting for an official fix. Not sure when it will happen.

@thadd3us
Copy link

thadd3us commented Feb 23, 2024

I am seeing the same problem with:

  • plotly-5.19.0-pyhd8ed1ab_0
  • notebook-7.1.0-pyhd8ed1ab_0
  • python-3.11.8-hdf0ec26_0_cpython

@gvwilson gvwilson self-assigned this Jul 8, 2024
@pmallas
Copy link

pmallas commented Jul 26, 2024

This still seems to a problem. It is now July, 2024. Is the above workaround still the recommended way to deal with this problem?
Thank you.

@gvwilson gvwilson removed their assignment Aug 2, 2024
@gvwilson gvwilson added P3 not needed for current cycle bug something broken labels Aug 12, 2024
@gvwilson
Copy link
Contributor

@archmoj will your recent work with MapLibre address this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P3 not needed for current cycle
Projects
None yet
Development

No branches or pull requests