0

I use a QWebEngineView and set it's HTML. When I display it, the HTML is always smaller than the total area I gave the QWebEngineView. Right clicking the page and clicking "Reload" always fixes the problem, but I don't want end users to click "Reload" every time. I've already searched online for a way to reload, and found:

my_qwebengineview.page().action(QWebEnginePage.Reload).trigger()

This isn't resizing it the same way a right click does, though.

If it helps with reading the code, it's displaying a Plotly plot as an HTML page (long story).

I create the widget it sits on with the code below:

grid_layout = QGridLayout()
logo = QLabel()
logo_pixmap = QPixmap('logo.jpg')
logo.setPixmap(logo_pixmap)
grid_layout.addWidget(logo, 0, 0, 2, 6)
grid_layout.addItem(QSpacerItem(500, 50, QSizePolicy.Minimum, QSizePolicy.Expanding), 0, 6)

graph_label = QLabel('Graph')
graph_names_combobox = QComboBox()

grid_layout.addWidget(graph_label, 0, 7, 1, 1)
grid_layout.addWidget(self.graph_names_combobox, 0, 8, 1, 4)

self.graph_widget = QStackedWidget()
grid_layout.addWidget(self.graph_widget, 2, 1, 10, 12)

self.graph_window_widget = QWidget()
self.graph_window_widget.setLayout(grid_layout)

Later on I use:

temp_webpage = QWebEngineView()
temp_webpage.setHTML(*graph html goes here*)
self.graph_widget.addWidget(temp_webpage)

# In the course of the full program, a lot of graphs are added, so reloading
# them all here.
for child in self.graph_widget.children()
    if type(child) == QWebEngineView:
        child.page().action(QWebEnginePage.Reload).trigger()

I've tried using the reload code right after I set it the stacked widget to the selected graph, but like I said, it's having the same result.

Is there a way to make the page do the same thing as right click -> reload?

Edit

