python logo


Tag: web

pyqt5 webview

QWebview inside a browser PyQt5 Webkit (QWebview) inside a browser

PyQt5 comes with a webkit webbrowser. Webkit is an open source web browser rendering engine that is used by Apple Safari and others. It was used in the older versions of Google Chrome, they have switched to the Blink rendering engine.

Related course:
Create GUI Apps with PyQt5

QWebView
The widget is called QWebView and webpages (HTML content) can be shown through this widget, local or live from the internet.

Methods
The QWebView class comes with a lot of methods including:


  • back (self)

  • forward (self)

  • load (self, QUrl url)

  • reload (self)


#!/usr/bin/python

import PyQt5
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWebKitWidgets import QWebView , QWebPage
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtNetwork import *
import sys
from optparse import OptionParser

class MyBrowser(QWebPage):
''' Settings for the browser.'''

def userAgentForUrl(self, url):
''' Returns a User Agent that will be seen by the website. '''
return "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"

class Browser(QWebView):
def __init__(self):
# QWebView
self.view = QWebView.__init__(self)
#self.view.setPage(MyBrowser())
self.setWindowTitle('Loading...')
self.titleChanged.connect(self.adjustTitle)
#super(Browser).connect(self.ui.webView,QtCore.SIGNAL("titleChanged (const QString&)"), self.adjustTitle)

def load(self,url):
self.setUrl(QUrl(url))

def adjustTitle(self):
self.setWindowTitle(self.title())

def disableJS(self):
settings = QWebSettings.globalSettings()
settings.setAttribute(QWebSettings.JavascriptEnabled, False)

app = QApplication(sys.argv)
view = Browser()
view.showMaximized()
view.load("https://pythonspot.com")
app.exec_()

Related course:
Create GUI Apps with PyQt5

python web development

Flask with static html files

You can use the Flask framework and use static files together.

Flask will give you URL routing, many features and all the Python benefits.

You may want an application that is partly dynamic and partly static. Or you may simply want to browse with URL routing. In this article we will teach you how to load static HTML files with Flask.

Related course
Python Flask: Make Web Apps with Python

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/<string:page_name>/')
def render_static(page_name):
return render_template('%s.html' % page_name)

if __name__ == '__main__':
app.run()

This application initializes a Flask app with the method:


app.run()

The app creates an URL route for any possible page and links that to static html files with:


@app.route('/<string:page_name>/')
def render_static(page_name):
return render_template('%s.html' % page_name)

</string:page_name>

Create a directory /templates/ and add the file hello.html:



<title>Hello World Template</title>

Hello World


Start the server with:

$ python app.py
* Running on http://127.0.0.1:5000/

Then any .html file is accesible using URL routing.

For example the static file hello.html can be accessed using http://127.0.0.1:5000/hello. You can store any css file in the /static/ directory.

Download Flask Examples

python json

python browser