#!/usr/bin/env python # coding: utf-8 # ## Alt For [How do I construct a python function where the input in python code and output is ipython rich output (in HTML)?](https://stackoverflow.com/q/74683522/8508004), based on making a importable python function # In[1]: s = '''def my_plot_func(): from matplotlib import pyplot as plt plt.plot([1, 2, 3]) plt.savefig('my_plot_via_func.png')''' get_ipython().run_line_magic('store', 's >plot_func.py') # In[2]: cat plot_func.py # In[3]: from plot_func import my_plot_func # In[4]: my_plot_func(); # In[5]: from IPython.display import Image Image("my_plot_via_func.png") # In[ ]: