r/Python • • Jan 05 '19

I hate notebooks, change my mind

I juste hate them. I never liked using them (I've been forced to for several weeks). It just produces bad code, bad habits and unmaintainnable scripts/code.

10 Upvotes

56 comments sorted by

31

u/SupahNoob Jan 05 '19

You don't even say WHAT you're using notebooks for. If you're just using it to prototype or do exploratory data analysis, it's a fantastic tool. If you're trying to build a library, learn python, or using it as a general project environment, then of course it has limitations: it's not designed for that use case.

-10

u/ricocotam Jan 05 '19

I used it for what it's done : explore the data

And I've been forced to do other things like actual coding. Even for the data exploration, I just don't see the benefits from using console/scripts, apart from the visual aspect.

9

u/[deleted] Jan 05 '19

apart from the visual aspect

Even this is huge for me

1

u/ricocotam Jan 05 '19

Alright. There’s one thing I like about notebooks is code presentation. The only thing that equals notebooks is generated doc. There’s different drawbacks but both are cool.

2

u/nsfy33 Jan 06 '19 edited Nov 04 '19

[deleted]

1

u/ricocotam Jan 06 '19

No fancy data, usual CSVs, a bit of text and a bit of images.

I don’t write plain .py files directly. I first play with a proper console and once I got what I was looking for I write it properly in a .py file

6

u/baubleglue Jan 06 '19

Agreed, I never found why I need it - ipython is great.

4

u/nsfy33 Jan 06 '19 edited Nov 04 '19

[deleted]

2

u/baubleglue Jan 06 '19

> it's very easy to update that cell and rerun the ones after it

I usually use ipython running in console (not GUI), so no cells... when I need to rerun something there are few ways: select/paste; use shell history. History has option to use simple arrow up/down navigation - that is enough in about 80% of cases, another option is start typing and then use arrow up/down, that will reduce the navigation to only lines started from what I typed before, for example if I type for and press arrow-up will bring previous loop. So if I need redo data = map(filter(....)) I type da arrow up and I have my line of code which I need to fix.

The best things about ipython are code completion, data introspection (it is like running code in debugger), feedback on typing is instant and mostly I don't need to use mouse. Probably you can do most of those things in Notebook, I just don't why to use it, when I have ipython.

​

1

u/ricocotam Jan 06 '19

Why don’t you like the QtConsole ? Same as IPython but some plots inline.

1

u/baubleglue Jan 06 '19

Autocomplete is annoying for some reason and startup time long. On other hand I use console build-in in Spyder, which is Qt. I usually do some sort of data processing and I don' need graphics - qt console doesn't provide any advantage unless I use pyplot, but for that I prefer to run program from console as pure python script.

1

u/ricocotam Jan 06 '19

The console you're talking about is the "Jupyter Qtconsole" which is great and is just a windowed version of IPython.

You could try better IDEs like PyCharm, VSCode, Atom, SublimeText...

1

u/ricocotam Jan 06 '19

So you’re saying that apart from markdown and visual aspects, the main advantage is having the possibility to go back on what you’ve made before and just rerun the cell you edited ?

I welcome you in the hell of cells. If you do that and somehow forget to run the cell. Or forget to run the following cells that modifies the data you just edited. Or you delete a variable you were printing, and the printing still works ! Because the variable is still there, just never assigned before and you don’t see it anymore. Or ...

Once you edit a before, if you want to be absolutely sure there’s no side effect, you have to restart the kernel (it cleans all variables in locals and globals) and rerun it all.

1

u/[deleted] Jan 06 '19 edited Jan 06 '19

So my way of doing analysis (and to be clear, that's what I use notebooks for, not writing actual libraries or scripts which I'll do in a text editor or pycharm) is to write a cell that handles, say, reading the file. Then one that handles data cleanup of one type (e.g. some string cleanup). Then another for, say, datetime cleanup. Then do some filtering or aggregation. And so on.

Is there anything equivalent in ipython?

Personally I do exactly this for data analysis, except that instead of writing a cell I write a function in a file, which can be reloaded in the ipython shell when necessary. For things that need some experimentation, I may test some or all of it first in the shell before writing up the function, since that's more convenient than reloading repeatedly after many small changes. I find that this works fine even without the notebook structure for handling multiple lines, although to be fair that's partly because the ipython shell UI has a lot of nice conveniences.

I think this probably works out pretty similar to notebook style development, but I never got used to the notebooks and find that maintaining a module of functions works better for me. That's partly because I find the file-based development much easier to come back to and understand later as normal code, whereas notebooks are mostly limited to a specific gui. Maybe I would like them more if I'd committed more to getting used to that.

25

u/antiproton Jan 05 '19

It does not produce bad code, bad habits and unmaintainable code. You produce bad code, bad habits and unmaintainable code. It's a poor workman that blames his tools.

Notebooks have a use case. If you're trying to fit a square peg into a round hole, then of course it would be frustrating.

Really, though, it just sounds like you're being obstinate because you're being forced to use a tool you don't want to use. That's not the tool's fault.

-1

u/ricocotam Jan 05 '19

Then what’s the use case ? I’ve been using it for data science, what it has been made for I guess

And, of course the user is to blame. But using notebook forces you to write all in one file, no modularity. Importing notebooks just failed a long time. And the code is unmaintainable. The only way to make it maintainable is restarting the kernel each time you want to test and execute everything , what’s the point ?

10

u/SonOfInterflux Jan 06 '19

I’m not a data scientist and normally don’t work in Jupyter notebooks, but I was asked recently to perform some investigation and analysis for a fairly basic business problem, something like how many customers have access to an external service across dimensions like country, operational office, and season. I was taking the work over from someone else who had started it in a Jupyter notebook, so I figured I’d continue using the notebook. There was no reason it couldn’t have been done in ipython or with a script or some reporting platform, and no one forced me to use the notebook. I found it to be a pretty positive experience overall. Here are some of my takeaways:

  1. The person who had started the work had formatted the notebook like a narrative, which made it really easy to get up to speed on (it was an area I didn’t know all that well).

  2. The inline output, many of them being snippets of dataframes, just looked nice. I don’t work with pandas heavily but in a terminal they always look crazy (there’s probably some pretty print that could help).

  3. I continued with the narrative approach and the final notebook looked great, like an interactive abstract.

  4. When I published the notebook, I just sent the requestor the link. I asked if they needed a walkthrough, but the narrative format made it unnecessary.

  5. The requestor was not a developer, but was able to interact with the notebook and play around with it, extending it to perform the same analysis across different dimensions.

  6. When the requestor presented the findings to their bosses, I didn’t receive a single question about how anything was arrived at, though I did receive some feedback, both positive and constructive. It felt like receiving a code review, and opened up good discussions about related business problems. It was a very transparent and open process.

  7. The original business problem seemed like a one-off, but if we were to ever make it an official report in our reporting platform, the notebook is basically the best spec I’ve ever seen in the real world.

Some random takeaways:

  1. There were features added to the notebook that I thought could be useful in other applications, so I moved them into a separate library and imported them into the notebook. Not sure that’s what others do, but it worked for me.

  2. I wasn’t sure how to test the work in the notebook. I probably should have looked into it further, but I ended up using general asserts just for sanity checks. The stuff I moved into the library I wrote proper unit tests for.

  3. When I moved items from the notebook into the library I basically cut and paste. It wasn’t horrible, but I felt like there was probably a better way. Maybe something like the %save command in ipython.

Most of the negatives were because I didn’t take the time to look into actual solutions, though most of them were minor.

Despite the positive experience I had in this one case, I’m not using notebooks daily for my regular work. I like my editor/IDE workflow, down to my fonts and colour schemes and custom key bindings. I did do some of the notebook work in my usual IDE (PyCharm), which gave me access to those features, but at the time PyCharm rendered notebooks a bit janky (I believe they’ve improved them recently).

In a few other comments you mentioned autocomplete not working, which I found odd as they worked for me outside of PyCharm (in the normal ipython way). I think gotos even worked, though I could be misremembering that.

When you say you are being forced to use notebooks, what exactly do you mean? Are you able to move helper functions and stuff into a library you can import into the notebook? If I was in that situation, I’d figure out how to get notebooks working in my editor/IDE of choice and just spit out something presentable at the end. How do you handle tests?

Funnily enough, the thoughts you mentioned about notebooks leading to poor code are the same thoughts I hear from C++ devs who have to work in python!

Hope you found my experience interesting at least!

3

u/ricocotam Jan 06 '19

You’re the first one actually providing arguments ! I kinda agree with you, using notebooks for code presentation/data presentation is almost great. I’m used to do my LaTeX but I can see why people prefer notebooks.

You talked about things I don’t like in notebooks in a positive way. You said you dropped some code in .py files. That’s why I would do and that’s what I did for almost all the code, except printing/plotting. Then that’s just plain data presentation. But the problem is most of people don’t do that. And when you want to reuse their code it’s a pain in the ass. Especially if those people tried to put some code in .py files or in another notebook or whatever crazy bad workflow they had.

But I got you, there’s positive experiences. And please don’t tell others I already had such a good experience. It just happened once or twice though, compared to tens of bad ones.

2

u/its_oliver Jan 06 '19

You can write LaTeX in jupyter notebooks btw!

1

u/SonOfInterflux Jan 06 '19

Not the OP, but that’s pretty awesome, I didn’t know that. I’ll have to look into that further; if you can mix Python and latex that would be really useful! Thanks for the tip!

2

u/its_oliver Jan 06 '19

Yeah, it makes describing models and algorithms way easier. See this link for more tips and tricks!

https://link.medium.com/Pi4YXQYfgT

1

u/SonOfInterflux Jan 06 '19

That is a great little article, thank you for sharing! I'm not sure what some of the extensions do yet, but based on names like Initialization cells, ExecuteTime, and Comment/Uncomment Hotkey, I am very excited to make use of this!

1

u/SonOfInterflux Jan 06 '19

I’m glad you found some value in my post, I didn’t set out to write as much as I did. It sounds like your main issues are not with notebooks themselves, but working with others in notebooks. While I took over the notebook I worked on from someone else, the co-authoring basically ended there, and I could see how working with others who aren’t diligent could lead to headaches. That said, that could apply to just about everything. As I mentioned, I’ve worked with devs who were used to C++ who have hated working in Python because they felt it lead to bad habits and they missed having the safety net of a compiler, but there are great Pythonic solutions to these issues.

We have a git repo dedicated to work in notebooks, which has access to our internal libraries and external libraries. We still expect to put notebooks through code review and work has to be approved before it can be merged and published. The process isn’t as tightly controlled as publishing to our reporting platform, but it’s enough to increase the quality of code to a fairly high level.

If you don’t have a process like this in place, I’d recommend suggesting one to your team and management. It shouldn’t be hard, these are conventional best practices, and if you do get push back against such a suggestion, I’d feel like that is a larger issue than the notebooks themselves.

Once you have a good code review process in place, you can focus on what you find as the largest pain points. Making use of PR templates and CI services can help the developers and reviewers achieve a high level of end product. If you are seeing duplicated code, ask to take some time to refactor it into a reusable, documented, and testable library. If your teammates and management are anything but ecstatic about this prospect, I’d be very surprised.

I’d be happy to discuss the issues you’ve experience and potential solutions further. As I mentioned, I’m not a data scientist and do not often work in notebooks, so it’s a learning experience for me, too. I’ve just applied what I know from working on back-end applications and services to notebooks, and simply treat them as a presentation tool with the benefits of a good console/shell. If I was noodling around in my usual console and made something interesting or useful, I wouldn’t check in a dump of the console expecting it to be merged into master without a heavy refactoring, and I treated the notebook the same. Thanks for reading!

3

u/Bengineer700 Jan 05 '19

I code on a Chromebook and desktop. Obviously the power is at the desktop, but if I'm travelling and need to prototype a few bits the Chromebook is more than enough. Currently I'm setting up a VM server so I can have a dedicated Linux VM for programming that I can use from my Chromebook (have power of a desktop but portability of a laptop), but it's not up yet.

I have two roommates at opposite ends of the spectrum, one pretty much only uses a 13" Dell to do his coding, even though he has a desktop. The other pretty much only uses his desktop even though he has a laptop.

At the end of the day it isn't a matter of us making you like one thing or the other. If you've found a set up that works for you and your use case, that's all that matters.

12

u/4-14 Jan 05 '19

I’m pretty sure he’s talking about jupyter notebooks, not laptops

5

u/ricocotam Jan 05 '19

Yup. Notebooks computers are awesome !

1

u/[deleted] Jan 05 '19

Sorry for slightly off-topic because I am not sure what you mean by notebooks,

Do you mean using something to keep snip bits of code so you can learn it over and over in a book, because I find that very useful.

2

u/ricocotam Jan 05 '19

No ^^ I’m talking about jupyter notebooks

3

u/burgerAccount Jan 05 '19

I'm okay with this statement in r/python. It doesn't feel very pythonic.

It's pretty decent for basic exploratory analysis. You'll probably end up using something more sophisticated for production quality work. So I don't 100% disagree with OP.

2

u/lunar-orbiter Jan 06 '19 edited Jan 06 '19

It may be useful to get some historical context and learn more about where Jupiter Book is coming from and why such tools are used, i.e. literate programming. If your local library carries any of Knuth's literate works I recommend that you at least browse through them.

1

u/WikiTextBot Jan 06 '19

Literate programming

Literate programming is a programming paradigm introduced by Donald Knuth in which a program is given as an explanation of the program logic in a natural language, such as English, interspersed with snippets of macros and traditional source code, from which a compilable source code can be generated.The literate programming paradigm, as conceived by Knuth, represents a move away from writing programs in the manner and order imposed by the computer, and instead enables programmers to develop programs in the order demanded by the logic and flow of their thoughts. Literate programs are written as an uninterrupted exposition of logic in an ordinary human language, much like the text of an essay, in which macros are included to hide abstractions and traditional source code.

Literate programming (LP) tools are used to obtain two representations from a literate source file: one suitable for further compilation or execution by a computer, the "tangled" code, and another for viewing as formatted documentation, which is said to be "woven" from the literate source. While the first generation of literate programming tools were computer language-specific, the later ones are language-agnostic and exist above the programming languages.


[ ^PM | Exclude ^me | Exclude from ^subreddit | FAQ / ^Information | ^Source ] Downvote to remove | v0.28

1

u/FunCicada Jan 06 '19

Literate programming is a programming paradigm introduced by Donald Knuth in which a program is given as an explanation of the program logic in a natural language, such as English, interspersed with snippets of macros and traditional source code, from which a compilable source code can be generated.

1

u/lunar-orbiter Jan 06 '19

The above is actually the historical context in the computer science community. For learning about Jupiter Notebook's motivations and use cases in the science community it's better to read the historical perspective by Fernando Pérez, the creator of IPython and Jupiter Notebook.

2

u/jackmaney Jan 06 '19

You're not alone at all. Notebooks are vastly inferior to PyCharm's scientific mode.

0

u/blabbities Jan 05 '19

Jupyter notebooks?

How funny I literally just watched a video on a course that I bought 2 years ago that added a few more segments. I never understood Jupyter notebooks and that vid didnt really help. Ive used maybe once at a Python meetup at the instruction of this science chick. I think I realized then that it's probably geared for Data Scientists.

That being said IPython(3). IPython is fantastic. Just for %paste% alone, but additionally I love using it for live programming testing. Its like having a really cool sensible smart IDE built into the interpreter rather than python3.exe alone. I also have benefitted greatly that history persists upon exit too.

1

u/ricocotam Jan 05 '19

If you like ipython, you’ll like « jupyter qtconsole »

1

u/blabbities Jan 05 '19

Wait dont you hate Jupyter? is this just Jupyter in QT ?

0

u/ricocotam Jan 05 '19

I just hate notebooks, not jupyter. In Notebooks there’s no code completion. Even the console does it with ipython

1

u/ukralibre Jan 05 '19

level 4ricocotamOriginal Poster1 point · 31 minutes agoI just hate notebooks, not jupyter. In Notebooks there’s no code completion. Even the console does it with ipython

So you don't like notebooks because of low code area usability?

I want to know

1

u/ricocotam Jan 05 '19

You can’t reuse the code properly -> unmaintainable work and bad habits No code completion -> bad tool

Those only two reasons are enough but I can think of more if you ask

1

u/ukralibre Jan 05 '19

Yes, because i genuinely thought it is a great idea. I can't be first time learner any more, so learning your experience is important to me.

Can you write down top things you liked and disliked?

Bad habits it is important one, i agree. All students i teached make horrible mess and it takes a while to teach them write mantainable code.

2

u/ricocotam Jan 05 '19

Alright. Then I suggest you to read Joel Grus’ presentation about notebooks at the Jupyter Conference ;)

