old_contents: from flask import Flask, send_file, request, render_template, jsonify
  import json
  app = Flask(__name__)
  from hexagram import Hexagram, Trigram
  
  def request_wants_json():
      best = request.accept_mimetypes \
          .best_match(['application/json', 'text/html'])
      return best == 'application/json' and \
          request.accept_mimetypes[best] > \
          request.accept_mimetypes['text/html']
  
  @app.route('/hexagram/<hexagram>.png')
  def hex_out(hexagram):
      generated = Hexagram([int(elem) for elem in hexagram])
      return send_file(generated.dump_image(), mimetype='image/png')
  
  @app.route('/trigram/<trigram>.png')
  def tri_out(trigram):
      generated = Trigram([int(elem) for elem in trigram])
      return send_file(generated.dump_image(), mimetype='image/png')
  
  @app.route('/')
  def index():
      return render_template('index.html'), 200
  
  @app.errorhandler(404)
  def page_not_found(error):
      error = {
          "Hexagram": "/hexagram/nnnnnn.png, where n is 1 (solid bar) or 0 (broken bar). Hexagrams are built bottom to top.",
          "Trigram": "/trigram/nnn.png, where n is 1 (solid bar) or 0 (broken bar). Trigrams are built bottom to top."
      }
      if request_wants_json():
          return jsonify(items=[json.dumps(error)])
      return render_template('index.html'), 404
  
  @app.errorhandler(500)
  def app_error(error):
      error = {
          "Hexagram": "/hexagram/nnnnnn.png, where n is 1 (solid bar) or 0 (broken bar). Hexagrams are built bottom to top.",
          "Trigram": "/trigram/nnn.png, where n is 1 (solid bar) or 0 (broken bar). Trigrams are built bottom to top."
      }
      if request_wants_json():
          return jsonify(items=[json.dumps(error)])
      return render_template('index.html'), 500
  
  if __name__ == '__main__':
      app.run()
  
new_contents: from flask import Flask, send_file, request, render_template, jsonify
  import json
  app = Flask(__name__)
  from hexagram import Hexagram, Trigram
  
  def request_wants_json():
      best = request.accept_mimetypes \
          .best_match(['application/json', 'text/html'])
      return best == 'application/json' and \
          request.accept_mimetypes[best] > \
          request.accept_mimetypes['text/html']
  
  @app.route('/')
  def index():
      msg = {
          "Hexagram": "/hexagram/nnnnnn.png, where n is 1 (solid bar) or 0 (broken bar). Hexagrams are built bottom to top.",
          "Trigram": "/trigram/nnn.png, where n is 1 (solid bar) or 0 (broken bar). Trigrams are built bottom to top."
      }
      if request_wants_json():
          return jsonify(items=[json.dumps(msg)])
      return render_template('index.html'), 200
  
  @app.route('/hexagram/<hexagram>.png')
  def hex_out(hexagram):
      generated = Hexagram([int(elem) for elem in hexagram])
      return send_file(generated.dump_image(), mimetype='image/png')
  
  @app.route('/trigram/<trigram>.png')
  def tri_out(trigram):
      generated = Trigram([int(elem) for elem in trigram])
      return send_file(generated.dump_image(), mimetype='image/png')
  
  @app.errorhandler(404)
  def page_not_found(error):
      error = {
          "Hexagram": "/hexagram/nnnnnn.png, where n is 1 (solid bar) or 0 (broken bar). Hexagrams are built bottom to top.",
          "Trigram": "/trigram/nnn.png, where n is 1 (solid bar) or 0 (broken bar). Trigrams are built bottom to top."
      }
      if request_wants_json():
          return jsonify(items=[json.dumps(error)])
      return render_template('index.html'), 404
  
  @app.errorhandler(500)
  def app_error(error):
      error = {
          "Hexagram": "/hexagram/nnnnnn.png, where n is 1 (solid bar) or 0 (broken bar). Hexagrams are built bottom to top.",
          "Trigram": "/trigram/nnn.png, where n is 1 (solid bar) or 0 (broken bar). Trigrams are built bottom to top."
      }
      if request_wants_json():
          return jsonify(items=[json.dumps(error)])
      return render_template('index.html'), 500
  
  if __name__ == '__main__':
      app.run()
  
subject: Index can return JSON