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>
5
  • are you using some kind of system like flask or django ? Commented Apr 18, 2020 at 13:21
  • web pages work different then desktop programs or console scripts - so better learn web frameworks like Flask, Django, Bottle Commented Apr 18, 2020 at 13:22
  • Does this answer your question? How can I execute a python script from an html button? Commented Apr 18, 2020 at 13:24
  • Does this answer your question? stackoverflow.com/questions/40844903/… Commented Apr 18, 2020 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. Commented Apr 18, 2020 at 13:26

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.