title: Coaxing Flask/werkzeug to render a (flask-admin) page from the python shell
body: <p>Using the python shell I'm poking at <a href="https://flask-admin.readthedocs.org" rel="nofollow">flask-admin</a>/<a href="https://flask-admin.readthedocs.org/en/latest/db_pymongo/" rel="nofollow">pymongo</a>/<a href="https://flask.readthedocs.org" rel="nofollow">flask</a> and trying to figure out if I can coax it into dumping the html that it would generate for a given page.</p>
  
  <p>The code I'm pasting into python shell is as follows:</p>
  
  <pre class="lang-python prettyprint-override"><code>from flask import Flask
  from flask.ext.admin import Admin
  from flask.ext.admin.contrib.pymongo import ModelView
  from wtforms import form, fields
  import pymongo
  
  class UserForm(form.Form):
      dispname = fields.TextField('Name')
      email = fields.TextField('Email')
  
  class UserView(ModelView):
      column_list = ('dispname', 'email', 'username', 'userextra')
      form = UserForm
  
  client = pymongo.MongoClient()
  db = client['cloud']
  app = Flask('putting_a_name_here')
  admin = Admin(app)
  admin.add_view(UserView(db['userpgs']))
  
  #app.run(host='0.0.0.0') # If this is uncommented connecting to
                           # http://localhost:5000/admin/userpgsview
                           # properly renders the contents of the mongo
                           # collection 'userpgs' in db 'cloud'
  </code></pre>
  
  <p>(I realize I'm making this considerably more complex by leaving the <code>flask-admin</code> and <code>pymongo</code>/<code>flask.ext.admin.contrib.pymongo</code> in the example, but the <code>flask-admin</code> rendering/html is what I'm really trying to understand.)</p>
  
  <p>I haven't been able to find direct documentation on how flask rendering/html is called/used, but by adding some debugging to the library I've been able to see the above code causes blueprint/rule code in flask to run and populate a list in my <code>app</code> called <code>view_functions</code> with function callbacks (with promising names like <code>userpgsview.ajax_loopup</code>, <code>userpgsview.index_view</code>, etc).</p>
  
  <p>The <code>index_view</code>, which (from other debugging) appears to be bound to url <code>admin/userpgsviw/</code> is what I most want to understand, so I started by trying a:</p>
  
  <pre><code>&gt;&gt;&gt; app.view_functions.get(u'userpgsview.index_view')()
  </code></pre>
  
  <p>which caused a traceback on the exception <code>RuntimeError: working outside of application context</code></p>
  
  <p>Digging around in the flask documentation on using the <a href="http://flask.pocoo.org/docs/shell/" rel="nofollow">python shell</a> I found information about setting a context, so I did the following</p>
  
  <pre class="lang-python prettyprint-override"><code>ctx=app.test_request_context()
  ctx.push()
  app.preprocess_request()
  </code></pre>
  
  <p>and tried again, resulting in:</p>
  
  <pre><code>&gt;&gt;&gt; app.view_functions.get(u'userpgsview.index_view')()
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/usr/local/lib/python2.7/dist-packages/flask_admin/base.py", line 62, in inner
      return f(self, *args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/flask_admin/base.py", line 62, in inner
      return f(self, *args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/flask_admin/base.py", line 62, in inner
      return f(self, *args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/flask_admin/model/base.py", line 1191, in index_view
      filters),
    File "/usr/local/lib/python2.7/dist-packages/flask_admin/model/base.py", line 1062, in _get_url
      return url_for(view, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/flask/helpers.py", line 312, in url_for
      return appctx.app.handle_url_build_error(error, endpoint, values)
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in handle_url_build_error
      reraise(exc_type, exc_value, tb)
    File "/usr/local/lib/python2.7/dist-packages/flask/helpers.py", line 305, in url_for
      force_external=external)
    File "/usr/local/lib/python2.7/dist-packages/werkzeug/routing.py", line 1620, in build
      raise BuildError(endpoint, values, method)
  werkzeug.routing.BuildError: ('index_view', {}, None)
  </code></pre>
  
  <p>And I haven't been able to find any leads on how I might initialize things so <code>werkzeug.routing</code> is happy.</p>
  
  <p>Frank, I won't be surprised if I'm coming at this from the completely wrong approach, but I haven't had any luck in finding other documentation I how I should be hand-running flask if my goal is to coax it into generating the html it would create on a html-get of <code>http://localhost:5000/admin/userpgsview</code>.</p>
  
  <p>Any ideas would be greatly welcome.</p>