Configure the backend of Ipython to use retina display mode with code - ipython

I am using code to configure Jupyter notebooks because I have a repo with plenty of notebooks and want to keep style consistency across all without having to write lengthy setting at the start of each. This way, what I do is having a method to configure the CSS, one to set up Matplotlib and one to configure Ipython.
The reasons I configure my notebooks this way rather than relying on a configuration file as per docs are two:
I am sharing this repo of notebooks publicly and I want all my configs to be visible
I want to keep these configs specific to just this repo I'm creating
As an example, the method to set the CSS looks like
def set_css_style(css_file_path='../styles_files/custom.css'):
styles = open(css_file_path, "r").read()
return HTML(styles)
and I call it at the start of each notebook with set_css_style(). Similarly, I have this method to configure the specifics of Ipython:
def config_ipython():
InteractiveShell.ast_node_interactivity = "all"
Both the above use imports
from IPython.core.display import HTML
from IPython.core.interactiveshell import InteractiveShell
At the moment, as can be seen, the method to configure Ipython only contains the instruction to make it so that when I type the name of variables in multiple lines in a cell I don't need to add a print to make them all be printed.
My question is how to transform the Jupyter magic command to obtain retina-display quality for figures into code. Such command is
%config InlineBackend.figure_format = 'retina'
From the docs of Ipython I can't find how to call this instruction in a method, namely can't find where InlineBackend lives.
I'd just like to add this configuration line to my config_ipython method above, is it possible?

There is a Python API for this:
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('retina')

Related

What is the full command for gdal_calc in ipython?

I've been trying to use raster calculation in ipython for a tif file I have uploaded, but I'm unable to find the whole code for the function. I keep finding examples such as below, but am unsure how to use this.
gdal_calc.py -A input.tif --outfile=result.tif --calc="A*(A>0)" --NoDataValue=0
I then tried another process by assigning sections, however this still doesn't work (code below)
a = '/iPythonData/cstone/prec_7.tif'
outfile = '/iPythonData/cstone/prec_result.tif'
expr = 'A<125'
gdal_calc.py -A=a --outfile=outfile --calc='expr' --NoDataValue=0
It keeps coming up with can't assign to operator. Can someone please help with the whole code.
Looking at the source code for gdal_calc.py, the file is only about 300 lines. Here is a link to that file.
https://raw.githubusercontent.com/OSGeo/gdal/trunk/gdal/swig/python/scripts/gdal_calc.py
The punchline is that they just create an OptionParser object in main and pass it to the doit() method (Line 63). You could generate the same OptionParser instance based on the same arguments you pass to it via the command-line and call their doit method directly.
That said, a system call is perfectly valid per #thomas-k. This is only if you really want to stay in the Python environment.

Is there auto-import functionality for typescript in Visual Studio Code?

