Closed helloooideeeeea closed 3 years ago
There's nothing in your code that would actually go towards capturing the output. What are you basing this code on?
@MrMino Thanks. Your Answer helped me ! But My question was wrong.
on Matplotlib inline mode, How does JupterNotebook publish graph png image ?..
on jupter kernel sever(ipython), does it exec plt.savefig('x.png')
?..
I close the issue once.
@helloooideeeeea See https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display
@MrMino Thanks! I got pyplot image data. I might be able to Capture this output to KIVY!
from IPython.terminal.interactiveshell import TerminalInteractiveShell
shell = TerminalInteractiveShell.instance()
from pandas import DataFrame
from matplotlib.collections import PathCollection
formatter = shell.display_formatter.formatters['text/plain']
def dataframe_formatter(object, printer, cycle):
printer.text('test')
def pyplot_formatter(object, printer, cycle):
import io
import base64
# object.figure.savefig('test.png')
data = io.BytesIO()
object.figure.savefig(data)
base64Text = base64.b64encode(data.getvalue()).decode()
printer.text(base64Text)
formatter.for_type(DataFrame, dataframe_formatter)
formatter.for_type(PathCollection, pyplot_formatter)
block='''
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from pandas import Series, DataFrame
from sklearn.datasets import load_boston
boston = load_boston()
DataFrame(boston.data, columns=boston.feature_names)
x_org, yt = boston.data, boston.target
feature_names = boston.feature_names
x_data = x_org[:,feature_names == 'RM']
x = np.insert(x_data, 0, 1.0, axis=1)
plt.scatter(x[:,1], yt, s=10, c='b')
'''
a = shell.run_cell(block)
Terminal Output
Out[1]: iVBORw0KGgoAAAANSUhEUgAAAbAAAAEgCAYAAADVKCZpAAAAOXRF....
<Figure size 432x288 with 1 Axes>
I want to capture stdout as variable with IPython. but, following code does not work.
Terminal Output
I want to capture Figure Class like following (output to stdout ). <Figure size 432x288 with 1 Axes>
How do I capture Figure class as variable ?
thanks.