-
-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ipython notebook page #32
base: master
Are you sure you want to change the base?
Changes from all commits
a44c334
0852c97
ae04c42
9c1fcb5
e52d466
d555bcf
94b7dd2
8108849
f420b2b
9582e54
dbfff8c
012f815
081f7a5
896b83e
375150d
150e6b8
5a1f1f1
a2b8c1f
20bd62d
c09aa44
c149864
4c33bd7
e7d77e5
7cac6a4
68f5862
4d5db5d
ff8b1f6
7caed87
9f1d58c
0db6c2c
fcae135
61d1d0f
fd1f422
fc5b10f
54bc84d
fae9d1a
b092da1
f158fd5
f824ba7
08e5ae5
d2e5abb
5e9a201
059f3e7
453439a
ed32069
86c8edc
5a73388
d82ddd2
1efc0ac
78388ec
adcb811
e26cdb9
8c04f10
a48d6b6
d5cfce1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
from __future__ import absolute_import | ||
from django.http import HttpResponse | ||
from copy import copy | ||
from app.logic.logic import SymPyGamma | ||
from app.views import eval_card | ||
from HTMLParser import HTMLParser | ||
import json | ||
import urllib2 | ||
import traceback | ||
|
||
#styling for notebook | ||
styling = '''<style> | ||
li{ list-style-type:none; | ||
list-style-position:inside; | ||
margin:0; | ||
padding:0;} | ||
</style>''' | ||
|
||
class Parser(HTMLParser): | ||
def __init__(self): | ||
HTMLParser.__init__(self) | ||
self.cell_input = [] | ||
self.is_cell_input = False | ||
def handle_starttag(self, tag, attrs): | ||
self.is_cell_input = False | ||
if tag == 'div': | ||
for attribute, value in attrs: | ||
if attribute=='class' and value=='cell_input': | ||
self.is_cell_input = True | ||
def handle_data(self, data): | ||
if self.is_cell_input: | ||
self.cell_input.append(data) | ||
|
||
def result_json(request): | ||
|
||
''' IPython notebook format generator. Parses the result set and | ||
converts them into IPython notebook's format. ''' | ||
|
||
notebook_format = { "metadata": {"name": ""}, "nbformat": 3, "nbformat_minor": 0, "worksheets": [{ "cells": [],"metadata": {} }]} | ||
code_cell = {"cell_type": "code", "input": [], "language": "python", "metadata": {}, "outputs": [{ "output_type": "pyout", "text": [], "prompt_number": 1}], "prompt_number": 1 }, | ||
markdown_cell = {"cell_type": "markdown","metadata": {},"source":[]}, | ||
heading_cell = {"cell_type": "heading","level": 3,"metadata": {},"source": []}, | ||
|
||
exp = request.GET.get('i') | ||
exp = urllib2.unquote(exp) | ||
g = SymPyGamma() | ||
result = g.eval(exp) | ||
notebook = copy(notebook_format) | ||
|
||
prompt_number = 1 #initial value | ||
|
||
for cell in result: | ||
title = copy(heading_cell[0]) | ||
title['level'] = 3 | ||
a = str(cell['title']) | ||
title['source'] = [a] | ||
notebook['worksheets'][0]['cells'].append(title) | ||
|
||
if 'input' in cell: | ||
if cell['input'] != None and cell['input'] != '': | ||
inputs = copy(code_cell[0]) | ||
inputs['input'] = [ str(cell['input']) ] | ||
inputs['prompt_number'] = prompt_number | ||
prompt_number = prompt_number + 1 | ||
notebook['worksheets'][0]['cells'].append(inputs) | ||
|
||
if 'output' in cell: | ||
output = copy(markdown_cell[0]) | ||
|
||
if 'pre_output' in cell: | ||
if cell['pre_output'] !="" and cell['pre_output'] != 'None': | ||
pre_output = copy(markdown_cell[0]) | ||
pre_output['source'] = ['$$'+str(cell['pre_output'])+'=$$'] | ||
notebook['worksheets'][0]['cells'].append(pre_output) | ||
|
||
if cell['output'] != "" and 'script' in cell['output']: | ||
output['source'] = [cell['output']] | ||
notebook['worksheets'][0]['cells'].append(output) | ||
elif 'div' in cell['output']: | ||
output['source'] = [cell['output']] | ||
notebook['worksheets'][0]['cells'].append(output) | ||
|
||
if 'card' in cell: | ||
if cell['card'] != 'plot': | ||
card_name = cell['card'] | ||
variable = cell['var'] | ||
parameters = {} | ||
if 'pre_output' in cell: | ||
if cell['pre_output'] != '' and cell['pre_output'] != 'None': | ||
cell_pre_output = copy(markdown_cell[0]) | ||
cell_pre_output['source'] = ['$$'+str(cell['pre_output'])+ '=$$'] | ||
notebook['worksheets'][0]['cells'].append(cell_pre_output) | ||
|
||
try: | ||
card_json = g.eval_card(card_name, exp, variable, parameters) | ||
card_json_output = card_json['output'] | ||
|
||
parser = Parser() | ||
parser.feed(card_json_output) | ||
parsed_cell_inputs = parser.cell_input #list of data values with <div class='cell_input'> | ||
card_json_output = [card_json_output] | ||
|
||
for card_cell_input in parsed_cell_inputs: | ||
if card_cell_input != '\n' and card_cell_input != '</div>' and card_cell_input != '\\': | ||
|
||
card_json_output = card_json_output[0].split(card_cell_input) | ||
try: #removing <ul>'s, <li>'s and <div>'s | ||
if card_json_output[0][:7] == '</div>\n': | ||
card_json_output[0] = card_json_output[0][7:] | ||
if card_json_output[0][:7] == '\n</div>': | ||
card_json_output[0] = card_json_output[0][7:] | ||
if card_json_output[0][:10] == '<ul>\n<li>\n': | ||
card_json_output[0] = card_json_output[0][10:] | ||
if card_json_output[0][:9] == '<ul>\n<li>': | ||
card_json_output[0] = card_json_output[0][19:] | ||
if card_json_output[0][-24:] == '<div class=\"cell_input\">': | ||
card_json_output[0] = card_json_output[0][:-24] | ||
if card_json_output[0][-12:] == '\n</li>\n<li>\n': | ||
card_json_output[0] = card_json_output[0][:-12] | ||
if card_json_output[0][-11:] == '</li>\n<li>\n': | ||
card_json_output[0] = card_json_output[0][:-11] | ||
except: | ||
pass | ||
card_result = copy(markdown_cell[0]) | ||
card_result['source'] = [card_json_output[0]] | ||
notebook['worksheets'][0]['cells'].append(card_result) #storing output after input | ||
|
||
if card_cell_input[:1] == '\n': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two conditionals can just be replaced with |
||
card_cell_input = card_cell_input[1:] | ||
if card_cell_input[-1:] == '\n': | ||
card_cell_input = card_cell_input[:-1] | ||
|
||
card_heading = copy(code_cell[0]) | ||
card_heading['input'] = [card_cell_input] | ||
card_heading['prompt_number'] = prompt_number | ||
prompt_number = prompt_number + 1 | ||
notebook['worksheets'][0]['cells'].append(card_heading) | ||
|
||
if len(card_json_output) > 1 : | ||
card_json_output = [card_json_output[1]] | ||
else: | ||
card_json_output = [card_json_output[0]] | ||
|
||
if card_json_output[0] != '<': | ||
try: | ||
if card_json_output[0][:7] == '</div>\n': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, this would be more robust with either regex or an actual HTML parser. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't forget that you can't parse HTML with regex. |
||
card_json_output[0] = card_json_output[0][7:] | ||
if card_json_output[0][:10] == '<ul>\n<li>': | ||
card_json_output[0] = card_json_output[0][10:] | ||
if card_json_output[0][:9] == '<ul>\n<li>': | ||
card_json_output[0] = card_json_output[0][9:] | ||
if card_json_output[0][-12:] == '\n</li>\n</ul>': | ||
card_json_output[0] = card_json_output[0][:-12] | ||
if card_json_output[0][-11:] == '</li>\n</ul>': | ||
card_json_output[0] = card_json_output[0][:-11] | ||
|
||
|
||
except: | ||
pass | ||
card_last_result = copy(markdown_cell[0]) | ||
card_last_result['source'] = [card_json_output[0]] | ||
notebook['worksheets'][0]['cells'].append(card_last_result) | ||
except: | ||
card_error = copy(markdown_cell[0]) | ||
card_error['source'] = [traceback.format_exc(1)] | ||
notebook['worksheets'][0]['cells'].append(card_error) | ||
else: | ||
card_plot = copy(markdown_cell[0]) | ||
card_plot['source'] = ['Plotting is not yet implemented.'] | ||
notebook['worksheets'][0]['cells'].append(card_plot) | ||
|
||
if 'cell_output' in cell: | ||
if cell['cell_output'] != "": | ||
cell_output = copy(markdown_cell[0]) | ||
cell_output['source'] = [cell['cell_output']] | ||
notebook['worksheets'][0]['cells'].append(cell_output) | ||
|
||
if 'error' in cell: | ||
cell_error = copy(markdown_cell[0]) | ||
cell_error['source'] = [cell['error']] | ||
notebook['worksheets'][0]['cells'].append(cell_error) | ||
|
||
else: | ||
pass | ||
|
||
#styling for the notebook (Css) | ||
notebook_styling = copy(markdown_cell[0]) | ||
notebook_styling['source'] = [styling] | ||
notebook['worksheets'][0]['cells'].append(notebook_styling) | ||
#Converting it into the json format. | ||
notebook_json = json.dumps(notebook) | ||
response = HttpResponse(notebook_json, content_type = 'text/plain') | ||
#uncomment the following line to display json rather than downloading it. | ||
response['Content-Disposition'] = 'attachment; filename=gamma.ipynb' | ||
return response |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,20 @@ <h1><a href="/"> | |
{{ form.i }}<input class="input_field" type="submit" value="=" /> | ||
</form> | ||
</div> | ||
|
||
<div class="result"> | ||
{% for cell in result %} | ||
{% show_card cell input %} | ||
{% endfor %} | ||
<div class="foot"> | ||
See what <a class="wolfram" | ||
<p>See what <a class="wolfram" | ||
href="http://www.wolframalpha.com/input/?i={{input|urlencode}}"> | ||
Wolfram|Alpha</a> has to say. | ||
Wolfram|Alpha</a> has to say.</p> | ||
<p>See the results as IPython <a class="notebook" | ||
href="http://nbviewer.ipython.org/url/sympygamma.com/export_notebook//%3Fi%3D{{ encoded_export_notebook }}"> | ||
Notebook Viewer</a> or export to an IPython Notebook <a href="{% url app.notebook.result_json %}?i={{ export_notebook }}">file</a></p> | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would word this as "See these results in IPython Notebook Viewer or export to an IPython Notebook file.", just to make it clear which is which. |
||
|
||
<p>{{ promote_live|safe }}</p> | ||
</div> | ||
</div> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be better of with a regex or with an actual HTML parser. If the output ever changes, this would break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am already using an htmlparser (see top) and i think "replace" should do the trick, also I dont want to increase the complexity of the code. I know its silly to discuss these little points, but am I right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I'm trying to determine the intent of this section of code. It is trying to extract the input and output sections from a multiple-result card, correct? I think there are better ways to handle this. See https://gist.github.com/lidavidm/37a0a27582805872334d - it just uses the HTML parser and extracts both the input expression and output LaTeX.