1

I have a python function like this :

def sum(x,y):
    return  x+y

I created a web.html file I want to print my results of my function in this html ? I never used it so looking for ways to print mu results inside it ?

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My webpage.</title>
</head>
<body>
</html>
habdie
  • 109
  • 7
  • are you using some kind of system like flask or django ? – SebNik Apr 18 '20 at 13:21
  • web pages work different then desktop programs or console scripts - so better learn web frameworks like Flask, Django, Bottle – furas Apr 18 '20 at 13:22
  • Does this answer your question? [How can I execute a python script from an html button?](https://stackoverflow.com/questions/48552343/how-can-i-execute-a-python-script-from-an-html-button) – MARSHMALLOW Apr 18 '20 at 13:24
  • Does this answer your question? https://stackoverflow.com/questions/40844903/how-to-run-python-script-in-html – MARSHMALLOW Apr 18 '20 at 13:25
  • minimal version is `html = "your html {} your html".format(sum(2,3))` to generate string with all your HTML and result from `sum()` and which you have to save in file - `web.html`. But if you what to send it to client then you still need www server - so better use Flask, Django or other web framework. – furas Apr 18 '20 at 13:26

1 Answers1

0

You need some kind of framework to get it to work. There are a few frameworks for python for example django or flask. With flask you would then deploy your website on the localhost or on some provider like Heroku.

One option using flask and python would be:

import flask
app = Flask(__name__)

def sum(x,y):
    return  x+y

@app.route('/')
@app.route('/index')
def index():
    dic = {'value': sum(x,y)}
    return '''
<html>
    <head>
        <title>Home Page</title>
    </head>
    <body>
        <h1>Hello, your sum is ''' + dic['value'] + '''!</h1>
    </body>
</html>'''

That's an example for it, but be sure to check out maybe: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

Moreover this is the easy solution. The more professional would be using flask and render_template

SebNik
  • 880
  • 3
  • 10
  • 21