1

Got an HTML code stored in a variable in Python script, I need the browser to open with my HTML. Thanks.

Maxim Mashkov
  • 61
  • 1
  • 3
  • 4
    Possible duplicate of [webbrowser.open() in python](https://stackoverflow.com/questions/22004498/webbrowser-open-in-python) – Ocaso Protal Jun 11 '19 at 08:45

2 Answers2

1

You can do with many ways one of the below :

import os
import webbrowser

html = '<html>Hello World</html>'
path = os.path.abspath('sample.html')
url = 'file://' + path

with open(path, 'w') as f:
    f.write(html)
webbrowser.open(url)
IMParasharG
  • 1,869
  • 1
  • 15
  • 26
1

Save the following HTML to a file:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="utf-8" />
  <title>Hello World!</title>
</head>
<body>
  <h1>Hello World!</h1>
  <p>This is a HTML document!</p>
</body>
</html>

Then add the following code to your python script:

import webbrowser
webbrowser.open("file://path/to/document.html")

Good luck.

Malekai
  • 4,765
  • 5
  • 25
  • 60