How to build a function in Python code that takes IPython rich output (HTML format) as output?
P粉713846879
P粉713846879 2023-08-31 21:59:28
0
1
661
<p>How to construct a function called <code>foo()</code> such that </p> <pre class="brush:php;toolbar:false;">pycode = ''' from matplotlib import pyplot as plt plt.plot([1, 2, 3]) plt.show() ''' out = foo(pycode)</pre> <p>where<code>out</code> is the rich text HTML output that ipython or jupyter notebook will output</p> <p>Is there a built-in function in ipython that can achieve this? I guess there should be, but I can't find it on Google. </p> <p>EDIT: I want this function to exist within my Python script, not when running the ipython shell. </p> <p> EDIT: What I want to do is basically recreate something similar to a jupyter notebook cell, I give it the code and it returns the rich output (probably in HTML) in a visual way, just like the jupyter notebook cell Same format. </p>
P粉713846879
P粉713846879

reply all(1)
P粉486138196

I think you are looking for Python's exec() method?

In JupyterLab with matplotlib installed, you can run the following code in a cell and see the output below it:

pycode = '''
from matplotlib import pyplot as plt
plt.plot([1, 2, 3])
plt.show()
'''
out = exec(pycode)

Please keep in mind that exec() and the related eval() are generally not recommended. Their use can cause serious problems by allowing other people or even yourself to inadvertently execute code you don't want to run. If you use the correct method, you usually don't need to use them, you can refer to here. (It now seems even possible to execute using WebAssembly-based Python, see PyScript: is it possible to execute an uploaded Python script?.)


Updated based on response to first comment:

If written like this, although the code can draw graphics, out does not collect anything because exec(pycode) happens to be on the last line. RichOutput can be collected by using IPython's capture tool. And, since it's implemented via imports, it should work if you also have the Jupyter ecosystem installed in Python. In other words, while I'm demonstrating it in the notebook, you can write a function that uses return captured.outputs[0] and it will return the RichOutput of the plot.

In this notebook elaborates on the underlying principles.

Please note that since I am dealing with RichOutput and generating images, you will also need to look at the references in it. There's a lot more to it that I haven't covered.


Create a simple function without using exec()

In this example notebook, you can see how to programmatically create a .py file. Normally you can create such a file in a text editor by manually saving the text, for example saving the following as plot_func.py:

def my_plot_func():
    from matplotlib import pyplot as plt
    plt.plot([1, 2, 3])
    plt.savefig('my_plot_via_func.png')

Then import and run it. During this process, an image file is also saved. In the notebook, the image file my_plot_via_func.png will be displayed.

This is just a simple version. The process of importing and executing functions would also work in pure Python from the terminal or elsewhere, until the image file is displayed. If you return plot results as graphics objects (graphs), you need a way to display them. This works in Jupyter, but not in pure Python.

I added this section because I'm still unclear on what you want to do. I also have a more advanced version that uses specially organized Pandas dataframes to generate specialized plots. To try it, click here, then launch binder, and follow the sample notebook that appears at the beginning of the session. Maybe this will help you express your needs better. Especially since it generalizes all the hardcoded stuff like in this drawing script into a command line script or function that can be called. You'll see the necessary infrastructure I talk about in my comments below. Command line scripts can also be run in pure Python. You'll see that I typically use %run in notebooks because it's more comprehensive; however, in notebooks, if you can sacrifice better output handling and don't need to run the script in the notebook namespace, Then you can usually replace !python with %run, as %run -i allows.

For an example of using matplotlib plotting to generate code that returns a specific plot, see here.


Generate HTML

The original question was about HTML, I've added a section that generates HTML at the bottom of this main example notebook, in response to the first comment. Actually, this post "Embedding matplotlib graphics in iPython HTML" probably covers most of the original question?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template