app.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # This file is part of DarkFi (https://dark.fi)
  2. #
  3. # Copyright (C) 2020-2024 Dyne.org foundation
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import rpc
  18. from flask import Flask, request, render_template
  19. app = Flask(__name__)
  20. @app.route('/')
  21. async def index():
  22. # Fetch data from RPC
  23. blocks = await rpc.get_last_n_blocks("10")
  24. basic_stats = await rpc.get_basic_statistics()
  25. # Fetch the metric statistics
  26. metric_stats = await rpc.get_metric_statistics()
  27. has_metrics = metric_stats and isinstance(metric_stats, list)
  28. # Get the latest metric statistics, or use None if no metrics are found
  29. if has_metrics:
  30. latest_metric_stats = metric_stats[-1]
  31. else:
  32. latest_metric_stats = None
  33. # Render template
  34. return render_template(
  35. 'index.html',
  36. blocks=blocks,
  37. basic_stats=basic_stats,
  38. metric_stats=latest_metric_stats,
  39. )
  40. @app.route('/search', methods=['GET', 'POST'])
  41. async def search():
  42. search_hash = request.args.get('search_hash', '')
  43. block = await rpc.get_block(search_hash)
  44. transactions = await rpc.get_block_transactions(search_hash)
  45. if transactions:
  46. return render_template('block.html', block=block, transactions=transactions)
  47. else:
  48. transaction = await rpc.get_transaction(search_hash)
  49. return render_template('transaction.html', transaction=transaction)
  50. @app.route('/block/<header_hash>')
  51. async def block(header_hash):
  52. block = await rpc.get_block(header_hash)
  53. transactions = await rpc.get_block_transactions(header_hash)
  54. return render_template('block.html', block=block, transactions=transactions)
  55. @app.route('/transaction/<transaction_hash>')
  56. async def transaction(transaction_hash):
  57. transaction = await rpc.get_transaction(transaction_hash)
  58. return render_template('transaction.html', transaction=transaction)
  59. @app.errorhandler(404)
  60. def page_not_found(e):
  61. return render_template('404.html'), 404
  62. @app.errorhandler(500)
  63. def page_not_found(e):
  64. return render_template('500.html'), 500