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

wrong indent in Code Export #520

Closed
Nriver opened this issue Jun 23, 2021 · 5 comments
Closed

wrong indent in Code Export #520

Nriver opened this issue Jun 23, 2021 · 5 comments

Comments

@Nriver
Copy link
Contributor

Nriver commented Jun 23, 2021

Below is the code generated from Code Export for charts.

# DISCLAIMER: 'df' refers to the data you passed in when calling 'dtale.show'

import pandas as pd

if isinstance(df, (pd.DatetimeIndex, pd.MultiIndex)):
	df = df.to_frame(index=False)

# remove any pre-existing indices for ease of use in the D-Tale code, but this is not required
df = df.reset_index().drop('index', axis=1, errors='ignore')
df.columns = [str(c) for c in df.columns]  # update columns to strings in case they are numbers

chart_data = pd.concat([
	pd.Series(df.set_index('created_at').index.to_period('M').to_timestamp(how='end').values, index=df.index, name='created_at|M'),
	df['created_at'],
], axis=1)
chart_data = chart_data.sort_values(['created_at|M'])
chart_data = chart_data.rename(columns={'created_at|M': 'x'})
chart_data_count = chart_data.groupby(['x'])[['created_at']].count()
chart_data_count.columns = ['created_at|count']
chart_data = chart_data_count.reset_index()
chart_data = chart_data.dropna()

import plotly.graph_objs as go

charts = []
charts.append(go.Bar(
	x=chart_data['x'],
	y=chart_data['created_at|count']
)
figure = go.Figure(data=charts, layout=go.Layout({
    'barmode': 'group',
    'legend': {'orientation': 'h'},
    'title': {'text': 'Count of created_at by created_at (Monthly)'},
    'xaxis': {'title': {'text': 'created_at (Monthly)'}},
    'yaxis': {'tickformat': '.0f', 'title': {'text': 'Count of created_at'}, 'type': 'linear'}
}))

# If you're having trouble viewing your chart in your notebook try passing your 'chart' into this snippet:
#
# from plotly.offline import iplot, init_notebook_mode
#
# init_notebook_mode(connected=True)
# chart.pop('id', None) # for some reason iplot does not like 'id'
# iplot(chart)

Executing it will give following exception:

  File "<ipython-input-17-627b899ee227>", line 32
    figure = go.Figure(data=charts, layout=go.Layout({
    ^
SyntaxError: invalid syntax
  1. There are several lines indented with tab instead of spaces and cause syntax error when execute. In above example, there are 5 lines indented with tab.

  2. In the last few lines, variable chart is quite confusing as it does not appear anywhere in the code above. To make the code working, the variable chart should change to figure according to the context.

@aschonfeld
Copy link
Collaborator

@Nriver sorry about that, looks like I'm missing another closing parenthesis on:

charts.append(go.Bar(
    x=chart_data['x'],
    y=chart_data['created_at|count']
))

As for (2), this is a one off scenario where we're trying to render multiple charts in one figure. I'll try and update the comment to the following:

# for chart in charts:
#     chart.pop('id', None) # for some reason iplot does not like 'id'
# iplot(figure)

Looks like I forgot to update the iplot call to use figure

@aschonfeld
Copy link
Collaborator

aschonfeld commented Jun 24, 2021

Fixed in v1.50.0, @Nriver please toss your ⭐ on the repo if you haven't already 🙏

@Nriver
Copy link
Contributor Author

Nriver commented Jun 24, 2021

I can confirm the indent is fixed in 1.50.0. While the iplot(chart) stays the same. May be you missed something for single chart scenario.

And, I have already starred this repo last year :)

@aschonfeld
Copy link
Collaborator

Thanks! Apologies, i’ll make a hotfix tomorrow for other piece

@aschonfeld aschonfeld reopened this Jun 24, 2021
@aschonfeld
Copy link
Collaborator

Just release v1.50.1 with a fix for this issue

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

No branches or pull requests

2 participants