javascript - Get js console.log as python variable in QWebView pyqt -
i'm trying build application creates html + js
code python , loads qwebview.
the final html + js
code following (raw code available here):
<head><meta charset="utf-8" /><script src="https://cdn.plot.ly/plotly-latest.min.js"></script></head><div id="mydiv" style="height: 100%; width: 100%;" class="plotly-graph-div"></div><script type="text/javascript">window.plotlyenv=window.plotlyenv || {};window.plotlyenv.base_url="https://plot.ly";plotly.newplot("mydiv", [{"type": "scatter", "x": [6.81, 6.78, 6.49, 6.72, 6.88, 7.68, 7.69, 7.38, 6.76, 6.84, 6.91, 6.74, 6.77, 6.73, 6.68, 6.94, 6.72], "y": [1046.67, 1315.0, 1418.0, 997.33, 2972.3, 9700.0, 6726.0, 6002.5, 2096.0, 2470.0, 867.0, 2201.7, 1685.6, 2416.7, 1618.3, 2410.0, 2962.0], "mode": "markers", "ids": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]}], {"showlegend": true}, {"linktext": "export plot.ly", "showlink": false})</script> <script> var plotly_div = document.getelementbyid('mydiv') var plotly_data = plotly_div.data plotly_div.on('plotly_selected', function(data){ featureid = plotly_data[0].ids[data.points[0].pointnumber] featureids = [] data.points.foreach(function(pt) { featureids.push(parseint(pt.id)) }) console.log(featureids) }) </script>
this code creates plot plotly
python api. saving plot in local file (e.g. /home/matteo/plot.html
) i'm able load in qwebview following piece of code:
from pyqt5.qtwebkit import qwebsettings pyqt5.qtwebkitwidgets import * pyqt5.qtcore import qurl import json plot_view = qwebview() plot_view.page().setnetworkaccessmanager(qgsnetworkaccessmanager.instance()) plot_view_settings = plot_view.settings() plot_view_settings.setattribute(qwebsettings.webglenabled, true) plot_view_settings.setattribute(qwebsettings.developerextrasenabled, true) plot_view_settings.setattribute(qwebsettings.accelerated2dcanvasenabled, true) plot_path = '/home/matteo/plot.html' plot_url = qurl.fromlocalfile(plot_path) plot_view.load(plot_url) plot_view.show()
the javascript function of above return ids of selected points of plot in javascript console when using lasso selection
tool of plot.
checking inspect
tool, works fine in qwebview: each time element within selection, in console.log can see corresponding id of point.
this static image taken jsfiddle (lasso selection, point selected , console.log output):
the running code available here, enable debugging console , select point lasso selection tool.
what i'd achieve take output of console (so ids of selected points), , re-inject them application in order have usable python object (list, dictionary, whatever) every time make selection.
is there way link javascript console of qwebpage in order obtain python objects?
to console.log()
output, necessary overwrite method javascriptconsolemessage().
plus: have added 2 ways access list of points, first through attribute obj, , second through signal.
class _loggedpage(qwebpage): obj = [] # synchronous newdata = pyqtsignal(list) #asynchronous def javascriptconsolemessage(self, msg, line, source): l = msg.split(",") self.obj = l self.newdata.emit(l) print ('js: %s line %d: %s' % (source, line, msg))
complete example:
class _loggedpage(qwebpage): obj = [] # synchronous newdata = pyqtsignal(list) #asynchronous def javascriptconsolemessage(self, msg, line, source): l = msg.split(",") self.obj = l self.newdata.emit(l) print ('js: %s line %d: %s' % (source, line, msg)) def onnewdata(data): print("asynchronous", data) app = qapplication(sys.argv) plot_view = qwebview() page = _loggedpage() page.newdata.connect(onnewdata) plot_view.setpage(page) plot_path = '/home/qhipa/plot.html' plot_url = qurl.fromlocalfile(plot_path) plot_view.load(plot_url) plot_view_settings = plot_view.settings() plot_view_settings.setattribute(qwebsettings.webglenabled, true) plot_view_settings.setattribute(qwebsettings.developerextrasenabled, true) plot_view_settings.setattribute(qwebsettings.accelerated2dcanvasenabled, true) # synchronous request simulation timer = qtimer(plot_view) timer.timeout.connect(lambda: print("synchronous", page.obj)) timer.start(1000) plot_view.show() sys.exit(app.exec_())
Comments
Post a Comment