Added plotly_intercept_code configuration option - #24
Open
Davide-sd wants to merge 1 commit into
Open
Conversation
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:
```python
plot(sin(x), (x, -10, 10))
```
The function `plot` returns a custom `Plot` object, containing the Plotly
figure. Inside the `plot` function, 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.
```python
fx = lambda u, v: (4 + np.cos(u)) * np.cos(v)
fy = lambda u, v: (4 + np.cos(u)) * np.sin(v)
fz = lambda u, v: np.sin(u)
plot3d_parametric_surface(fx, fy, fz, ("u", 0, 2 * pi), ("v", 0, 2 * pi),
rendering_kw=dict(
contours={
"z": {"show": True, "start": -1, "end": 1, "size": 1/10,
"width": 1, "usecolormap": True}
},
showscale=False, colorscale="Aggrnyl"),
zlim=(-2.5, 2.5), title="Torus", backend=PB)
```
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:
```python
fx = lambda u, v: (4 + np.cos(u)) * np.cos(v)
fy = lambda u, v: (4 + np.cos(u)) * np.sin(v)
fz = lambda u, v: np.sin(u)
myplot = plot3d_parametric_surface(fx, fy, fz,
("u", 0, 2 * pi), ("v", 0, 2 * pi),
rendering_kw=dict(
contours={
"z": {"show": True, "start": -1, "end": 1, "size": 1/10,
"width": 1, "usecolormap": True}
},
showscale=False, colorscale="Aggrnyl"),
zlim=(-2.5, 2.5), title="Torus", backend=PB, show=False)
myplot.fig
```
Here:
1. I assigned the object returned by the plot command to a variable.
2. I added `show=False` to 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).
3. I added a new command extracting the Plotly figure from my custom object.
Once this modified code is returned, it will be normally processed by this
extension and everything works as expected.
Author
|
@harupy Please let me know if you intend to merge my pull requests and eventually update the package to a new version... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.