sudo-soldier commited on
Commit
8a8a1f3
·
verified ·
1 Parent(s): 8256401

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -29
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import os
3
  import zipfile
4
  from pathlib import Path
 
5
 
6
  # Define paths
7
  ZIP_FILE = "nes.zip"
@@ -53,33 +54,29 @@ def list_roms():
53
  print("Found ROMs:", roms) # Debugging output
54
  return [str(rom.relative_to(EXTRACTED_FOLDER)) for rom in roms]
55
 
56
- # Generate HTML content with links to the ROMs
57
- def generate_html():
58
- rom_files = list_roms()
59
- html_content = """
 
 
60
  <html>
61
- <head>
62
- <style>
63
- body, html { margin: 0; padding: 0; text-align: center; font-family: Arial; }
64
- #game { width: 80vw; height: 80vh; margin: auto; }
65
- </style>
66
- </head>
67
- <body>
68
- <h1>Available ROMs</h1>
69
- <ul>
70
- """
71
-
72
- if rom_files:
73
- for rom_name in rom_files:
74
- # Adjust for Gradio file serving
75
- rom_url = f"/file/{rom_name}"
76
- html_content += f'<li><a href="play.html?rom={rom_url}">{rom_name}</a></li>'
77
- else:
78
- html_content += "<li>No ROMs available</li>"
79
-
80
- html_content += """
81
- </ul>
82
- </body>
83
  </html>
84
  """
85
  return html_content
@@ -89,10 +86,16 @@ def gradio_app():
89
  with gr.Blocks() as app:
90
  gr.Markdown("# 🎮 Retro ROM File Server")
91
 
92
- show_html_button = gr.Button("View ROM List as HTML")
93
- html_output = gr.HTML()
 
 
 
 
 
94
 
95
- show_html_button.click(generate_html, outputs=html_output)
 
96
 
97
  app.launch(server_name="0.0.0.0", server_port=7860, share=True)
98
 
@@ -102,3 +105,4 @@ gradio_app()
102
 
103
 
104
 
 
 
2
  import os
3
  import zipfile
4
  from pathlib import Path
5
+ import urllib.parse
6
 
7
  # Define paths
8
  ZIP_FILE = "nes.zip"
 
54
  print("Found ROMs:", roms) # Debugging output
55
  return [str(rom.relative_to(EXTRACTED_FOLDER)) for rom in roms]
56
 
57
+ # Function to generate HTML for playing the selected ROM
58
+ def generate_rom_page(rom_name):
59
+ rom_path = os.path.join(EXTRACTED_FOLDER, rom_name)
60
+ rom_url = urllib.parse.quote(rom_path) # Ensure correct URL encoding
61
+
62
+ html_content = f"""
63
  <html>
64
+ <head>
65
+ <div style='width:640px;height:480px;max-width:100%'>
66
+ <div id='game'></div>
67
+ </div>
68
+
69
+ <script>
70
+ EJS_player = "#game";
71
+ EJS_core = "nes";
72
+ EJS_color = "#0064ff";
73
+ EJS_pathtodata = "https://cdn.emulatorjs.org/stable/data/";
74
+ EJS_gameUrl = "https://sudo-soldier-nes.hf.space/RETRO/{rom_url}";
75
+ </script>
76
+ <script src="https://cdn.emulatorjs.org/stable/data/loader.js"></script>
77
+ </head>
78
+ <body>
79
+ </body>
 
 
 
 
 
 
80
  </html>
81
  """
82
  return html_content
 
86
  with gr.Blocks() as app:
87
  gr.Markdown("# 🎮 Retro ROM File Server")
88
 
89
+ # List available ROMs and create a button for each
90
+ rom_files = list_roms()
91
+
92
+ if rom_files:
93
+ for rom_name in rom_files:
94
+ # Create a button for each ROM
95
+ gr.Button(rom_name).click(generate_rom_page, inputs=gr.State(rom_name), outputs=gr.HTML())
96
 
97
+ # Display the ROM page
98
+ gr.HTML(label="ROM Emulator").style(height=500)
99
 
100
  app.launch(server_name="0.0.0.0", server_port=7860, share=True)
101
 
 
105
 
106
 
107
 
108
+