Skip to content

Instantly share code, notes, and snippets.

@sergeyfarin
Last active March 6, 2024 07:24
Show Gist options
  • Save sergeyfarin/c689fd0171f95865055fad857579bc94 to your computer and use it in GitHub Desktop.
Save sergeyfarin/c689fd0171f95865055fad857579bc94 to your computer and use it in GitHub Desktop.
Example of using Python, PyQt5 and QtWebEngineView to open local html file (+js, css). test.html and test.js should be in html subfolder
# coding: utf-8
import sys
import os
# import site
# site.addsitedir('/usr/local/lib/python2.7/site-packages')
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
app = QtWidgets.QApplication(sys.argv)
view = QtWebEngineWidgets.QWebEngineView()
# view.settings().setAttribute(QtWebEng.QWebSettings.LocalContentCanAccessRemoteUrls, True)
# f = open('html/test.html', 'r')
#
# html = f.read()
# f.close()
# print(os.path.abspath(__file__))
# path = os.path.abspath(__file__)
# print()
# view.setHtml(html)
# view.setHtml(html, baseUrl=QtCore.QUrl().fromLocalFile(os.path.split(os.path.abspath(__file__))))
# view.setUrl()
# view.set
# view.load(QtCore.QUrl('http://'))
view.load(QtCore.QUrl().fromLocalFile(
os.path.split(os.path.abspath(__file__))[0]+r'\html\test.html'
))
view.show()
sys.exit(app.exec_())
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!--<script type="text/javascript" src="plotly-latest.min.js"></script>-->
<script type="text/javascript" src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<h1>Hello, world!</h1>
<div id="test"></div>
wevwev
<script src="test.js"></script>
<div id="tester" style="width:600px;height:250px;"></div>
<script>
TESTER = document.getElementById('tester');
Plotly.plot( TESTER, [{
x: [1, 2, 3, 4, 5],
y: [1, 2, 4, 8, 16] }], {
margin: { t: 0 } } );
</script>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
/**
* Created by Sergey.Farin on 07/06/2016.
*/
document.getElementById("test").innerHTML="someContent";
@jocon15
Copy link

jocon15 commented Jun 20, 2023

Thanks, this works beautifully for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment