title: How do I serve both HTML and JSON content with Flask?
body: <p>I'd like to setup a Flask app that has both a GUI and API component.  So here's a simple example that retrieves a list of groups:</p>
  
  <pre><code>def get_groups():
      groups = db.session.query(SgmGroup.group_name).order_by(SgmGroup.group_name).all()
      return [group.group_name for group in groups]
  
  @app.route('/list')
  def group_list():
      groups_list = get_groups()
      return render_template('groups.html', groups=groups_list)
  </code></pre>
  
  <p>So far so good - this will give the user an HTML list of users, maybe in a table or some other nice GUI format.</p>
  
  <p>But I'd also like to serve JSON content for API requests:</p>
  
  <pre><code>@app.route('/api/list')
  def api_group_list():
      groups_list = get_groups()
      return jsonify({'groups': group_list})
  </code></pre>
  
  <p>This works - but is this a good idea?  I have two questions:</p>
  
  <p>1) Is this the appropriate way to use Flask when I have two different types of content?  Make an <code>/endpoint</code> URL for web browsers (HTML), and an identical <code>/api/endpoint</code> URL to handle API requests (JSON)?</p>
  
  <p>2) Or should I combine these two endpoints into a single function?  That would seem to be neater, but how can you determine which endpoint the user accessed?   That is, can the pseudocode below be done?</p>
  
  <pre><code>@app.route('/list')
  @app.route('/api/list')
  def group_list():
      groups_list = get_groups()
  
      if &lt;user arrived here via the /list endpoint&gt;:
          return render_template('groups.html', groups=groups_list)
  
      elif &lt;user arrived here via the /api/list endpoint&gt;:
          return jsonify({'groups': group_list})
  </code></pre>