Is there any shortcut that will allow me to auto generate my typescript imports? Like hitting ctrl+space next to the type name and having the import declaration placed at the top of the file. If not, what about intellisense for filling out the module reference path so that I wont have to do it manually? I would really love to use vscode but having to do manual imports for typescript is killing me.
There are rumors of making it support tsconfig.json (well, better than rumors). This will allow us to be able to use all files for our references.
Your feature would be to create an auto import of all commonly used 3rd party libs into the typings. Perhaps auto scan the files and create a list of ones to go gather. Wouldn't it be fine to just have a quick way to add several of these using tsd directly from Code (interactively)?
I believe the plugin called "TypeScript Importer" does exactly what You mean: https://marketplace.visualstudio.com/items?itemName=pmneo.tsimporter .
Automatically searches for TypeScript definitions in workspace files and provides all known symbols as completion item to allow code completion.
With it You can truly use Ctrl+Space to choose what exactly You would like to be imported.
You can find and install it from Ctrl+Shift+X menu or just by pasting ext install tsimporter in Quick Open menu opened with Ctrl+P.
I know a solution for Visual Studio (not Visual Studio Code, I'm using the 2015 Community edition, which is free), but it needs some setup and coding -- however, I find the results to be adequate.
Basically, in Visual Studio, when using the Web-Essentials extension, .ts files can be dragged into the active document to automatically generate a relative reference path comment:
/// <reference path="lib/foo.ts" />
With which of course we might as well wipe it, because it's an import statement we need, not a reference comment.
For this reason, I recently wrote the following command snippet for Visual Commander, but it should be easily adaptable to other use casese as well. With Visual Commander, your drag the needed imports into the open document, then run the following macro:
using EnvDTE;
using EnvDTE80;
using System.Text.RegularExpressions;
public class C : VisualCommanderExt.ICommand
{
// Called by Visual Commander extension.
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
TextDocument doc = (TextDocument)(DTE.ActiveDocument.Object("TextDocument"));
var p = doc.StartPoint.CreateEditPoint();
string s = p.GetText(doc.EndPoint);
p.ReplaceText(doc.EndPoint, this.ReplaceReferences(s), (int)vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers);
}
// Converts "reference" syntax to "ES6 import" syntax.
private string ReplaceReferences(string text)
{
string pattern = "\\/\\/\\/ *<reference *path *= *\"([^\"]*)(?:\\.ts)\" *\\/>";
var regex = new Regex(pattern);
var matches = Regex.Matches(text, pattern);
return Regex.Replace(text, pattern, "import {} from \"./$1\";");
}
}
When running this snippet, all reference comments in the active document will be replaced with import statements. The above example is converted to:
import {} from "./lib/foo";
This has just been released in version 1.18.
From the release notes:
Auto Import for JavaScript and TypeScript
Speed up your coding with auto imports for JavaScript and TypeScript. The suggestion list now includes all exported symbols in the current project. Just start typing:
If you choose one of the suggestion from another file or module, VS Code will automatically add an import for it. In this example, VS Code adds an import for Hercules to the top of the file:
Auto imports requires TypeScript 2.6+. You can disable auto imports by setting "typescript.autoImportSuggestions.enabled": false.
The files attributes in the tsconfig.json file allows you to set your reference imports in your whole project. It is supported with Visual Studio Code, but please note that if you're using a specific build chain (such as tsify/browserify) it might not work when compiling your project.

Autocomplete in wxpython if load from xrc

I am trying to work with xrc resource in wxpython.
It is good but where is one big "no" - there is no autocomplete of wxFrame class loadet from xrc. And other loaded from xrc classes too.
Is this right or I'am doing somthing wgong?
here is the part of code for example:
import wx
from wx import xrc
class MyApp(wx.App):
def OnInit(self):
if os.path.exists("phc.xrc"):
self.res = xrc.XmlResource("phc.xrc")
self.frame = self.res.LoadFrame(None, 'MyFrame')
self.list_box = xrc.XRCCTRL(self.frame, "list_box_1")
self.notebook = xrc.XRCCTRL(self.frame, "Notebook")
self.StatusBar= xrc.XRCCTRL(self.frame, "MFrame_statusbar")
self.list_ctrl= xrc.XRCCTRL(self.frame, "list_ctr_1")
Well, how good the autocomplete function is depends entirely on the editor/IDE that you are using. You didn't specify what you are using to write python scripts, but from personal experience I would say that it is probably true, that there is no autocomplete.
I've used Eclipse/PyDev, Spyder, SPE and PyCharm in the past and they all did not show an ability to autocomplete widgets created with XRC. You could still try to get the Emacs autocomplete for Python to work and try it there, but I doubt it'll work.
I did not find this a particular hindrance, but everyone's different, I guess. Hopefully, that answers your question.
Yes autocomplete wouldn't work here since our code doesn't know what the xrc is going to return. Your code gets to know about the type of variable (in this case, frame) only during runtime.
And, unfortunately/fortunately, we cannot assign 'type' to a variable in Python for the autocompletion to work.
But in Eclipse + PyDev plugin
you can add this statement for autocomplete to work:
assert isinstance(self.frame, wx.Frame)
autocomplete works after this statement.

programmatically add cells to an ipython notebook for report generation

