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

notebook cells too wide #1

Open
MareoRaft opened this issue Feb 25, 2021 · 1 comment
Open

notebook cells too wide #1

MareoRaft opened this issue Feb 25, 2021 · 1 comment

Comments

@MareoRaft
Copy link

(Hide)

On my capstone page for the Data Incubator (https://hidefusaokabe.github.io/meaningfulmovement/), the code contained in some lines (like line 5) extend outwards to the right.

How can we circumvent this visual appearance?

@MareoRaft
Copy link
Author

activity = [5, 5, 5, 5, 5, ...

The list is too long. But in the end the user is not actually running the notebook. So with the understanding that this is code for presentation, and need not be working code, you could just explain what should be there:

# The correct labels.  It is a list of integers of length 2947
activity = [5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, ...]

(yes put an actual ... in there)

# The predicted labels.  In a perfect world they would be exactly the same as the correct labels above.
predicted = [4, 3, 3, 3, 3, 4, ....]

or if for some reason you feel that you should provide working code, consider instead

# The correct labels.  It is a list of integers of length 2947
from data import activity

and then you will have a separate data.py file in which you define activity = [. I am not super familiar with notebooks but in a regular python script you can certainly import other local python files in this way.


For all the other code which is long, but not super long. For example:

cmap = plt.get_cmap('Pastel1', 6) # Notice that we have a different color map here. Up to you, whatever you like. Don't worry too much about graphic design kinda stuff, though.

    # This gives us a colormap with white and transparent. 
    # That way, if two things are equal, x==y has a value of 1. 
    # Since cmap_correct(1) = transparent, we only draw a white arc when two things are not equal.
    cmap_correct = ListedColormap(['white', [0, 0, 0, 0]])

you can just reformat it to be shorter. Firstly it is recommended to put a comment like "Notice that we have a different..." on its own line. It should be on the line immediately above the code it is commenting. (see the PEP8 styling convention https://pep8.org/).

Comments can be split into multiple lines if necessary

# this is a really really really really really really really really really really really really
# really really really really really really really really long comment, and so i split it
# into 3 separate lines

and code can be split into multiple lines with the \ linebreak character:

aggregated_hide.columns=['segment_length', 'predicted', 'appasionato', 'rambunctuous', \
'inquisitive', 'this', 'list', 'is', 'really', 'long']

You can use \ in whitespace without a problem. Certain things like lists and dictionaries don't even need the \ character at all.

If you want to be really fancy, you can use a tool (like https://pypi.org/project/autopep8/) to help automatically reformat your code for you, although this is not necessary.

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

1 participant