Ticket #1501: embedded_ipython_shell_r2521.diff

File embedded_ipython_shell_r2521.diff, 2.5 KB (added by django@…, 19 years ago)

patch to embed IPython shell in development server

  • django/utils/shell.py

    === django/utils/shell.py
    ==================================================================
     
     1"""
     2Django embedded IPython shell
     3
     4When using the development server Django's embedded IPython shell allows you to
     5drop into a Python shell at any point during a request to inspect the local
     6namespace.
     7
     8Requires IPython.
     9
     10usage:
     11
     12from django.utils.shell import ipshell
     13
     14def some_view(request):
     15    response = render_to_response(...)
     16    ipshell() # suspend request here and drop into IPython shell
     17    return response
     18
     19If a call to ipshell() is left in production code a warning will be sent to
     20stdout and processing will continue normally.
     21
     22"""
     23
     24BANNER = """
     25# Interrupting this request to bring you the Django Shell!
     26
     27Django  %s
     28Python  %s
     29IPython %s
     30
     31?       -> Introduction to IPython's features.
     32%%magic  -> Information about IPython's 'magic' functions.
     33help    -> Python's own help system.
     34object? -> Details about 'object'. ?object also works, ?? prints more.
     35
     36Type Ctrl-D or 'quit' to continue serving the request."""
     37
     38def make_fake_shell(message):
     39    def ipshell():
     40        print "\n# %s\n" % message
     41    return ipshell
     42
     43try:
     44    from IPython.Shell import IPShellEmbed
     45except ImportError:
     46    ipshell = make_fake_shell("Django Shell error: Could not import IPython")
     47else:
     48    import django
     49    import sys
     50    import IPython
     51
     52    def version_to_string(version_info):
     53        return '%s.%s.%s %s' % version_info[:4]
     54
     55    # Django development server sets a __builtin__ flag so we can check it is running
     56    try:
     57        __DJANGO_DEVELOPMENT_SERVER__
     58    except NameError:
     59        ipshell = make_fake_shell("Django Shell warning: ipshell() called from non-development server, ignored")
     60    else:
     61        ipshell = IPShellEmbed([], banner=BANNER % (
     62            version_to_string(django.VERSION),
     63            version_to_string(sys.version_info),
     64            IPython.__version__))
  • django/core/servers/basehttp.py

    === django/core/servers/basehttp.py
    ==================================================================
     
    637637        return output
    638638
    639639def run(addr, port, wsgi_handler):
     640    # Development server flag checked by django.utils.shell.ipshell()
     641    import __builtin__
     642    __builtin__.__DJANGO_DEVELOPMENT_SERVER__ = True
    640643    server_address = (addr, port)
    641644    httpd = WSGIServer(server_address, WSGIRequestHandler)
    642645    httpd.set_app(wsgi_handler)
Back to Top