Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Jupyter to write a 5-minute diary every day

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge about "how to write a diary for 5 minutes every day with Jupyter". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Import ipywidgets module

First, you need to import a bunch of things, like ipywidgets and Twisted. The Twisted module can be used to create an asynchronous time counter:

import twisted.internet. asyncioreactor.install()from twisted.internet import reactor, taskimport ipywidgets, datetime, subprocess, functools, os Set timed entries

Twisted time counters are implemented using task.LoopingCall. However, the only way to end the loop call is with an exception. The countdown clock always stops, so you need a custom exception to indicate "everything is OK; counter is over":

class DoneError(Exception): pass

Now that you have written the exception, you can write the timer. The first step is to create a text label component for ipywidgets.Label. Loop divmod to calculate minutes and seconds, then set the text value of the label:

def time_out_counter(reactor): label = ipywidgets.Label("Time left: 5:00") current_seconds = datetime.timedelta(minutes=5).total_seconds() def decrement(count): nonlocal current_seconds current_seconds -= count time_left = datetime.timedelta(seconds=max(current_seconds, 0)) minutes, left = divmod(time_left, minute) seconds = int(left.total_seconds()) label.value = f"Time left: {minutes}:{seconds:02}" if current_seconds < 0: raise DoneError("finished") minute = datetime.timedelta(minutes=1) call = task.LoopingCall.withCount(decrement) call.reactor = reactor d = call.start(1) d.addErrback(lambda f: f.trap(DoneError)) return d, label Save text from Jupyter component

The next step is to write something, save the text you typed into a file, and submit it to Git. Also, since you're writing a 5-minute diary, you'll need a widget that gives you a writing area (scrolling is certainly okay, but seeing more text at once is better).

This uses the Textarea component, which is a text field you can write, and Output, which is used to give feedback. This is important because git push can take a while or fail, depending on the network. If the backup fails, it is important to remind users with feedback:

def editor(fname): textarea = ipywidgets.Textarea(continuous_update=False) textarea.rows = 20 output = ipywidgets.Output() runner = functools.partial(subprocess.run, capture_output=True, text=True, check=True) def save(_ignored): with output: with open(fname, "w") as fpout: fpout.write(textarea.value) print("Sending... ", end='') try: runner(["git", "add", fname]) runner(["git", "commit", "-m", f"updated {fname}"]) runner(["git", "push"]) except subprocess.CalledProcessError as exc: print("Could not send") print(exc.stdout) print(exc.stderr) else: print("Done") textarea.observe(save, names="value") return textarea, output, save

continuous_update=False is to avoid saving every character and sending it to Git. Instead, it is preserved whenever it is removed from input focus. This function also returns the save function, so you can call it explicitly.

create a layout

Finally, you can use ipywidgets.VBox to put these things together. This is something that contains some components and is displayed vertically. There are other ways to arrange components, but this is simple enough:

def journal(): date = str(datetime.date.today()) title = f"Log: Startdate {date}" filename = os.path.join(f"{date}.txt") d, clock = time_out_counter(reactor) textarea, output, save = editor(filename) box = ipywidgets.VBox([ ipywidgets.Label(title), textarea, clock, output ]) d.addCallback(save) return box

biu! You've defined a journaling function, so it's time to give it a try.

journal()

Jupyter journal

You can write for five minutes now!

"How to write a 5-minute diary every day with Jupyter" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report