Mario / app.py
Gregniuki's picture
Update app.py
51bd00e verified
raw
history blame
1.59 kB
import subprocess
import sys
import gradio as gr
# Install Flask if not already installed
def install_flask():
try:
import flask
except ImportError:
subprocess.run([sys.executable, "-m", "pip", "install", "flask"], check=True)
# Install Flask on startup
install_flask()
# Clone the Mario game repository
def install_package():
subprocess.run(["git", "clone", "https://github.com/reruns/mario.git", "mario"], check=True)
return "http://localhost:7860/mario" # Local URL (Change port if needed)
# Install the game files on startup
install_package()
# Create a Flask app to serve static files
flask_app = Flask(__name__)
@flask_app.route("/mario")
def serve_index():
return send_from_directory("mario", "index.html")
# Now import Flask
from flask import Flask, send_from_directory
# Serve static files (JavaScript, CSS, assets, etc.)
@flask_app.route("/mario/<path:path>")
def serve_static(path):
return send_from_directory("mario", path)
# Read the index.html file
def serve_game():
game_url = "http://localhost:7860/mario" # Or from install_package()
return f'<iframe src="{game_url}" width="800" height="600"></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!"
)
# Launch the Gradio app
iface.launch(server_name="0.0.0.0", server_port=7860)