[IPython-dev] problem with publish_display_data?

MinRK benjaminrk at gmail.com
Wed Jun 13 19:08:27 EDT 2012


On Wed, Jun 13, 2012 at 3:52 PM, Jonathan Taylor <
jonathan.taylor at stanford.edu> wrote:

> On the master branch, I am getting some unexpected behaviour when I try
> the following
>
> import numpy as np
> from IPython.core.displaypub import publish_display_data
>
> class NormalMean(object):
>     """
>     Sample from N(mean, sd) independently n times
>     """
>     tol = 5.e-2
>     checkable = True
>
>     def __init__(self, mean, sd, n, identifier):
>         self.mean = mean
>         self.identifier = identifier
>         self.sd = sd
>         self.n = n
>         self.sequence = self.generate()
>         self.correct_answer = np.mean(self.sequence)
>
>     def generate(self):
>         return np.round(np.random.standard_normal(self.n), 1) * self.sd +
> self.mean
>
>     @property
>     def answer(self):
>         return np.mean(self.sequence)
>
>     def publish(self, return_data=False):
>
>         data = {'text/latex': r"What is the sample mean, $\bar{X}$ of this
> sequence: %s ?" % `["%0.1f" % s for s in self.sequence]`,
>                 'text/html': ''}
>
>         data['text/html'] += '<form method="post" name=%s ><p><input
> type="text" ></p></form>' % self.identifier
>         data['application/json'] = {'constructor_info': ('normal_mean',
> [self.mean, self.sd, self.n], {})}
>         publish_display_data("NormalMean", data)
>         if return_data:
>             return data
>
>     def check_answer(self, answer):
>         if np.fabs(answer - self.correct_answer) / np.std(self.sequence) <
> self.tol:
>             data = self.publish(return_data=True)
>             data['text/html'] += '\n<p><h2>Good job!</h2></p>\n'
>         else:
>             self.sequence = self.generate()
>             data = self.publish(return_data=True)
>             data['text/html'] += '\n<p><h2>Try Again!</h2></p>\n'
>
>
> NormalMean(10, 3, 6, 'mean_10_sd_3_n_6').publish()
>
>
> I expect to see a latex rendering of the question with a textbox below it
> but I am only getting a text box.
>
> By the way, if I want to see these things in order, I suppose it is best
> to use several calls to publish_display_data? Is that right, if so the
> outputs are in the order I want them to be rather than
> the order of the dict keys (which I cannot control).
>

Right - one call to "publish_display_data" should always result in exactly
one thing being displayed.  The idea being "I have a thing I want to
display, and I can display in all these different ways". It is up to the
frontends to pick their favorite (HTML in the notebook).  If you want to
display multiple things, then you should use multiple calls.

-MinRK


>
> Thanks,
>
> Jonathan
>
>
> --
> Jonathan Taylor
> Dept. of Statistics
> Sequoia Hall, 137
> 390 Serra Mall
> Stanford, CA 94305
> Tel:   650.723.9230
> Fax:   650.725.8977
> Web: http://www-stat.stanford.edu/~jtaylo
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20120613/2a3056d8/attachment.html>


More information about the IPython-dev mailing list