• Home
  • Get started
  • Documentation
  • Source
  • License
  • About
reahl

reahl

Power tools for Python development

Gallery

  • Navigation
  • Form
  • Table
  • Chart
  • Payment
  • Other

Interactive Plotly chart

def create_chart(self, view):

    class ChartForm(Form):
        countries = ['France', 'Switzerland']

        def __init__(self, view):
            super().__init__(view, 'chartform')
            self.other_country = self.countries[0]
            self.use_layout(FormLayout())

            select_input = self.layout.add_input(SelectInput(self, self.fields.other_country))
            chart = self.create_chart(self.other_country)
            select_input.set_refresh_widget(chart.contents)

        def create_chart(self, factor):
            fig = self.create_plotly_figure(factor)
            return self.add_child(Chart(self.view, fig, 'changing-chart'))

        @exposed
        def fields(self, fields):
            fields.other_country = ChoiceField([Choice(i, Field(label=i)) for i in self.countries],
                                               label='Choose country to compare against South Africa')

        def create_plotly_figure(self, other_country):
            # https://en.wikipedia.org/wiki/List_of_mountains_in_South_Africa
            # https://en.wikipedia.org/wiki/List_of_French_mountains_by_prominence
            # https://en.wikipedia.org/wiki/List_of_highest_mountains_of_Switzerland

            france_peaks = [2750, 2217, 2082, 4102, 4808]
            switzerland_peaks = [4545, 4554, 4563, 4609, 4634]
            height_rank_bins = ['5', '4', '3', '2', '1']
            south_africa_peaks = [-1 * i for i in [3375, 3377, 3410, 3451, 3455]]
            if other_country == 'France':
                other_country_peaks = france_peaks
            else:
                other_country_peaks = switzerland_peaks

            fig = go.Figure()
            fig.add_trace(go.Bar(y=height_rank_bins, x=other_country_peaks,
                                 name=other_country, orientation='h'))
            fig.add_trace(go.Bar(y=height_rank_bins, x=south_africa_peaks,
                                 name='South Africa', orientation='h'))

            fig.update_layout(
                title='Peak Elevation Pyramid',
                barmode='relative',
                bargap=0.1,
                bargroupgap=0,
                xaxis=dict(tickvals=[-4000, -3000, -2000, 0, 2000, 3000, 4000],
                           ticktext=['4,000m', '3,000m', '2,000m', '0m',
                                     '2,000m', '3,000m', '4,000m'],
                           title='Peak elevation')
            )

            return fig

    return ChartForm(view)

Copyright © 2006-2023 Reahl Software Services (Pty) Ltd

Running on Reahl