Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Share Your REPL Session

00:00 Share Your REPL Session. The bpython REPL is an excellent teaching tool that you can use to explain an idea and then share the resulting code with your coworkers or students in a classroom. After you’ve written something in bpython, you can copy the entire REPL session with all outputs to the clipboard, save it in a file, or share it online.

00:23 You can copy the contents of your REPL session to the clipboard with a single keystroke, which can be handy for sharing code snippets with others or pasting them into a forum online. Note, however, that the copy-to-clipboard feature doesn’t work by default in bpython.

00:37 To enable it, you must first install the pyperclip library into the virtual environment so that bpython can import it.

00:55 Once it’s installed, press F10, which is the default shortcut to grab your REPL session. Here, a familiar function is created and used a couple of times. F10 is pressed, which copies the session to the clipboard.

01:11 Exiting bpython, another editor is opened—in this case, nano.

01:19 The code is pasted in from the clipboard, complete with comments for the lines where the output was present, and can be saved for use elsewhere. If you want to share the code in a visual manner, including code coloring, then be sure to open it in an appropriate editor.

01:37 There is another option available to you. Instead of pasting the code by hand into a messaging application or text editor, you can save your current REPL session directly to a local file on your computer.

01:48 The default keyboard shortcut to save your bpython REPL session to a file is the familiar Ctrl + S keyboard combination, which most Windows programs use for this purpose.

02:02 It will ask you to type the filename to be created in the current working directory or an entire path indicating where you want it to be saved. If the file already exists, then you’ll have the option to overwrite it, append to it, or cancel the operation.

02:21 On some UNIX shells, the Ctrl + S key combination is a standard hotkey reserved for stopping the output of a running program. It sends a special code to pause the transmission of data through the terminal, overriding bpython’s default behavior.

02:36 This was the case on my MacBook running zsh. To disable this archaic software flow control, you can use the following command in your terminal session.

02:49 Now, you’ll be able to use the Ctrl + S shortcut in bpython.

02:55 If you want to use this feature often, then you could add the command to your shell profile or reassign the keyboard shortcut in bpython’s configuration file to something else, such as F4.

03:07 The nice thing about saving code written in bpython to a file is that it won’t include REPL prompts such as the triple chevron (>>>) or the ellipsis (...).

03:15 This makes code easier to run. However, you still have to send the file as an attachment yourself if you want to share the code with others, and that’s where sending your REPL session to a pastebin comes in handy.

03:29 The third way of sharing code in bpython is to upload your REPL session to a pastebin or an online text storage service. The bpython REPL supports bpa.st by default, which is an instance of pinnwand, but you can change the default pastebin to another service if you want to. When you hit F8 and confirm your choice with Y, after a few moments, you’ll be presented with a publicly available URL to share.

04:04 As an owner, you’ll also get another URL, which you can use to delete the paste if you no longer want it on the server. If you don’t delete the paste yourself, then the URL will expire after one week.

04:17 You can change this default setting in bpython’s configuration. When someone navigates to your paste, they’ll see a nicely formatted snapshot of your bpython REPL session with syntax highlighting and the ability to toggle the dark mode.

04:32 You can click one of the links in the uploaded REPL session to view the code in plain text without any colors, copy it to the clipboard, or download it as a file.

04:42 There’s also an option to encode the REPL session using hexadecimal characters, which you can decode using Python’s binascii.unhexlify() function.

04:54 Throughout this course, you’ve heard how there are options that you can configure in bpython. In the next section of the course, you’ll see how you can do that.

Avatar image for Stuart

Stuart on Sept. 19, 2024

Hi

When F8 is selected to upload my code snippet to pastebin I get the following error after selecting “y”

Upload failed: 405 Client Error: Method Not Allowed for url: bpa.st/api/v1/paste

bpython –version bpython version 0.24 on top of Python 3.12.2

Any ideas on what might be causing this error?

Many thanks

Become a Member to join the conversation.