title: Flask routing, multiple route functions
body: <p>So, to achieve a seemless web app, not having the page reload on every new view change is pretty essential. To do this, I'm using socketio, and converting to base64 on the backend so html isn't lost in transmission.</p>
  
  <p>However, when the user goes directly to the url, I want them to be able to see the same page as if a user was shown the page if they clicked the link on the index page, then loading it via socketio. (index &rarr; page)</p>
  
  <p>This is the socketio routing decorator class <a href="https://ains.co/blog/things-which-arent-magic-flask-part-1.html" rel="nofollow noreferrer">inspired by a blog post by Ainsley Jones</a></p>
  
  <pre><code>class socketio_route:
      def __init__(self):
          self.routes = {}
  
      def route(self, rule):
          if rule[0] == '/':
              rule = rule[1::]
          def decorator(f):
              self.routes[rule] = f
              return f
          return decorator
  
      def serve(self, path):
          view_func = self.routes.get(path)
          if view_func:
              return view_func(False)
  </code></pre>
  
  <p>which is then used in conjunction with the standard <code>@app.route('/')</code> flask decorator.</p>
  
  <pre><code>@app.route('/branches')
  @skt.route('/branches')
  def route_branches(fullpage=True):
      values = {"data": {'branch': 'master'}}
      page = render_template('default.html', **values)
  
      if fullpage == False:
          socketio.emit('page', base64.b64encode(page), namespace='/page')
      else:
          return render_template('index.html', socket_preload_content=page)
  </code></pre>
  
  <p>This returns the <strong><em>bare-bones</em></strong> page if it is accessed via socketio, and the full page (a <code>render_template</code> of the initial <code>render_template</code>) if it is accessed via the default method; directly.</p>
  
  <p>I'm using <a href="https://flask-socketio.readthedocs.io/en/latest/" rel="nofollow noreferrer"><code>flask_socketio</code></a> to handle all things socketio.
  <br><br></p>
  
  <h3>Question</h3>
  
  <p>So, my question is, is it possible to integrate all of this into a single route decorator, or extend the existing flask <code>@app.route()</code> decorator?</p>
  
  <p>Maybe something like this for the custom route?</p>
  
  <pre><code>@CUSTOM.route('/branches')
  def branches():
      values = {"data": {'branch': 'master'}}
      return render_template('default.html', **values)
  </code></pre>
  
  <p>which would return either</p>
  
  <pre><code>socketio.emit('page', base64.b64encode(OUTPUT_FROM_ABOVE), namespace='/page')
  </code></pre>
  
  <p>or</p>
  
  <pre><code>render_template('index.html', socket_preload_content=OUTPUT_FROM_ABOVE)
  </code></pre>
  
  <p>depending on the way it was called.
  <br>
  <br></p>
  
  <p>I looked into flask's <a href="http://flask.pocoo.org/docs/0.12/api/#flask.Flask.add_url_rule" rel="nofollow noreferrer"><code>app.add_url_rule</code></a> but was confused as to how I might integrate this into my existing <code>socketio_route</code> decorator class.</p>
  
  <p>Can anyone suggest as to how I might go about this?</p>