[IPython-dev] problem with publish_display_data?

Jonathan Taylor jonathan.taylor at stanford.edu
Wed Jun 13 18:52:31 EDT 2012

On the master branch, I am getting some unexpected behaviour when I try the

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 +

    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) <
            data = self.publish(return_data=True)
            data['text/html'] += '\n<p><h2>Good job!</h2></p>\n'
            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).



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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20120613/056ca176/attachment.html>

More information about the IPython-dev mailing list