I want to select the data points using box/lasso selct from a scatter mapbox as below. My code is highlightng selected points. But they are not updated to selectedpoints attribute of fig.data[0]. When i want to print selcted points or pass them to a function, selected points are still none as initially declared.
import sys
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QDialog
import plotly.graph_objects as go
import pandas as pd
import numpy as np
import plotly.express as px
from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QDialog
class DashboardScreen(QDialog):
def init(self):
super(DashboardScreen, self).init()
data = {'Fid': ['A', 'B', 'C'],
'Project': [1,2,3],
'Date': [23, 24, 25],
'Latitude': [10, 20, 30],
'Longitude': [70, 75, 80]}
df = pd.DataFrame(data)
fig = go.Figure(go.Scattermapbox(
lat=df['Latitude'],
lon=df['Longitude'],
hovertext=df['Project'],
mode='markers',
marker={'size': 10, 'color': 'blue'},
selected=dict(marker={'color': 'red'}),
selectedpoints=[]
))
fig.update_layout(
mapbox_style="white-bg"
)
fig.update_layout(mapbox_layers=[
{
"below": 'traces',
"sourcetype": "raster",
"sourceattribution": "United States Geological Survey",
"source": [
"https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
# "http://localhost:7000/offline_tiles/{z}/{x}/{y}.png"
]
}
],
margin={"r":0,"t":0,"l":0,"b":0}
)
self.setLayout(QVBoxLayout())
# Create the web view
self.webView = QWebEngineView()
self.webView.setHtml(fig.to_html(include_plotlyjs='cdn'))
self.layout().addWidget(self.webView)
# Create the push button
self.button = QPushButton("Pass Selected Data")
self.button.clicked.connect(lambda: self.passSelectedData(fig))
self.layout().addWidget(self.button)
def passSelectedData(self, fig):
print(fig.data[0])
print(fig.data[0].selectedpoints)
if fig.data[0].selectedpoints:
selected_indices = fig.data[0].selectedpoints
selected_data = {
'lat': [fig.data[0].lat[i] for i in selected_indices],
'lon': [fig.data[0].lon[i] for i in selected_indices]
}
print(selected_data)
app = QApplication(sys.argv)
welcome = DashboardScreen()
welcome.show()
if name == ‘main’:
try:
sys.exit(app.exec_())
except:
logging.info(“Exiting!!”)
print(“Exiting!!”)