NeelJ 83
NeelJ 83

Reputation: 149

Displaying Websites with Python

I am making an os in python, but I need a web browser. Currently I am using the os.startfile method to launch chrome, but I want another way. I want a program that a user can enter a webpage and displaying the web page without using chrome, firefox, safari etc.

Here is the basic framework I have:

from tkinter import *
import webbrowser as wb
window = Tk()
window.configure(bg="Dark Red")
window.geometry("1000x1000")
window.title("Hyper Web Browser")
window.iconbitmap("icon.ico")
''' Defined Functions'''


def submit_url():
  wb.open_new_tab(Address_Bar.get())
  file2write = open("History.txt", "a")
  file2write.write(["\n", Address_Bar.get()])
  return submit_url
  '''Objects'''
  Address_Bar = Entry(
    bg="White",
    bd=0,
    font=("Comic", 25),
    width=100
  )
  Tab_1 = Label(
    bg="Red",
    bd=0,
    width=20,
    height=3
  )
  Return = Button(
    command=submit_url()
  )
  Address_Bar.place(x=20, y=60)
  Tab_1.place(x=0, y=0)
  Return.pack()

window.mainloop()

However, this program launches the web page into the user's default browser. Hence, I want to display the web page without using any other web browsers.

Upvotes: 4

Views: 8155

Answers (2)

Devco
Devco

Reputation: 1

Something like this may be what you are looking for. This is a simple demo script that allows web browsing, without opening it in a default web browser such as chrome. (This is essentially a DIY web browser script) I hope this helps!

from functools import cached_property
import sys
import keyboard
from prompt_toolkit.key_binding import KeyBindings
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtCore import pyqtSlot
bindings = KeyBindings()
    
class WebView(QtWebEngineWidgets.QWebEngineView):
    def createWindow(self, type_):
        if not isinstance(self.window(), Browser):
            return

        if type_ == QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
            return self.window().tab_widget.create_tab()


class TabWidget(QtWidgets.QTabWidget):
    def create_tab(self):
        view = WebView()

        index = self.addTab(view, "...")
        self.setTabIcon(index, view.icon())
        view.titleChanged.connect(
            lambda title, view=view: self.update_title(view, title)
        )
        view.iconChanged.connect(lambda icon, view=view: self.update_icon(view, icon))
        self.setCurrentWidget(view)
        return view

    def update_title(self, view, title):
        index = self.indexOf(view)
        if 'DuckDuckGo' in title:
            self.setTabText(index, 'Search')
        else:
            self.setTabText(index, title)

    def update_icon(self, view, icon):
        index = self.indexOf(view)
        self.setTabIcon(index, icon)


class Browser(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        #main browser functrion
        
        super().__init__(parent)
        self.setCentralWidget(self.tab_widget)
        
        view = self.tab_widget.create_tab()
        view.load(QtCore.QUrl("https://www.duckduckgo.com/"))

        QtWebEngineWidgets.QWebEngineProfile.defaultProfile().downloadRequested.connect(self.on_downloadRequested)

    @QtCore.pyqtSlot("QWebEngineDownloadItem*")
    def on_downloadRequested(self, download):
        old_path = download.url().path()
        suffix = QtCore.QFileInfo(old_path).suffix()
        path, _ = QtWidgets.QFileDialog.getSaveFileName(
            self, "Save File", old_path, "*." + suffix
        )
        if path:
            download.setPath(path)
            download.accept()

    
    @cached_property
    def tab_widget(self):
        return TabWidget()
    
def main():
    app = QtWidgets.QApplication(sys.argv)
    w = Browser()
    w.show()
    w.showMaximized()
    sys.exit(app.exec_())


if __name__ == "__main__":
    while True:
        main()

Upvotes: 0

Anmol Gautam
Anmol Gautam

Reputation: 1020

Here is a simpler version of webbrowser using PyQt5 :

import sys
from PyQt5 import QtWidgets,QtGui,QtCore
from PyQt5.QtWebEngineWidgets import *
app=QtWidgets.QApplication(sys.argv)
w=QWebEngineView()
w.load(QtCore.QUrl('https://google.com')) ## load google on startup
w.showMaximized()
app.exec_()

You can now add different widgets to it . In python you have two most common ways to make a webbrowser 1. by using gtk webkit 2. by QtWebEngine under PyQt5.

Webkit is based upon Safari while QtWebEngine is based on Chromium. You can decide which one suits you the best. Hope it helps.

Upvotes: 4

Related Questions