Joubert Lucas
Joubert Lucas

Reputation: 163

PyQt5 QWebview Refresh Issue

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

Upvotes: 0

Views: 2902

Answers (1)

eyllanesc
eyllanesc

Reputation: 243993

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

Upvotes: 1

Related Questions