Added plotly_intercept_code configuration option #24
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are use cases where a Plotly figure is being generated by a user defined class and shown through one of the class methods. For example, one of the commands in my module might be:
The function
plotreturns a customPlotobject, containing the Plotly figure. Inside theplotfunction, the Plotly figure will be shown before the return statement.With the current implementation of this extension it is impossible to visualize the resulting plot. Plotly doesn't have something similar to Matplotlib's figure managers, which "intercepts" the figures being shown at a lower level.
Solution: create a new configuration option,
plotly_intercept_code, which will eventually be set to a user-defined function, accepting the current code-block being processed and returning a modified code. The user can modify the code with regex or ast to extract the Plotly figure from the custom objects. Then, the current implementation will be able to continue successfully.Consider the following code-block example contained into a docstring. If a user executes it, it will show a Plotly figure.
If this extension executes the above code block, it will open the plot on a new browser window, but it won't be able to include the figure in the docs, because it doesn't know where the figure is actually stored.
With this commit, the extension processes this code block, send it to the "intercept code function", where I can use the ast module to modify the code to:
Here:
show=Falseto the plot command, so no figure will be shown on the browser (which is annoying if you have dozen of figures opening every time you build the docs).Once this modified code is returned, it will be normally processed by this extension and everything works as expected.