Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify code for exported Altair vis code to use inspect.getsource #37

Open
dorisjlee opened this issue Jul 18, 2020 · 1 comment
Open
Labels
enhancement New feature or request

Comments

@dorisjlee
Copy link
Member

dorisjlee commented Jul 18, 2020

Currently, the to_Altair functionality relies on chart.code, which is manually created via string concatenation. This approach is hard to maintain across changes. We can rewrite this by leveraging the inspect.getsource function to obtain the exported code.
For example:

def change_color_add_title(chart):
    chart = chart.configure_mark(color="green") # change mark color to green
    chart.title = "Custom Title" # add title to chart
    return chart

import inspect
print (inspect.getsource(change_color_add_title))

image

@westernguy2
Copy link
Contributor

The main issue in finding a solution here is that there are many functions being called to generate the final code being outputted by to_Altair, and so string concatenation works well to combine them all. I came up with a few potential solutions:

  • Combine all the functions into one function
  • Define a function using the string generated and use inspect.getsource to extract the body of the function in to_Altair.

The first option probably won't work because the functions are spread across AltairChart and its different subclasses.

The second option is not possible because defining a function using a string (using the Python compile function) invalidates any attempt to use inspect.getsource. This happens because the string used to define the function is thrown away by Python, and so inspect.getsource has no reference to extract from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants