Create an html page and launch it with CefPython3 in Chrome

With this simple script you can launch the browser Chrome with cefpython 3, after you created the page in the same script that launches the browser:

from cefpython3 import cefpython as cef
import sys
import os


def open_link(url):
    sys.excepthook = cef.ExceptHook
    cef.Initialize()
    cef.CreateBrowserSync(url=url, window_title=url.replace("file:///",""))
    cef.MessageLoop()


if __name__ == '__main__':
    html = """<h3>Welcome to my site</h3>
<img src="https://lh6.googleusercontent.com/-STVPXBxpfbk/AAAAAAAAAAI/AAAAAAAAAMM/7wmzAdslc70/photo.jpg?sz=48" />
Hi, come to check my blog <a href="https://pythonprogramming.altervista.org">pythonprogramming.altervista.org</a>
"""
    name = "my.html"
    # if the file already exists, it will not be created again
    if name not in os.listdir():
        with open(name, "w") as file:
            file.write(html)
    open_link("file:///" + name)

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.