Host website interacting with embedded flask replit

After embedding a replit in an iframe and running it, is there any way for the host website to interact with it?

e.g. If my replit has the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/hello')
def hello_world():
  return 'Hello World'

if __name__ == '__main__':
  app.run('0.0.0.0', 5000)

Is there any way for the host website to call /hello on that specific replit instance?

Hello @ThreeMovesAhead , welcome to the forums!
You can try this:

from flask import Flask

app = Flask(__name__)

@app.route('/hello')
def hello_world():
  page=open('index.html','r') # opens a file called index.html 
  return page # displays the file
  page.close() # closes the file

if __name__ == '__main__':
  app.run('0.0.0.0', 5000)

With this code, you can display a file called index.html, linked to the app route ā€˜Hello’.
Hope this helps!
(If your problem is solved, you can mark this post as a Solution)

page.close() never gets called. Also, that’s not what they wanted to do.

Yes. If you want to make a request to ā€œ/helloā€ from the client side of the host website, you can do something like this in your javascript: (This doesn’t require the repl to be embedded in it)

const response = await fetch("https://repl-name--threemovesahead.repl.co/hello");
console.log(await response.text()); // "Hello World"

Note that you will need to configure CORS headers on your flask repl:

from flask import Flask, make_response
@app.route('/hello')
def hello_world():
  response = make_response("Hello World")
  response.headers["Access-Control-Allow-Origin"] = "https://host-website-origin-here"
  return response
3 Likes

Thank you for your helpful response.

I’m aware that replit does create a public url for your flask instance if you set host to ā€œ0.0.0.0ā€, but what I’m trying to achieve is specifically:

My understanding is, when a user runs an embedded replit, a new instance is created for them. I want the host website to be able to communicate with that specific instance.

Could you be more specific on what you mean by embedding the repl? (e.g. do you mean accessing it from the cover page, embedding the public url with an iframe on another website, or embedding the cover page on a website?)

You are correct that when you run a console repl (one that does not have a webserver) from its cover page on replit.com, a new instance (ā€œghost forkā€) is created for them.

However, your repl has a web server, so a new instance is not created for each user, regardless of whether they access it via the cover page, direct url, or embed from another site.

Essentially, all users of a repl with a web server are interacting with the same instance of the web server.

1 Like

Ah… I did not realize that. I specifically want to the host website to communicate with what you call the ā€œghost forkā€.

If it wasn’t a web server, is there anyway to communicate via postMessage? Something like below:

<html>
    <body>
        <iframe id="my_replit" src="<my_replit_url>?embed=true"></iframe>
        <script>
            // Method 1
            // post message to iframe
            document.getElementById("my_replit").contentWindow.postMessage(...);

            // listen for message from replit iframe?
            window.addEventListener(
                "message",
                (event) => {
                    // do stuff
                },
                false,
            );
        </script>
    </body>
</html>

No, this isn’t possible, because you don’t control the webpage returned by <my_replit_url> (I’m assuming it’s something of the form replit.com/@ThreeMovesAhead/repl-name.), and afaik replit doesn’t offer a way of interacting with repls using frame messaging.

What exactly are you trying to do? There might be another way that doesn’t involve passing messages to a ghost fork

I want to use javascript to execute python code. I’m exploring whether embedding replit can be a viable alternative to pyodide.

It might be easier to have the python code in the same repl as the host website. You can send a network request to a web server running on the repl and have it execute python code there

Nice suggestion. I’ll mark it as the solution for replit users, though I ultimately just went with pyodide (makes it possible to run python in browser)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.