How to separate and run the code part of an Ipython notebook? - web-services

I have an IPython file. I want to take all the code-parts of the Ipython file and run it separately to display all plots and figures.(All the code-part is refering to the fact that a ipython file has both markdown and code snippets and I want only code part.) How can this be done?
I have tried to take the code and paste it in jupyter console but it won't still display images. For example I have
from IPython.display import Image
Image('http://jakevdp.github.com/figures/mpl_version.png')
It returns < IPython.core.display.Image object > but not the actual image. How do I get to display the actual image? Finally, I want to implement this as a web service so that users can upload .ipynb files and get all the output plots of the python codes as output.

Related

How to render LaTeX in local Gorilla worksheet

I am trying to embed LaTeX formulas inside Markdown cell in a locally running Gorilla worksheet. For example, the following Markdown text should render an inline formula and a standalone formula:
This is an inline formula, ##\sin(x)##, and this is on its own line:
$$\int_0^{2\pi}\sin^2(x) \textrm{d}x$$
However, when I run Gorilla locally (using lein gorilla) what I see is the raw code. Markdown itself is rendered absolutely fine. On the other hand, when pushing the worksheet to GitHub and using an online viewer LaTeX formulas are rendered as expected.
Gorilla's documentation is saying the following, which I'm not sure I fully understand:
Note: currently you will need to be online in order for LaTeX to render properly.
What I thought is that it means that I have to have a network connection while trying to render LaTeX, but, if this is indeed the meaning, than I do not understand why it is not working. Another meaning I could think about is that currently LaTeX can only be rendered with the online viewer, but this interpretation dose not make much sense to me.
Any ideas? Thanks to any one pointing me to the right direction!
With some help I have found the solution to this problem—I was using an outdated version of lein-gorilla plugin. With version 0.5.3 LaTeX is rendered just fine[1]. The exact plugins line in project.clj should be similar to
:plugins [[org.clojars.benfb/lein-gorilla "0.5.3"]]
Also, it seems I was not aware that I could use the app-gorilla template for creating gorilla projects (I was using lein new app and then manually edited project.clj to add the plugins line). The command
$ lein new gorilla-app <project-name>
generates a project.clj file with the right version of the lein-gorilla plugin already in the file.
I guess this is the right way for creating gorilla apps, and that it ensures that the plugin will always be up to date.
[1] Some very specific LaTeX formulas seems to have rendering problems, but it needs some more testing and maybe a separate question.

Python Jupyter nbconvert limiting printed pages

I am running Jupyter with a Python 2.7 kernel and I was able to get
nbconvert to run on both the command line (ipython nbconvert --to pdf file.ipynb) as well as through the jupyter browser interface.
For some reason, however, my file only prints 4 out of the 8 total pages I see on the browser. I can convert an html preview (generated by Juypter) file, and then use an online converter at pdfcrowd.com to get the full pdf files printed.
However, the online converter is lacking in latex capability.
When I installed nbconvert, I did not manually install MikTex latex for windows, as I already had it successfuly installed for Rstudio and Sweave.
I didn't want to mess with the installation.
I printed a few longer files (greater than 4 pages), without latex and a few that had latex and printed the first couple of pages fine. But that last few pages always seem to get cut off from the pdf output.
Does anyone know if that might be causing it to limit the print output, or any other ideas why?
Also, I do notice the command line prompt where jupyter is launched has a few messages...
1.599 [\text{feature_matrix}
] =
?
! Emergency stop.
<inserted text>
1.599 [\text{feature_matrix}
] =
Output written on notebook.pdf (r pages)
[NbConvertApp] PDF successfully created
I ran another file and saw the output showed
!you can't use 'macro parameter character #,' in restricted horizontal mode.
...ata points }}{\mbox{# totatl data points}}
?
! Emergency stop
Look like it is somehow choking on using the latex character #
then halting publishing. But it at least publishes all the pages prior to the error and emergency stop.
While not a solution, I manually changed the latex symbol # to number and
the entire file printed. I wonder if this is a problem with my version of latex maybe?
halted pdf job
$$\mbox{accuracy} = \frac{\mbox{# correctly classified data points}}{\mbox{# total data points}}$$
passed pdf job
$$\mbox{accuracy} = \frac{\mbox{number correctly classified data points}}{\mbox{number total data points}}$$
edit: if it help anyone, I used the suggestion here, to use a backslash escape character before the #, to solve the issue; so it looks like it is a latex issue, if that helps anyone.

Modify an image on Google App Engine using PIL Library

I am currently trying to deploy a python(2.7) application on the GAE, but I seem to have run into a wall doing so.
In the application I need to apply some transformations on an image's data which I have retrieved in the form of an array of bytes. I proceeded as pointed out by the OP of this question. Everything seems to go fine, until I wish to retrieve back the image's data from the PIL.Image object. The code is:
def transform_image(im_data,ratio):
im = Image.open(BytesIO(im_data))
w,h = im.size
im = im.crop((0,0,w,h-20))
new_b_io = BytesIO()
im.save(new_b_io,format='JPEG')
im.close()
return new_b_io.getvalue()
#I write this data to a new '.jpg' file on my GCS bucket.
Looking at my GAE application logs, the exception being raised is:
UnsupportedOperation: fileno
This is an identified bug with the PIL version 1.1.7 (the only PIL version available with GAE), as pointed out here.
Looking everywhere, all I could manage to understand is to use a more latest version of PIL (preferably Pillow), but as one would imagine, our friends at Google haven't made that an option. I could move to the Google's Image Processing API, but I'd appreciate if I could make my way through with PIL.
I'd appreciate if someone could walk me through this dilemma.
Thanks for your time.

Integrate powercli commands with Python scripts

When we run commands on power cli it displays the operations we have performed.
For example
Start-VM –VM “VM1”
simply starts the VM in the v center.
I want to write such code in Python that we can call these commands in code and store the output and display to the user.
Is there any way to link our Python code with power cli commands or we can say can we bind power cli code inside Python?
You want two things: dot source the script (which is (as far as I know) similar to python's import), and subprocess.call.
import subprocess
subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./SamplePowershell\";", "&hello"])
So what happens here is that we start up powershell, tell it to import your script, and use a semicolon to end that statement. Then we can execute more commands, namely, hello.
You also want to add parameters to the functions, so let's use the one from the article above (modified slightly):
Function addOne($intIN)
{
Write-Host ($intIN + 1)
}
and then call the function with whatever parameter you want, as long as powershell can handle that input. So we'll modify the above python to:
import subprocess
subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./SamplePowershell\";", "&addOne(10)"])
this gives me the output:
PowerShell sample says hello.
11
You'll need to edit the above to include the PowerCLI library, but it should work.

Under linux, how to run application via .html

I am working on a image processing project. I am not familiar with html. Here is what I think.
My C++ application is able to read an image and write the image to file after processing. The procedural is that user can click mouse in a fixed region of my web, and the position data could be passed as parameter to my application, and then my C++ application will use the position data to process the image and output image to file, finally my web display the image.
So is that possible to implement this?
I'm afraid it's not possible only with HTML.
It should be possible with any server-side scripts written in PHP (for example). Anyway, you can make you program to watch folder uploaded and processed images save into another folder. You will need PHP or something like this though.