|
| 1 | +# freeze.py |
| 2 | +import sys |
| 3 | +import traceback |
| 4 | +from flask_frozen import Freezer |
| 5 | +# Import the Flask app instance from app/__init__.py |
| 6 | +from app import app |
| 7 | + |
| 8 | +# --- Configuration for GitHub Pages --- |
| 9 | +# Sets the base URL. CRITICAL for links/CSS/JS on GitHub Pages. |
| 10 | +# Must match your repository name structure. |
| 11 | +print("Setting FREEZER_BASE_URL to '/pyExplorer/'") |
| 12 | +app.config['FREEZER_BASE_URL'] = '/pyExplorer/' |
| 13 | + |
| 14 | +# Sets the output directory for the static files |
| 15 | +app.config['FREEZER_DESTINATION'] = 'build' |
| 16 | +# Keep .nojekyll file if generated by GitHub Actions |
| 17 | +app.config['FREEZER_REMOVE_EXTRA_FILES'] = False |
| 18 | + |
| 19 | +# --- Handling Dynamic URLs (IMPORTANT LIMITATION) --- |
| 20 | +# Static site generation CANNOT dynamically create pages for every possible |
| 21 | +# block, transaction, or address. We MUST provide examples/placeholders |
| 22 | +# for Frozen-Flask to generate the *structure* of these pages. |
| 23 | +# The actual *data* on these generated pages will only reflect |
| 24 | +# what Flask renders for these specific placeholder values during the build. |
| 25 | +# Features requiring real-time data or searching arbitrary hashes won't work fully. |
| 26 | + |
| 27 | +freezer = Freezer(app) |
| 28 | + |
| 29 | +@freezer.register_generator |
| 30 | +def transaction_urls(): |
| 31 | + print("Generating static structure for /tx/<hash> using placeholder.") |
| 32 | + # Provide one placeholder hash to generate the template structure |
| 33 | + yield {'tx_hash': 'placeholder_tx_hash'} |
| 34 | + |
| 35 | +@freezer.register_generator |
| 36 | +def block_urls(): |
| 37 | + print("Generating static structure for /block/<hash> using placeholder.") |
| 38 | + # Provide one placeholder hash/number |
| 39 | + yield {'block_hash': 'placeholder_block_hash_or_number'} |
| 40 | + |
| 41 | +@freezer.register_generator |
| 42 | +def address_urls(): |
| 43 | + print("Generating static structure for /address/<addr> using placeholder.") |
| 44 | + # Provide one placeholder address |
| 45 | + yield {'address': 'placeholder_address'} |
| 46 | + |
| 47 | +# You might have other dynamic routes - add generators for them too! |
| 48 | + |
| 49 | +# Main execution block for freezing |
| 50 | +if __name__ == '__main__': |
| 51 | + print(f"Starting static site generation for pyExplorer...") |
| 52 | + print(f"Base URL: {app.config.get('FREEZER_BASE_URL', 'Not Set')}") |
| 53 | + print(f"Output directory: {app.config.get('FREEZER_DESTINATION', 'build')}") |
| 54 | + |
| 55 | + try: |
| 56 | + # This crawls your Flask app (using defined routes and generators) |
| 57 | + # and saves the output as static files. |
| 58 | + freezer.freeze() |
| 59 | + print(f"Static site generated successfully in '{app.config['FREEZER_DESTINATION']}'.") |
| 60 | + print("NOTE: Only placeholder pages were generated for dynamic routes (/tx, /block, /address).") |
| 61 | + print("Full dynamic explorer functionality requires a live backend or client-side fetching.") |
| 62 | + except Exception as e: |
| 63 | + print(f"\nERROR during freezing process: {e}\n", file=sys.stderr) |
| 64 | + if isinstance(e, ValueError) and 'URL generator' in str(e): |
| 65 | + print("This often means a dynamic route exists in Flask", file=sys.stderr) |
| 66 | + print("but lacks a corresponding @freezer.register_generator", file=sys.stderr) |
| 67 | + print("function in freeze.py to provide placeholder values.", file=sys.stderr) |
| 68 | + traceback.print_exc() |
| 69 | + sys.exit(1) # Exit with error to fail the GitHub Action |
0 commit comments