Hi Reader,
Wherever you are in the world today, I wish you safety, health, and happiness! 💗
This guide was written by my pal Trey Hunner, and it’s the single best source I’ve found for clear explanations of Python terms and concepts. I use it to learn new things and to double-check that my teaching materials are correct!
Back in tip 24, I introduced you to IPython magic commands, which are special commands that you can use in Jupyter.
You learned about line magics, which start with % and apply to one line of code:
You also learned about cell magics, which start with %% and apply to an entire cell:
Today, I’m going to introduce you to 4 more magic commands that are great for saving, displaying, and running code!
Let’s pretend that you’re working in a large Jupyter notebook and you come up with a brilliant new function:
You want to save this function to reuse in other notebooks. Without leaving Jupyter, you use the %%writefile cell magic to save just this function to another Python script:
And now your function is preserved in a separate file!
Weeks pass, and you’re working in a new notebook that could benefit from this function. You want to remind yourself what’s in the function, so you output the file contents using the %pycat line magic:
If it needed some edits, you could use the %load line magic:
Running that command loads the contents of the file into the cell (but does not run it) so that you can make those edits:
But in this case, the function is perfect as-is. Thus you use the %run line magic to run the existing file:
Our function is now available for use in this new notebook:
Key takeaway: With this workflow, you can save and reuse code blocks without ever leaving the Jupyter environment!
By the way, %run even works with notebooks! For example, if you had this notebook called drinks.ipynb:
You could run the entire notebook from another notebook, and just see its output:
Key takeaway: You can build a series of smaller notebooks, and then run them all from a “master” notebook that only displays the output (and hides the underlying code).
If you enjoyed this week’s tip, please forward it to a friend! Takes only a few seconds, and it really helps me reach more people! 🙏
See you next Tuesday!
- Kevin
P.S. Which came first, the chicken or the egg?
Did someone awesome forward you this email? Sign up here to receive Data Science tips every week!
Join 25,000+ intelligent readers and receive AI tips every Tuesday!
Hi Reader, Here are your top AI stories for the week: AI-driven education in 2 hours per day Add yourself to an AI-generated TV show AI models send "subliminal messages" to one another Read more below! 👇 Sponsored by: Superhuman AI Find out why 1M+ professionals read Superhuman AI daily. AI won't take over the world. People who know how to use AI will. Here's how to stay ahead with AI: Sign up for Superhuman AI. The AI newsletter read by 1M+ pros. Master AI tools, tutorials, and news in just...
Hi Reader, In this week’s tip, I’ll be breaking down some highly practical advice for taking full advantage of the capabilities of today’s AI models. Check it out below! 👇 Today’s tip is based on Ethan Mollick’s excellent article, Using AI Right Now: A Quick Guide. I recommend reading the whole thing if you have time, but if not, I’ve pulled out some important quotes from the article and added my own commentary: For most people who want to use AI seriously, you should pick one of three...
Hi Reader, Here are the most important AI stories I’ve found this month: Microsoft’s AI solves medical mysteries Cloudflare charges AI crawlers Books are legal for AI training AI model teaches itself AI researchers are paid like superstars Details below! 👇 🩺 Microsoft’s AI solves medical mysteries Microsoft’s new “AI Diagnostic Orchestrator” solved 85% of 304 real medical mysteries, whereas experienced doctors (who did not have access to colleagues or medical databases) only solved an average...