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

Shorthand supports for dash-core-components #1763

Merged
merged 12 commits into from
Sep 21, 2021
Merged

Conversation

workaholicpandaren
Copy link
Contributor

(this PR replaces PR #1747 with one squashed commit)

Implements this issue to support optional shorthand supports for below components.
You can compare old format and new format.

Dependency Update

  • Input Output State can now accept component object as well as string value of the component key
    • (Required) A separate PR will need to be merged before this

Updated Components with change notes

  • Dropdown

    • options props can now be passed without being a keyword argument
    • options can now accept array of string as well as previous format (array of dictionary)
  • RangeSlider

    • Changed props order to make it start with min max step
  • Checklist

    • options props becomes first argument, can be passed without keyword
    • Added inline props to specify if options are array of string or dictionary (previous format)
  • RadioItems

    • options props becomes first argument, can be passed without keyword
    • Added inline props to specify if options are array of string or dictionary (previous format)
  • DataTable

    • data and columns props becomes first 2 arguments, can be passed without keyword
    • (Optional) Separate PR will be followed to extract columns automatically from given data

* update Checklist, Dropdown, RangeSlider to accept optional shorthands

* update Checklist, RadioItems - added inline props and shorthand options data,
update DataTable - params order changed for shorthand support, added TODO

* fill component id if not given

* fix Dropdown props to accept array of string

* remove comments

* fix lint issues

* generating seed for component id

* lint fix in dash-table

* lint fix

* remove inline props and lint fix

* broken eslint 😬

* prettier fix

* set random id inside dash dependency

* lint fixes

* bug fix

* add inline styling support

* fix bugs

* add column populating in dash-table

* disable restricting dependency type

* fix radio options type check

* add comments to inline props

* add tests for shorthands

* remove unused imports

* remove tests, refactor assign

* use ramda.type instead of typeof

* backup commit

* add auto generating marks to RangeSlider & Slider

* add number support for shorthanded options - Checklist, Dropdown

* some refactor to get react-docgen working, slider props re-arrange

* fix unit test - remove outdated part

* update props comment

Co-authored-by: Chris Parmer <chris@plot.ly>

* fix feedback comments

* fix some issues in Checklist, DataTable props

* pylint fix

* Apply suggestions from code review

Co-authored-by: Chris Parmer <chris@plot.ly>

* copy paste ProTypes to get doc-gen working

* Test Slider and RangeSlider shorthand properties

* Test Dropdown shorthand properties

* Remove unnecessary imports

* random seed moved out to global scope, the test for set_random_id was implemented

* fix slider markers - respect steps given

* assert comparision is fixed

* fix slider issues

* Add dropdown option sanitization to some additional required places

* Convert  labels to strings when an Ojbect is passed as options

* Add more variants to Dropdown shorthand test

* Extend Slider tests

* Add test for Dropdown array value type

* fix: update inline description & style

* chore: pass black format

* chore: react doc-gen fix & flake fix

* chore: rename test file names to avoid conflict

* Handle undefined options in Dropdown

* fix: the case when truncated out input marks handled

* fix: correct step calculations implemented for sliders

* fix: removed Start and End prefix / suffix from labels on Slider

* chore: add labels to dropdown tests

* chore: lint fix

* fix: dropdown options for test

* chore: prettier 😪

* bump dash-renderer to v1.11.0
This new version is not important for most dash users, only dash-embedded,
and they get the renderer elsewhere; but as is there's a mismatch
between the local and CDN versions of the renderer

* fix: removed a test file which was causing percy tests fail

* fix: defining emptiness of dictionary implemented correctly, which fixes the disappearing of explicitely given marks

* fix: omit 'step' from props

* chore: update props description

* chore: remove unnecessary prop type checker

* fix: range slider test corrected, formatted to have some margins

* fix: the test_ddsh001_dropdown_shorthand_properties test restored back and the DropDowns propTypes are fixed

* fix: added bool type to CheckList label/value and RadioItem label/value

* fix: the edited JS files are reformatted using lint

* feat: implemented SI Units format for unit values of slider

* feat: added tests to cover slider SI Units format for unit values

* chore: eslint fix

* fix: the test adopted to propsTypes which now accepts bool for several components

* fix: slider numbers whose ten factor is less than 3 and bigger than -3 are not formatted, so that they can have floating numebrs

* fix: lint issue fixed

* fix: explicit null prevents auto generating marks in sliders

* fix: add snapshot for set_auto_id dependency link and asserts

* fix: test marks=None and SI units format

* fix: test_persistence - give step=1 to expect integer value output, otherwise 0.1 will be auto assigned

* fix: checking RadioItems and Checklist to accept new propTypes added in tests

Co-authored-by: workaholicpanda <>
Co-authored-by: Chris Parmer <chris@plot.ly>
Co-authored-by: Szabolcs Markó <akksi@akksi.eu>
Co-authored-by: alexcjohnson <alex@plot.ly>
@workaholicpandaren
Copy link
Contributor Author

workaholicpandaren commented Sep 19, 2021

Changelog

Dash and Dash Renderer

  • Input, State, and Output now accept components instead of ID strings and Dash callback will auto-generate the component's ID under-the-hood if not supplied. This allows usage like:

    my_input = dcc.Input()
    my_output = html.Div()
    app.layout = html.Div([my_input, my_output])
    
    @dash.callback(Output(my_output, 'children'), Input(my_input, 'value'))
    def update(value):
        return f'You have entered {value}'

    Or, if using Python >=3.8 you can use the := walrus operator:

    app.layout = html.Div([
      my_input := dcc.Input(),
      my_output := html.Div()
    ])
    
    @dash.callback(Output(my_output, 'children'), Input(my_input, 'value'))
    def update(value):
        return f'You have entered {value}'

Dash Core Components

Rearranged Keyword Arguments & Flexible Types

Dropdown, RadioItem, and Checklist

  • Rearranged Keyword Arguments - options & value are now the first two keywords which means they can be supplied as positional arguments without the keyword. Supplying the keywords (options= and value=) is still supported.

  • Flexible Types - options can be supplied in two new forms:

    1. An array of string|number|bool where label and value are equal to the items in the list.
    2. A dictionary where the keys and values set as value and label respectively.

Before:

dcc.Dropdown(
  options=[
    {'label': 'New York', 'value': 'New York'},
    {'label': 'Montreal', 'value': 'Montreal'},        
  ],
  value='New York'
)

or

dcc.Dropdown(
  options=[
    {'label': 'New York', 'value': 'NYC'},
    {'label': 'Montreal', 'value': 'MTL'},        
  ],
  value='New York'
)

After:

dcc.Dropdown(['New York', 'Montreal'], 'New York')

Or

  dcc.Dropdown({'NYC': 'New York', 'MTL': 'Montreal'}, 'New York')

RangeSlider & Slider

  • Rearranged Keyword Arugments - min, max, and step are now the first three keyword arguments which means they can be supplied as positional arguments without the keyword.

  • Flexible Types

    • step will be calculated implicitly if not given.
    • marks will be auto generated if not given. It will use min and max and will respect step if supplied. Auto generated marks labels are SI unit formatted. Around 5 human-readable marks will be created.
    • To remove the Slider's marks, set marks=None.

Before:

dcc.Slider(marks={1: 2, 2: 2, 3: 3})

After:

dcc.Slider(min=1, max=3, step=1)

Or equivalently:

dcc.Slider(1, 3, 1)

Step can also be omitted and the Slider will attempt to create a nice, human readable
step with SI units and around 5 marks:

dcc.Slider(0, 100)

The SI units used in marks are:

  • z - zepto, 10⁻²¹
  • a - atto, 10⁻¹⁸
  • f - femto, 10⁻¹⁵
  • p - pico, 10⁻¹²
  • n - nano, 10⁻⁹
  • µ - micro, 10⁻⁶
  • m - milli, 10⁻³
  • (none) - 10⁰
  • k - kilo, 10³
  • M - mega, 10⁶
  • G - giga, 10⁹
  • T - tera, 10¹²
  • P - peta, 10¹⁵
  • E - exa, 10¹⁸
  • Z - zetta, 10²¹

DataTable

  • Rearranged Keyword Arguments - data and columns the first twokeyword arguments which means they can be supplied as positional arguments without the keyword.
  • Inferred Properties - If columns isn't supplied then it is extracted from the the first row in data

Before:

dash_table.DataTable(data=df.to_dict('records'), columns=[{'name': i, 'id': i} for i in df.columns])

After:

dash_table.DataTable(data=df.to_dict('records'))

New Component Properties

Checklist & RadioItems

  • A new property inline appends display: inline-block to labelStyle.
    dcc.Checklist(inline=True)

…_sliders_shorthands.py

Co-authored-by: Chris Parmer <chris@plot.ly>
Co-authored-by: Chris Parmer <chris@plot.ly>
… Slider, RangeSlider, lint issues fixed"

This reverts commit a363691.
Copy link
Member

@chriddyp chriddyp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💃🏼 Great work! Let's do it.

CHANGELOG.md Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants