-
Notifications
You must be signed in to change notification settings - Fork 795
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
Add method-based attribute setting #2795
Conversation
…etion parameter support
…er class docstring when not appropriate
… backported changes that we do not test on older versions
Co-authored-by: Stefan Binder <[email protected]>
Co-authored-by: Jake VanderPlas <[email protected]>
This PR introduces an alternative method-based syntax for setting encoding channel options. How will it workTraditionally, encoding channel options are defined by arguments. For example, below we adjust the y-axis title and increase the step between the x-axis ticks: import altair as alt
from vega_datasets import data
cars = data.cars()
alt.Chart(cars).mark_point().encode(
x=alt.X('Horsepower', axis=alt.Axis(tickMinStep=50)),
y=alt.Y('Miles_per_Gallon', title="Miles per Gallon"),
color='Origin',
shape='Origin'
) In the example above, the The same technique works with all encoding channels and all channel options. For example, notice how we make the analogous change with respect to the import altair as alt
from vega_datasets import data
cars = data.cars()
alt.Chart(cars).mark_point().encode(
x=alt.X('Horsepower').axis(tickMinStep=50),
y=alt.Y('Miles_per_Gallon').title('Miles per Gallon'),
color='Origin',
shape='Origin'
) These option-setter methods can also be chained together, as in the following, in which we set the import altair as alt
from vega_datasets import data
cars = data.cars()
alt.Chart(cars).mark_point().encode(
x=alt.X('Horsepower')
.axis(ticks=False)
.bin(maxbins=10)
.scale(domain=(30,300), reverse=True),
y=alt.Y('Miles_per_Gallon').title('Miles per Gallon'),
color='Origin',
shape='Origin'
) Regarding the implementation
What are the breaking changesThe following breaking change is for practices that I never seen in occurring in the real application code, but mentioned nevertheless.
When you have encoded a channel specification (within a chart/mark/encode) eg as such: x = alt.X(title='my title') And you want to get the content of the defined parameter x.title or x['title'] And it would both return
After this PR this will still work with
That is a breaking change for getting an encoding channel option.
When you have a bare encoded channel specification as such: x = alt.X() And you like set an encoding channel option on this defined encoded channel. You could use both x.title = 'my title' Or: x['title'] = 'my title' After this PR this two approaches will continue to be both possible and when called (
There is thus no breaking change for setting an encoding channel option. Limitations on tab-complete for help and docstringsThe docstrings are defined within the property setters class (in the hidden But when working with chaining this only will work with the first chained method. So when you want to use the docstrings or help utility for your second or third channel options, you have to write it as it is the first: Since the help will not appear when pressing shift+tab on the second chained method: We consider this a limitation of the ipykernel for now and raised this issue: ipython/ipykernel#1065 When one is using another python kernel, such as the XPython Raw kernel through xeus-python, it will present the user with the function signature and docstring from the defined type hints: Even for the chained methods after the first method: Since this limitation can be a bit confusing when people start adopting this approach, we first like to collect feedback from users how this is being received before changing all examples to use this new syntax. Final remarksI would consider this PR a true team-effort. Firstly, thanks to @jakevdp for the initial idea and prototype! Thanks to @joelostblom for putting it on the agenda again & your work on the docstrings. Thanks to @ChristopherDavisUCI for implementing the type hinting for autocompletion and documentation. Thanks to @binste for the review on the docs and finally thanks to all other people that have been involved in the comments. Merging this. |
Wow it is fantastic to see this merged!! 🚀 🥳 Thank you everyone for contributing and working on this over a long period of time 🎆 A true team-effort as Mattijn said above 🙌 |
Hi @binste, great to see you adding better support for type hints in #2846. Have you taken a look at the type hints that were added in this PR, for example these lines? I believe those were added in this commit. This was my first time working with type hints, so if you have more experience with them, it would be great if you double-checked that the results produced here look reasonable. |
Great, thanks a lot @binste, it's very reassuring to have you look it over. |
Based on #1629, #2592, and #2770.