IPython qtconsole custom banner - python

I'm trying to set a custom banner for the IPython qtconsole (v3.0.0). In my profile configuration, I set c.IPythonWidget.banner = u'Custom Banner', and then launch ipython qtconsole --profile=myprof. What I get is my custom banner pre-pended to the regular banner:
Custom BannerPython 2.7.5 (default, Mar 9 2014, 22:15:05)
Type "copyright", "credits" or "license" for more information.
IPython 3.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
%guiref -> A brief reference about the graphical user interface.
IPython profile: myprof
In [1]:
How can I get ONLY my banner to print, e.g.
Custom Banner
IPython profile: myprof
In [1]:
Thanks.

It's not ideal, but I was able to suppress the default banner by setting default_gui_banner=""in the core/usage.py module in the IPython source. I was not able to find any way to do this without modifying the source (which is ugly), so if anyone has a better way, I'm all ears.

The message you don't want is printed as part of the kernel's banner (which it gets from its shell). I was able to avoid printing it by setting the shell's banner1 attribute to an empty string before using/connecting to the kernel elsewhere:
from IPython.qt.inprocess import QtInProcessKernelManager
kernel_manager = QtInProcessKernelManager()
kernel_manager.start_kernel()
kernel_manager.kernel.shell.banner1 = ""
I've only worked with/tested this with the IPython Qt stuff, so this might not work for other kernels.

Related

How to interrupt the loop with IPython.embed() inside [duplicate]

I have some code like this:
form IPython import embed
for item in my_item_list:
embed()
If I then run this program with
python my_example_program.py
on the first iteration through the loop I get put into an ipython shell and can inspect item and the environment as I would like to.
On quitting ipython the loop resumes and then I can inspect the next item and the environment as you would expect.
Is there a way for me to quit this code from within ipython (so that I am returned to a shell prompt). in any way short of opening another shell and killing the process?
There's a %kill_embedded command in IPython.
It doesn't put you directly back to the shell prompt, but it skips the other embed instances.
from IPython import embed
for item in range(5):
print 'embedding', item
embed()
And here's the output:
$ python my_example_program.py
embedding 0
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: print item
0
In [2]: ^D
embedding 1
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [2]: %kill_embedded
Are you sure you want to kill this embedded instance (y/n)? [y/N] y
This embedded IPython will not reactivate anymore once you exit.
In [3]: print item
1
In [4]:
embedding 2
embedding 3
embedding 4
$
UPD (06.03.2016): Seems that the %kill_embedded feature is kind of broken in IPython 4.0; you can use %exit_raise which will raise an exception and return back to the shell.

How do I suppress the IPython startup message?

When debugging, I often drop into an IPython shell for interactive code testing. However this also causes a large info dump of the python version, date and help instructions to stdout. How can I suppress this information so it doesn't obscure the intentional debugging messages?
x = get_important_data()
print 'important info! right here! The data is in variable "x"'
import IPython
IPython.embed()
This code gives output like this...
important info! right here! The data is in variable "x"
Python 2.7.11 |Anaconda 2.4.0 (x86_64)| (default, Dec 6 2015, 18:57:58)
Type "copyright", "credits" or "license" for more information.
IPython 4.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
You can do this:
IPython.embed(banner1="")
Setting banner1 to empty string would make the startup messages go away. It will not actually remove the messages but will replace them with empty strings.
You can also add useful messages using the banner1, banner2 and exit_msg parameters:
IPython.embed(
banner1="Entering Debug Mode",
banner2="Here's another helpful message",
exit_msg="Exiting the debug mode!"
)
If you ever need to launch IPython instance from the command line, you can do this:
ipython --no-banner

how to set font for ipython matplotlib/pylab?