1

u/ukralibre Jan 05 '19 edited Jan 05 '19

Thanks, found it.

Yeah, sounds funny )

That perfectly answers my question.

1

u/blabbities Jan 05 '19

Ahh ok gotcha. Def dont even use that then

1

u/Paddy3118 Jan 06 '19

It's much better than obscure, unmaintainable Excel macros.

1

u/nielsrolf Jan 06 '19

When your code has a lot of start up time, you can debug or explore way faster. Pandas has a great integration, which makes notebooks + pandas a great tool for data exploration. It is great for tutorials, you see code and results next to each other, and on github you can view them nicely. In one comment you said you prefer notebooks because of the code completion, but vscode has code completion and can run notebooks, so there is at least one solution. You are not supposed to have a notebook with >1000 lines of code though.

0

u/[deleted] Jan 05 '19

[deleted]

1

u/ricocotam Jan 06 '19

Sorry dude but I know the link you provided and it’s just a disaster to use. You can’t code properly in .py files when importing notebooks because you have to go read the notebooks in a browser which lack of all IDE’s tools for code.

The unmaintainable part, I answered somewhere, is because you’re invited to edit previous cells without checking if following cells are still working. You might delete a cell, stuff still works and when you rester, tada it doesn’t work. I can understand that people not coming from CS background need to code. But they should take some software engineering courses. When you want to use a tool you don’t know, you better ask people doing this as a job how to do. Taking shortcuts is usually not a good idea.

