File size: 1,717 Bytes
9a7a3b0
0d07eeb
c3df806
0d07eeb
 
 
9a7a3b0
 
 
0d07eeb
 
c3df806
0d07eeb
 
 
 
 
 
 
 
 
 
 
c3df806
0a6e204
 
c3df806
 
 
 
0d07eeb
 
c3df806
0d07eeb
 
 
 
 
 
 
 
 
c3df806
0d07eeb
 
 
 
 
 
 
 
9a7a3b0
 
0d07eeb
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
from flask import Flask, send_from_directory
from flask_cors import CORS
import gradio as gr
import threading

# Set the temporary directory for Gradio to a writable location
os.environ["GRADIO_TEMP_DIR"] = "/tmp"

# Create a Flask app to serve static files
flask_app = Flask(__name__)
CORS(flask_app)  # Enable CORS

# Serve the Mario game files
@flask_app.route("/mario")
def serve_index():
    return send_from_directory("/app/mario", "index.html")

# Serve static files (JavaScript, CSS, assets, etc.)
@flask_app.route("/mario/<path:path>")
def serve_static(path):
    return send_from_directory("/app/mario", path)

# Debug route to list files
@flask_app.route("/mario/files")
def list_files():
    import os
    files = os.listdir("/app/mario")
    return {"files": files}

# Run Flask in a separate thread
def run_flask():
    print("Starting Flask server...")
    flask_app.run(host="0.0.0.0", port=5000)

flask_thread = threading.Thread(target=run_flask)
flask_thread.daemon = True
flask_thread.start()

# Gradio interface to embed the game
def serve_game():
    game_url = "http://localhost:5000/mario"  # Flask is running on port 5000
    return f'<iframe src="{game_url}" width="800" height="600" style="border: none;"></iframe>'

# Create a Gradio interface
iface = gr.Interface(
    fn=serve_game,  # Function to generate the HTML content
    inputs=None,    # No inputs needed
    outputs=gr.HTML(),  # Output is HTML content
    live=True,      # Keep the interface live
    title="Mario HTML Game",
    description="Play the Mario HTML game embedded in Gradio!",
    allow_flagging="never"  # Disable flagging
)

# Launch the Gradio app
iface.launch(server_name="0.0.0.0", server_port=7860)