I set my font for matplotlib to be a ttf through ~/.matplotlib/matplotlibrc. When I run:
$ python myplot.py
it uses the correct font. but if I do:
$ ipython --pylab
Python 2.7.8 |Anaconda 2.1.0 (x86_64)| (default, Aug 21 2014, 15:21:46)
Type "copyright", "credits" or "license" for more information.
IPython 2.2.0 -- An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: %run myplot
it does not use the font. I'm using Anaconda Python on Mac OS X. My rcParams are not read. Does ipython (with pylab option) use a different configuration? How can I set my matplotlib default font?
to add to the confusion, if I use plt.show() in ipython, it by default uses one font, but if I plt.savefig() from ipython, it uses another.
As far as I know setting rcParams in the code itself before plotting is the best approach. IPython does a lot of magic with its inlining which tends to be very weird. Chances are that IPython tramples your rcParams with its own default values, if you set them in your script, the interpreter will read them in again.
from matplotlib import rcParams
rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = ['Bitstream Vera Sans']
rcParams['font.serif'] = ['Bitstream Vera Sans']
rcParams["font.size"] = "40"
Try this at the top of the script and report back?
Additionally you can try to run your script as ipython script.py --pylab. Issue could also be that you're updating your script outside IPython which could still be running byte-compiled code from a previous version. (although not a big chance)

Can I make ipython exit from the calling code?

I have some code like this:
form IPython import embed
for item in my_item_list:
embed()
If I then run this program with
python my_example_program.py
on the first iteration through the loop I get put into an ipython shell and can inspect item and the environment as I would like to.
On quitting ipython the loop resumes and then I can inspect the next item and the environment as you would expect.
Is there a way for me to quit this code from within ipython (so that I am returned to a shell prompt). in any way short of opening another shell and killing the process?
There's a %kill_embedded command in IPython.
It doesn't put you directly back to the shell prompt, but it skips the other embed instances.
from IPython import embed
for item in range(5):
print 'embedding', item
embed()
And here's the output:
$ python my_example_program.py
embedding 0
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: print item
0
In [2]: ^D
embedding 1
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [2]: %kill_embedded
Are you sure you want to kill this embedded instance (y/n)? [y/N] y
This embedded IPython will not reactivate anymore once you exit.
In [3]: print item
1
In [4]:
embedding 2
embedding 3
embedding 4
$
UPD (06.03.2016): Seems that the %kill_embedded feature is kind of broken in IPython 4.0; you can use %exit_raise which will raise an exception and return back to the shell.

Pylons paster shell does not run in ipython

I install ipython, and then i run
./paster shell dev.ini
command, paster open standard python console. How can I make it run ipython?
Here's how it worked for me on Fedora 17 with IPython 0.12, paste-1.7.5.1 and pylons 1.0:
$ paster shell dev.ini
Pylons Interactive Shell
Python 2.7.3 (default, Jul 24 2012, 10:05:38)
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)]
All objects from project.lib.base are available
Additional Objects:
mapper - Routes mapper object
wsgiapp - This project's WSGI App instance
app - paste.fixture wrapped around wsgiapp
>>> __name__ = '__main__'
>>> import IPython
>>> IPython.embed()
Python 2.7.3 (default, Jul 24 2012, 10:05:38)
Type "copyright", "credits" or "license" for more information.
IPython 0.12 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
Resetting __name__ is necessary because either Pylons/Paste sets __name__ to "pylons-admin" which confuses IPython (it tries to lookup the main module by its name in sys.modules).
I solved this problem downgrading ipython to version 0.10
Did you try the steps from the Pylons Quick Site Development:
13.1.2 Using an IPython embedded shell
IPython provides a more powerfull interactive prompt and a powerful
embedded shell. If you are a Python programmer and have not yet tried
IPython, you definitely should look into it.
First, import from IPython -- Add something like the following at the
top of your controller module, in our case in
firstapp/controllers/firstcontroller.py:
from IPython.Shell import IPShellEmbed args = ['-pdb', '-pi1', 'In
<\#>: ', '-pi2', ' .\D.: ',
'-po', 'Out<\#>: ', '-nosep'] ipshell = IPShellEmbed(args,
banner = 'Entering IPython. Press Ctrl-D to exit.',
exit_msg = 'Leaving Interpreter, back to Pylons.')
Then, place this code in your action/method:
ipshell('We are at action abc')
Return to Pylons and continue on responding to the request by pressing
Ctrl-D.
Note that because of some idiosyncratic feature of
IPython.Shell.IPShellEmbed, I had to put the following before each
call to ipshell():
ipshell.IP.exit_now = False ipshell('We are at action abc')
Is there any chance that your ipython is installed globally, but that you're running pylons from a virtualenv that is --no-site-packages? If that's the case, then pylons won't see your installation of ipython.
Ipython updated to version .11 just recently. Some of the components no longer work correctly, especially with 3rd party libraries. Check the mailing lists for pylons, and you may want to consider a bug report.
Try this:
paster ishell dev.ini

Categories