5

The following snippet will produce the output below in Jupyter:

display(HTML('<h2>Hello, world!</h2>'))

enter image description here

Running the same snippet in an IPython console in Spyder only returns <IPython.core.display.HTML object> like this:

enter image description here

Is it possible to display the same output in an IPython console using Spyder? I thought I would get somewhere with from IPython.core.display import display, HTML as mentioned here, but I may be missing the point completely.

Thank you for any suggestions!

vestland
  • 55,229
  • 37
  • 187
  • 305

3 Answers3

7

(Spyder maintainer here) The Spyder IPython console doesn't support html output, so the above code doesn't work on it.

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124
  • Thank you for your feedback! Just to clarify; do you mean the snippet I provided, or the suggestion from Igaud? – vestland Nov 02 '17 at 21:03
  • 1
    The suggestion from @lgaud is about `spyder-notebook`, a plugin that runs the Jupyter notebook inside Spyder. – Carlos Cordoba Nov 02 '17 at 22:18
  • 1
    Is there at least a simple way to catch the object and put it in a matplotlib.pyplot.figure ? – Keith Sep 14 '18 at 16:52
  • @Keith, if you're referring to having dedicated panel for Maptlotlib plots, that's already implemented and it'll be part of Spyder 4. You can look at a preview [here](https://github.com/spyder-ide/spyder/pull/6430). – Carlos Cordoba Sep 14 '18 at 17:00
  • 1
    I was hoping for something more generic. I am using Canopy. For example eli5.show_weights() produces a IPython.core.display.HTML object and I want it in a matplotlib figure. Would be happy to go through a png to get there – Keith Sep 14 '18 at 17:11
  • Well, this question is about Spyder and Html ouput, so you should ask another question. – Carlos Cordoba Sep 15 '18 at 00:53
  • 2
    I have been wondering why both Spyder and PyCharm still use (mainly) text console. Wouldn't it be possible to use Qt WebEngine to have all output displayed by _repr_html_() when available? This is with regular .py files, without Jupyter notebooks. – Tronic Apr 22 '19 at 15:50
5

There is a plugin for Spyder which integrates notebooks: Spyder-Notebook (I have not used this). Pycharm also has an integration.

lgaud
  • 2,430
  • 20
  • 30
1

An alternative idea would be to store the output of the permutation feature importance into an html file and open it with the default browser. I got the idea from J Hudok in another thread. The following is my working example

from sklearn.datasets import load_iris
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
import eli5
from eli5.sklearn import PermutationImportance
from sklearn.model_selection import train_test_split
import webbrowser

# Load iris data & convert to dataframe
iris_data = load_iris()
data = pd.DataFrame({
    'sepal length': iris_data.data[:,0],
    'sepal width': iris_data.data[:,1],
    'petal length': iris_data.data[:,2],
    'petal width': iris_data.data[:,3],
    'species': iris_data.target
})
X = data[['sepal length', 'sepal width', 'petal length', 'petal width']]
y = data['species']

# Split train & test dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

# Initialize classifier
clf = RandomForestClassifier(n_estimators=56, max_depth=8, random_state=1, verbose=1)
clf.fit(X_train, y_train)

# Compute permutation feature importance
perm_importance = PermutationImportance(clf, random_state=0).fit(X_test, y_test)

# Store feature weights in an object
html_obj = eli5.show_weights(perm_importance, feature_names = X_test.columns.tolist())

# Write html object to a file (adjust file path; Windows path is used here)
with open('C:\\Tmp\\Desktop\iris-importance.htm','wb') as f:
    f.write(html_obj.data.encode("UTF-8"))

# Open the stored HTML file on the default browser
url = r'C:\\Tmp\\Desktop\iris-importance.htm'
webbrowser.open(url, new=2)