Ask coding questions

← Back to all posts
How do I open a webpage? and have python communicate with that page?
h
JULIODIAZ1 (46)

Me and a friend( @TheC0derGirl ) are making a "simple" python game where you choose what to make: a pizza, icecream, and hot coco. And were going to open a page when its finished to show the finished product and we need to communacate those two projects to tell the website what the pizza or icecrem, etc. looks like so it knows what to put on the page.

Answered by 19ecal (228) [earned 5 cycles]
View Answer
Comments
hotnewtop
19ecal (228)

You can send a link like this
https://repl.username.repl.co/?food=pizza
and have a flask server at that address like

from flask import *
app=Flask("main")
@app.route("/")
def main():
    food=request.args.get("food")
    if food=="pizza":
        return render_template("pizza.html")
    elif food=="icecream":
        return render_template("icecream.html")

You can have more URL arguments on the end like
https://repl.username.repl.co/?food=pizza&topping1=cheese
Then have more elifs to render pizzawithcheese.html
And get the URL arguments with request.args.get

19ecal (228)

Sorry if that doesn't make as much sense as it could I'm on mobile and it's hard to type

TheC0derGirl (210)

@19ecal oooooooooooooooooooooooooooooh ok thanks

JULIODIAZ1 (46)

thanks! ill try and make sense of it!
@19ecal

19ecal (228)

@JULIODIAZ1 @TheC0derGirl I forgot, that won't work, you need to run it with app.run(host="0.0.0.0") at the end

JULIODIAZ1 (46)

question, would it be possible to send variables over from python to the html page? @19ecal

19ecal (228)

@JULIODIAZ1 Well, you can use this
Python:

from flask import *
app=Flask("main")
@app.route("/")
def main():
    return render_template("page.html", text="some text")

page.html

<html>
    <head>
    </head>
    <body>
        <p>{{ text }}</p>
    </body>
</html>

That should put whatever you put text as in render_template() in the <p>

MEGATANK58 (0)

You can pass arguments as text (or http requests) or URL arguments to the website and receive it on the other end and show the image