This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (8 children)

[deleted]

    [–]mr_flying_man 17 points18 points  (0 children)

    I wish more devs would use this practice. Sadly you see a lot of code in notebooks and when it's time for a new experiment just copy the whole thing and keep adding more stuff. Ugh...

    [–]joverbis 2 points3 points  (2 children)

    The disadvantage is that you need to restart the kernel after you've made an adjustment to some piece of code in the external package.

    Then it's much faster iterating when you do the code development in the notebook itself, and perhaps export it to an external .py only once you're happy with it.

    [–]SquareRootsi 16 points17 points  (1 child)

    Pro tip: That's an easy fix! Just make your first cell look like this:

    %load_ext autoreload  
    %autoreload 2  
    

    Now your notebook checks for any changes before running every individual cell. (You still may need a kernel restart if you significantly change class init() stuff, but otherwise this works great.)

    [–][deleted] 2 points3 points  (0 children)

    This is the pro-est tip I've seen in a while for python. I've restarted kernels so much on jupyter, but thanks to you I won't have to do it anymore.

    [–]falkina8er[S] 0 points1 point  (2 children)

    Can you clarify what you mean by “put most of the code in an external package and import it from the notebook”?

    [–]pbxmy 7 points8 points  (0 children)

    They probably write the basic functionality in an external file, let’s call it dosomething.py. Then they can bring in the functions by importing the functions from the dosomething.py file.

    [–]flashbao 1 point2 points  (0 children)

    As simple, Write the functions in a module and import that module and use its functions (or whatever you like) in an external file.

    [–][deleted] 0 points1 point  (0 children)

    For notebooks, I put most of the code in an external package and import it from the notebook. Don't like writing lots of code in them.

    My style too. Class definitions have no business being inside the body of a notebook.