[IPython-dev] Display Function from with in a Function

Cyrille Rossant cyrille.rossant at gmail.com
Wed Jan 28 10:38:44 EST 2015


You're welcome! Also have a look at the "raw=True" option to 'display()'.

2015-01-28 16:34 GMT+01:00 John Omernik <john at omernik.com>:
> So I tried this:
>
> from IPython.display import display, display_html
>
>
> html = "<H1>OH MY </H1>"
> def runme(html):
>    display_html(html)
>
> runme(html)
>
> # This produced no results/output Then I tried
> def runme(html):
>    display(html)
>
> runme(html)
>
> # This printed: '<H1> OH MY </H1>'
>
> Which isn't what HTML(html) displayed (it displayed it rendered).  So
> I had a hunch...
>
> h = display_html(html)
>
> type(h)
> NoneType
>
> so then in my function I did this:
>
> def runme(html):
>     h = HTML(html)
>     display(h)
>     print "did it work?"
>
>
> and then runme(html) produced the expected Heading 1 OH MY and the
> printed statement after.... thanks for the pointer in the right
> direction!
>
> John
>
>
>
> On Wed, Jan 28, 2015 at 9:17 AM, Cyrille Rossant
> <cyrille.rossant at gmail.com> wrote:
>> HTML(...) will only display something if it's the last statement of a
>> code cell. This is because IPython always displays the result of the
>> last result of a cell (unless you terminate the line with a semicolon
>> ';').
>>
>> A more reliable way of displaying stuff is to use display() or
>> display_html(). This will work from within functions. See
>> http://ipython.org/ipython-doc/dev/api/generated/IPython.display.html
>> for more details.
>>
>> 2015-01-28 16:08 GMT+01:00 John Omernik <john at omernik.com>:
>>> Greetings all, I am using iPython notebook, I can do this:
>>>
>>> from IPython.display import display, HTML
>>>
>>> h = "<H1>Oh My </H1>"
>>> HTML(h)
>>>
>>> and get the expected result
>>>
>>> But if I do this:
>>>
>>> from IPython.display import display, HTML
>>>
>>> def runme(html):
>>>     HTML(html)
>>>
>>>
>>> h = "<H1>Oh My </H1>
>>> runme(h)
>>>
>>> I get no results.
>>>
>>> I've noticed this is true for other displays as well, how can I add
>>> displays from withi functions?
>>>
>>> Thanks!
>>>
>>> John
>>> _______________________________________________
>>> IPython-dev mailing list
>>> IPython-dev at scipy.org
>>> http://mail.scipy.org/mailman/listinfo/ipython-dev
>> _______________________________________________
>> IPython-dev mailing list
>> IPython-dev at scipy.org
>> http://mail.scipy.org/mailman/listinfo/ipython-dev
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev



More information about the IPython-dev mailing list