In response to a comment, the code I use to generate the html comes from this (data is a pandas DataFrame that I load somewhere else from SQL, but I've included a toy DataFrame here):

path = QDir.current().filePath('plotly-latest.min.js')
local = QUrl.fromLocalFile(path).toString()

data_frame = DataFrame(index=(range(2)),columns=('Date', 'SKUs', 'Lines', 'Orders', 'Eaches'))

data_frame.loc[0:1,'Date'] = (datetime.date(1999, 1, 1), datetime.date(1999, 1, 2))
data_frame.loc[0:1,'SKUs'] = (12, 13)
data_frame.loc[0:1,'Lines'] = (14, 15)
data_frame.loc[0:1,'Orders'] = (16, 17)
data_frame.loc[0:1,'Eaches'] = (18, 19)

trace_lines = Bar(x=data_frame['Date'], y=data_frame['Lines'], name='Lines', visible=True)
            trace_orders = Bar(x=data_frame['Date'], y=data_frame['Orders'], name='Orders', visible=False)
            trace_eaches = Bar(x=data_frame['Date'], y=data_frame['Eaches'], name='Eaches', visible=False)
            trace_SKUs = Bar(x=data_frame['Date'], y=data_frame['SKUs'], name='SKUs', visible=False)

data = [trace_lines, trace_orders, trace_eaches, trace_SKUs]

lines_title = "%s Lines" % name
orders_title = "%s Orders" % name
eaches_title = "%s Eaches" % name
skus_title = "%s SKUs" % name

update_menus = list([dict(active=0,
                                  buttons=list([dict(label='Lines',
                                                     method='update',
                                                     args=[{'visible': [True, False, False, False]},
                                                           {'title': lines_title}]),
                                                dict(label='Orders',
                                                     method='update',
                                                     args=[{'visible': [False, True, False, False]},
                                                           {'title': orders_title}]),
                                                dict(label='Eaches',
                                                     method='update',
                                                     args=[{'visible': [False, False, True, False]},
                                                           {'title': eaches_title}]),
                                                dict(label='SKUs',
                                                     method='update',
                                                     args=[{'visible': [False, False, False, True]},
                                                           {'title': skus_title}])]))])

layout = dict(title=lines_title, showlegend=False, updatemenus=update_menus, xaxis=dict(
            rangeselector=dict(buttons=list([dict(count=1, label='1m', step='month', stepmode='backward'),
                                             dict(count=6, label='6m', step='month', stepmode='backward'),
                                             dict(step='all')])),
            rangeslider=dict(),
            type='date'
        ))

fig = dict(data=data, layout=layout)

raw_html = '<html><head><meta charset="utf-8" /><script src="{}"></script></head>'.format(local)
raw_html += '<body>'
raw_html += plot(fig, include_plotlyjs=False, output_type='div')
raw_html += '</body></html>'

return raw_html
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Joubert Lucas
  • 153
  • 2
  • 12
  • You could provide a code that reproduces your error (with the necessary HTML since this is the main element that generates the error), many I have used QWebEngineView and I have not had that problem, maybe it is a particular problem and I do not want to create the code from a start to observe the error, and perhaps never observe it. – eyllanesc Nov 16 '17 at 18:34
  • It's hard to explain, but I can't really share the HTML. It loads plotly-min.js from a local source so that I don't need to be connected to the internet to use it, so it would need to be modified before you ran it. I can give you the code that generates the HTML, if that would help. – Joubert Lucas Nov 16 '17 at 18:48
  • Of course, maybe for professional reasons I can not share that particular project, but if you share another project that reproduces the error, it would help me to test it and be able to suggest solutions. – eyllanesc Nov 16 '17 at 18:52
  • I've added the code that I use to make the HTML. You'll need a pandas DataFrame with some dummy variables (including one column of dates) and to download plotly-min.js and put it in the same folder as the code. Thank you for looking at it! – Joubert Lucas Nov 16 '17 at 18:56
  • Make our lives easy, share the dataframe, I do not want to waste time putting together a pandas that fits your example. – eyllanesc Nov 16 '17 at 18:56
  • I have worked many times with plotly and I know how to do it but what I do not want is to reconstruct your project and in the end not being able to reproduce your error, please provide the dataframa with appropriate fictitious data. – eyllanesc Nov 16 '17 at 18:58
  • Sorry, I have to run to a meeting, will post the dataframe later – Joubert Lucas Nov 16 '17 at 18:58
  • Could you please explain what does the following mean: *This isn't resizing it the same way a right click does, though*?, I see that it looks the same as when you use reload. Also when you need to reload the page? I used the method you mentioned and also the reload() function – eyllanesc Nov 16 '17 at 20:02
  • When it first shows up, the plot is *very* small (as in you can only see the very top of it). Another version of this had the plot come in taking up approximately 1/2 the area I left for it in the widget. I have to right click and reload to get it to resize and fill up the space I gave the widget. – Joubert Lucas Nov 16 '17 at 20:15
  • I noticed that when initially loading the plot is small compared to the space provided by the widget, then I used my_qwebengineview.page().action(QWebEnginePage.Reload).trigger () and recharges the same as when I right click and I press reload (also QWebEngineView has a [`reload()`](http://doc.qt.io/qt-5/qwebengineview.html#reload) method), I do not notice the difference visually between trigger() or reload() with the reload that is done with the right click, you could post images pointing out those differences. – eyllanesc Nov 16 '17 at 20:22
  • Is exactly like you saw it, but for some reason my reloads aren't working. I'm updating the code to show where I'm doing the reload, as I've tried both methods (reload() and the trigger()) and neither is working. – Joubert Lucas Nov 16 '17 at 20:45
  • To do a proof of concept I have recharged a second time when it is fully charged and works properly: https://github.com/eyllanesc/stackoverflow/blob/master/47336766 and I get the following: https://imgur.com/a/EcJ6K – eyllanesc Nov 16 '17 at 20:51
  • That works! But I think what I was missing was the temp_webpage.loadFinished.connect(self.onLoadFinished) (I've never used that before). If you can put that in an answer, I'll mark it as answered! – Joubert Lucas Nov 16 '17 at 21:00

1 Answers1

1

A possible solution is to reload the page for the second time as shown below:

    self.isFirst = True 
    temp_webpage.loadFinished.connect(self.onLoadFinished)

def onLoadFinished(self, ok):
    if self.isFirst:
        # self.sender().page().action(QWebEnginePage.Reload).trigger()
        self.sender().reload()
    self.isFirst = False
eyllanesc
  • 235,170
  • 19
  • 170
  • 241