Display Function from with in a Function

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Display Function from with in a Function

John Omernik
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
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Display Function from with in a Function

rossant
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 <[hidden email]>:

> 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
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/ipython-dev
_______________________________________________
IPython-dev mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Display Function from with in a Function

John Omernik
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
<[hidden email]> 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 <[hidden email]>:
>> 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
>> [hidden email]
>> http://mail.scipy.org/mailman/listinfo/ipython-dev
> _______________________________________________
> IPython-dev mailing list
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/ipython-dev
_______________________________________________
IPython-dev mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Display Function from with in a Function

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

2015-01-28 16:34 GMT+01:00 John Omernik <[hidden email]>:

> 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
> <[hidden email]> 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 <[hidden email]>:
>>> 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
>>> [hidden email]
>>> http://mail.scipy.org/mailman/listinfo/ipython-dev
>> _______________________________________________
>> IPython-dev mailing list
>> [hidden email]
>> http://mail.scipy.org/mailman/listinfo/ipython-dev
> _______________________________________________
> IPython-dev mailing list
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/ipython-dev
_______________________________________________
IPython-dev mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-dev