I have seen a few of the talks by iPython developers about how to convert an ipython notebook to a blog post, a pdf, or even to an entire book(~min 43). The PDF-to-X converter interprets the iPython cells which are written in markdown or code and spits out a newly formatted document in one step.
My problem is that I would like to generate a large document where many of the figures and sections are programmatically generated - something like this. For this to work in iPython using the methods above, I would need to be able to write a function that would write other iPython-Code-Blocks. Does this capability exist?
#some pseudocode to give an idea
for variable in list:
image = make_image(variable)
write_iPython_Markdown_Cell(variable)
write_iPython_Image_cell(image)
I think this might be useful so I am wondering if:
generating iPython Cells through iPython is possible
if there is a reason that this is a bad idea and I should stick to a 'classic' solution like a templating library (Jinja).
thanks,
zach cp
EDIT:
As per Thomas' suggestion I posted on the ipython mailing list and got some feedback on the feasibility of this idea. In short - there are some technical difficulties that make this idea less than ideal for the original idea. For a repetitive report where you would like to generate markdown -cells and corresponding images/tables it is ore complicated to work through the ipython kernel/browser than to generate a report directly with a templating system like Jinja.
There's a Notebook gist by Fernando Perez here that demonstrates how to programmatically create new cells. Note that you can also pass metadata in, so if you're generating a report and want to turn the notebook into a slideshow, you can easily indicate whether the cell should be a slide, sub-slide, fragment, etc.
You can add any kind of cell, so what you want is straightforward now (though it probably wasn't when the question was asked!). E.g., something like this (untested code) should work:
from IPython.nbformat import current as nbf
nb = nbf.new_notebook()
cells = []
for var in my_list:
# Assume make_image() saves an image to file and returns the filename
image_file = make_image(var)
text = "Variable: %s\n![image](%s)" % (var, image_file)
cell = nbf.new_text_cell('markdown', text)
cells.append(cell)
nb['worksheets'].append(nbf.new_worksheet(cells=cells))
with open('my_notebook.ipynb', 'w') as f:
nbf.write(nb, f, 'ipynb')
I won't judge whether it's a good idea, but if you call get_ipython().set_next_input(s) in the notebook, it will create a new cell with the string s. This is what IPython uses internally for its %load and %recall commands.
Note that the accepted answer by Tal is a little deprecated and getting more deprecated: in ipython v3 you can (/should) import nbformat directly, and after that you need to specify which version of notebook you want to create.
So,
from IPython.nbformat import current as nbf
becomes
from nbformat import current as nbf
becomes
from nbformat import v4 as nbf
However, in this final version, the compatibility breaks because the write method is in the parent module nbformat, where all of the other methods used by Fernando Perez are in the v4 module, although some of them are under different names (e.g. new_text_cell('markdown', source) becomes new_markdown_cell(source)).
Here is an example of the v3 way of doing things: see generate_examples.py for the code and plotstyles.ipynb for the output. IPython 4 is, at time of writing, so new that using the web interface and clicking 'new notebook' still produces a v3 notebook.
Below is the code of the function which will load contents of a file and insert it into the next cell of the notebook:
from IPython.display import display_javascript
def make_cell(s):
text = s.replace('\n','\\n').replace("\"", "\\\"").replace("'", "\\'")
text2 = """var t_cell = IPython.notebook.get_selected_cell()
t_cell.set_text('{}');
var t_index = IPython.notebook.get_cells().indexOf(t_cell);
IPython.notebook.to_code(t_index);
IPython.notebook.get_cell(t_index).render();""".format(text)
display_javascript(text2, raw=True)
def insert_file(filename):
with open(filename, 'r') as content_file:
content = content_file.read()
make_cell(content)
See details in my blog.
Using the magics can be another solution. e.g.
get_ipython().run_cell_magic(u'HTML', u'', u'<font color=red>heffffo</font>')
Now that you can programatically generate HTML in a cell, you can format in any ways as you wish. Images are of course supported. If you want to repetitively generate output to multiple cells, just do multiple of the above with the string to be a placeholder.
p.s. I once had this need and reached this thread. I wanted to render a table (not the ascii output of lists and tuples) at that time. Later I found pandas.DataFrame is amazingly suited for my job. It generate HTML formatted tables automatically.
from IPython.display import display, Javascript
def add_cell(text, type='code', direct='above'):
text = text.replace('\n','\\n').replace("\"", "\\\"").replace("'", "\\'")
display(Javascript('''
var cell = IPython.notebook.insert_cell_{}("{}")
cell.set_text("{}")
'''.format(direct, type, text)));
for i in range(3):
add_cell(f'# heading{i}', 'markdown')
add_cell(f'code {i}')
codes above will add cells as follows:
#xingpei Pang solution is perfect, especially if you want to create customized code for each dataset having several groups for instance. However, the main issue with the javascript code is that if you run this code in a trusted notebook, it runs every time the notebook is loaded.
The solution I came up with is to clear the cell output after execution. The javascript code is stored in the output cell, so by clearing the output the code is gone and nothing is left to be executed in the trusted mode again. By using the code from here, the solution is the code below.
from IPython.display import display, Javascript, clear_output
def add_cell(text, type='code', direct='above'):
text = text.replace('\n','\\n').replace("\"", "\\\"").replace("'", "\\'")
display(Javascript('''
var cell = IPython.notebook.insert_cell_{}("{}")
cell.set_text("{}")
'''.format(direct, type, text)));
# create cells
for i in range(3):
add_cell(f'# heading{i}', 'markdown')
add_cell(f'code {i}')
# clean the javascript code from the current cell output
for i in range(10):
clear_output(wait=True)
Note that the clear_output() needs the be run several times to make sure the output is cleared.
As a slight update incorporating Tal's answer above, updates from Chris Barnes and a little digging in the nbformat docs, the following worked for me:
import nbformat
from nbformat import v4 as nbf
nb = nbf.new_notebook()
cells = [
nbf.new_code_cell(f"""print("Doing the thing: {i}")""")
for i in range(10)
]
nb.cells.extend(cells)
with open('generated_notebook.ipynb', 'w') as f:
nbformat.write(nb, f)
You can then start up the new artificial notebook and cut-n-paste cells where ever you need them.
This is unlikely to be the best way to do anything, but it's useful as a dirty hack. 🐱‍💻
This worked with the following versions:
Package Version
-------------------- ----------
ipykernel 5.3.0
ipython 7.15.0
jupyter 1.0.0
jupyter-client 6.1.3
jupyter-console 6.1.0
jupyter-core 4.6.3
nbconvert 5.6.1
nbformat 5.0.7
notebook 6.0.3
...
Using the command line goto the directory where the myfile.py file is located
and execute (Example):
C:\MyDir\pip install p2j
Then execute:
C:\MyDir\p2j myfile.py -t myfile.ipynb
Run in the Jupyter notebook:
!pip install p2j
Then, using the command line, go the corresponding directory where the file is located and execute:
python p2j <myfile.py> -t <myfile.ipynb>

Integrate application in GTK+2 with Nautilus 3 (using GTK+3). Is this possible?

I've an application written using PyGTK (GTK+2). I'd like to integrate it with Nautilus via an extension (something I am trying to learn). My current desktop has GNOME3 and Nautilus 3, which is written in GTK+3 and the extensions for Nautilus uses PyGObject.
Can I integrate my application in GTK+2 with Nautilus 3? (without porting my application to GTK+3, yet). Any hint?
I'm planning to port my application to GTK+3 (PyGObject), but it'll require more time than I have now.
Yes, it is possible. For instance, you can use Nautilus to call your program with the files or directories as arguments. The program you are calling can be written with any toolkit, or even be just a shell script.
A tiny example or an extension:
from gi.repository import Nautilus, GObject
from urllib import unquote
PROGRAM_NAME = '/path/to/your/program'
class MyExtension(GObject.GObject, Nautilus.MenuProvider):
def __init__(self):
pass
def call_my_program(self, menu, files):
# Do whatever you want to do with the files selected
if len(files) == 0:
return
# Strip the URI format to plain file names
names = [ unquote(file.get_uri()[7:]) for file in files ]
argv = [ PROGRAM_NAME ] + names
GObject.spawn_async(argv, flags=GObject.SPAWN_SEARCH_PATH)
def get_file_items(self, window, files):
# Show the menu if there is at least on file selected
if len(files) == 0:
return
# We care only files (local files)
for fd in files:
if fd.is_directory() or fd.get_uri_scheme() != 'file':
return
item = Nautilus.MenuItem(name='MyExtensionID::MyMethodID',
label='Do something with my program...')
item.connect('activate', self.call_my_program, files)
return item,
The extension is written using GObject Introspection (Nautilus 3), and it is generic: you can call any external program you want that accepts files as arguments. The key is GObject.spawn_async().
get_file_items is the method that Nautilus call when the user interacts with files. In that, you can bind a contextual menu (with Nautilus.MenuItem()). Then, you connect that menu with the method that calls your program (call_my_program()).
You can create other filters in the method get_file_items. For instance, to show the contextual menu only if there are text plain files selected (using fd.is_mime_type()). You can do whatever you have in mind. Beware of performing only non-blocking operations, otherwise you could block Nautilus.
To test the extension, you can install it in ~/.local/share/nautilus-python/extensions.
Check Introspection Porting:
Note that you can't do a migration halfway: If you try to import both
gtk and gi.repository.Gtk, you'll get nothing but program hangs and
crashes, as you are trying to work with the same library in two
different ways. You can mix static and GI bindings of different
libraries though, such as dbus-python and gi.repository.Gtk.
So, it depends on how the Nautilus plugins are implemented.