So for a super basic thing I have to install new modules, go to options and check some boxes ? The console already do this !

0

u/[deleted] Jan 05 '19

[deleted]

3

u/ricocotam Jan 05 '19

If I never coded in Cobol, I would have trolled you on that. But you're right, worst tools exists..

4

u/[deleted] Jan 05 '19

[removed] — view removed comment

3

u/ricocotam Jan 05 '19

Why not using a console then ? Jupyter’s qtconsole or ipython are really good. And they have code completion.

1

u/shvimas Jan 05 '19

There actually is code completion in notebooks, alongside with many other IDE features. What I really miss though are some of PyCharm’s refactoring tools but any console lacks them for good so whatever.

1

u/ricocotam Jan 05 '19

There is ? I never saw it and I think I have the latest version. Of course if by installing 12 other libs you can have a lot more features. But consoles already have that and I don’t see why using notebooks over console

1

u/yngvizzle Jan 06 '19

There is code completion with no add-ons. It has some quirks but does the job.

1

u/shvimas Jan 06 '19 edited Jan 06 '19

Don’t understand how could you miss it, just hit Tab while typing e.g. some class method or func. Important thing: using module must be imported already that’s why people usually import everything in the first cell, run it, and right after that autocomplete works fine.

Also take a look at Jupyter Lab, imho they’ve fixed a lot of arguable things from Notebook.

1

u/Caminando_ Jan 06 '19

I use jupyter for working with Sympy. The console kind of sucks in comparison.