Planet Python

Subscribe to Planet Python feed
Planet Python - http://planetpython.org/
Updated: 7 hours 18 min ago

Kushal Das: Updated blog theme after many years

Sat, 2024-09-28 03:12

One of the major reason of using static blogging for me is to less worry about how the site will look like. Instead the focus was to just write (which of course I did not do well this year). I did not change my blog's theme for many many years.

But, I noticed Oskar Wickström created a monospace based site and kindly released it under MIT license. I liked the theme, so decided to start using it. I still don't know HTML/CSS but managed to change the template for my website.

You can let me know over mastodon what do you think :)

Categories: FLOSS Project Planets

Zero to Mastery: Python Monthly Newsletter 💻🐍

Fri, 2024-09-27 21:44
58th issue of Andrei Neagoie's must-read monthly Python Newsletter: itertools Guide, Become an Open-Source God, and much more. Read the full newsletter to get up-to-date with everything you need to know from last month.
Categories: FLOSS Project Planets

Python Morsels: The string split method in Python

Fri, 2024-09-27 18:00

Strings can be split by a substring separator. Usually the string split is called without any arguments, which splits on any whitespace.

Table of contents

  1. Breaking apart a string by a separator
  2. Splitting a specific number of times
  3. Splitting from the end of a string
  4. The string split method versus regular expressions

Breaking apart a string by a separator

If you need to break a string into smaller strings based on a separator, you can use the string split method:

>>> time = "1:19:48" >>> time.split(":") ['1', '19', '48']

The separator you split by can be any string. It doesn't need to be just one character:

>>> graph = "A->B->C->D" >>> graph.split("->") ['A', 'B', 'C', 'D']

Note that it's a little bit unusual to call the string split method on a single space character:

>>> langston = "Does it dry up\nlike a raisin in the sun?\n" >>> langston.split(" ") ['Does', 'it', 'dry', 'up\nlike', 'a', 'raisin', 'in', 'the', 'sun?\n']

It's usually preferable to call split without any arguments at all:

>>> langston = "Does it dry up\nlike a raisin in the sun?\n" >>> langston.split() ['Does', 'it', 'dry', 'up', 'like', 'a', 'raisin', 'in', 'the', 'sun?']

Calling the split with no arguments will split on any consecutive whitespace characters. So we're even splitting on a new line here in between up and like (up\nlike).

Also note that the split method without any arguments removes leading and trailing whitespace (note that the last element in the list is sun? rather than sun?\n).

There's one more split feature that's often overlooked: maxsplit.

Splitting a specific number of times

When calling split with a …

Read the full article: https://www.pythonmorsels.com/string-split-method/
Categories: FLOSS Project Planets

Carl Trachte: DAG Hamilton Graph Presented as SVG in Blogger

Fri, 2024-09-27 15:30

Through the kindness of the DAG Hamilton project team, I was able to secure an official svg version of the DAG Hamilton logo. It looks significantly better than the one I had generated with an online image to svg converter and is much smaller and easy to work with (4 kilobytes versus 200 kb). The DAG Hamilton graphviz graph now shows up in Blogger; it is unlikely to show up on the planet(python) feed. Blogger is not liking the code and svg I have included (complaints of malformed html). In the interest of preserving the rendering of the graph(s), I am constraining the text here to a few paragraphs

The first graph has the code provided. This graph is from a previous post.

The second graph represents the DAG Hamilton workflow for the production of the first graph. This is in keeping with the "Eat your own dogfood" mantra. I happen to like the DAG Hamilton dogfood as I've mentioned in previous posts. It allows me to visualize my workflows and track complexity and areas for improvement in the code.

The third one I did with a scaled down version of the code presented (no logos). I hand pasted the DAG Hamilton official logo into the third one. It is not subtle (the logo is huge), but it provides an idea of what one can do creatively with the logo or any svg element. Also, it shows the DAG Hamilton workflow for the graph with the respective two logos.

All the code is a work in progress. Ideally I would like to keep reducing this to the most simple svg implementation possible to get it to show up or "work." Realistically, I'm afraid to sneeze for fear Blogger will protest. For now, I'm leaving good enough alone. Links and thoughts on svg (there is at least one python library (orsinium-labs/svg.py) out there that is way more elegant in its treatment of the medium than my rough regular expressions / text processing) will have to wait for another post.

Thanks for stopping by.

Toy Web Scraping Script Run Diagram Web Scraping Functions Highlighted Legend datafile str commodity_word_counts dict info_dict_merged dict colloquial_company_word_counts dict data_with_wikipedia dict data_with_company dict parsed_data dict wikipedia_report str info_output str input function

run.py code

""" Hamilton wrapper. """ # run.py import sys import pprint from hamilton import driver import dag_hamilton_to_blogger as dhtb dr = driver.Builder().with_modules(dhtb).build() dr.display_all_functions('dhtb.svg', deduplicate_inputs=True, keep_dot=True, orient='BR') results = dr.execute(['defluffed_lines', 'scale_and_translation', 'logo_positions', 'captured_values', 'scaled_elements', 'translated_elements', 'hamilton_logo_data', 'scale_and_translation_hamilton_logo', 'fauxcompany_logo_data', 'scale_and_translation_fauxcompany_logo', 'svg_ready_doc', 'written_svg'], inputs={'svg_file':'web_scraping_functions_highlighted.svg', 'outputfile':'test_output.svg', 'hamiltonlogofile':'hamilton_official_stripped.svg', 'hamiltonlogo_coords':{'min_x':-0.001, 'max_x':4353.846, 'min_y':-0.0006, 'max_y':4177.257}, 'fauxcompanylogofile':'fauxcompanylogo_stripped_down.svg', 'fauxcompanylogo_coords':{'min_x':11.542786063261742, 'max_x':705.10684, 'min_y':4.9643821, 'max_y':74.47416391682819}})

Main DAG Hamilton functions (dag_hamilton_to_blogger.py)

# python 3.12 """ Make DAG Hamilton graph show up in Blogger. """ import re import sys import pprint import math import copy import reusedfunctions as rf VIEWBOX_PAT = (r'[ ]viewBox[=]["][-]?[0-9]+[.]?[0-9]*[ ][-]?[0-9]+[.]?[0-9]*[ ]' r'([0-9]+[.]?[0-9]*)[ ]([0-9]+[.]?[0-9]*)') # 5 coordinates. POLYGON_PAT = (r'[]polygon' r'.*([ ]points[=]["])([-]?[0-9]+[.]?[0-9]*)[,]' r'([-]?[0-9]+[.]?[0-9]*)[ ]' r'([-]?[0-9]+[.]?[0-9]*)[,]' r'([-]?[0-9]+[.]?[0-9]*)[ ]' r'([-]?[0-9]+[.]?[0-9]*)[,]' r'([-]?[0-9]+[.]?[0-9]*)[ ]' r'([-]?[0-9]+[.]?[0-9]*)[,]' r'([-]?[0-9]+[.]?[0-9]*)[ ]' r'([-]?[0-9]+[.]?[0-9]*)[,]' r'([-]?[0-9]+[.]?[0-9]*)["]') # 4 coordinates instead of 5. POLYGON_PAT_4 = (r'[]polygon' r'.*([ ]points[=]["])([-]?[0-9]+[.]?[0-9]*)[,]' r'([-]?[0-9]+[.]?[0-9]*)[ ]' r'([-]?[0-9]+[.]?[0-9]*)[,]' r'([-]?[0-9]+[.]?[0-9]*)[ ]' r'([-]?[0-9]+[.]?[0-9]*)[,]' r'([-]?[0-9]+[.]?[0-9]*)[ ]' r'([-]?[0-9]+[.]?[0-9]*)[,]' r'([-]?[0-9]+[.]?[0-9]*)["]') # x, y TEXTPAT = (r']text[ ].*' r'([ ]font[-]size[=])' r'["]([0-9]+[.]?[0-9]*)["]') # initial bezier curve notation PATHPAT = (r'[]path[ ].*' r'([ ]d[=]["]M)([-]?[0-9]+[.]?[0-9]*)[,]' r'([-]?[0-9]+[.]?[0-9]*)C') X_SIZE = 600 NEW_FIRST_LINE = '') IMAGE_FLAG = '') # 4 coords (arrow head). POLYGON_STR_4 = (r' points="{0:.3f},{1:.3f} {2:.3f},{3:.3f} ' r'{4:.3f},{5:.3f} {6:.3f},{7:.3f}"/>') PATH_START_STR = r' d="M{0:.3f},{1:.3f}C' PATH_STR_SEGMENT = ' {0:.3f},{1:.3f}' PATH_STR = r' {0:s}"/>' TEXT_STR = r' x="{0:.3f}" y="{1:.3f}"' TEXT_STR_FONT = r' font-size="{0:.3f}"' HAMILTON_LOGO_DIMENSIONS_PAT = (r'.*width[=]["]([0-9]+[.]?[0-9]*)px["][ ]' r'height[=]["]([0-9]+[.]?[0-9]*)px["][>]') FAUXCOMPANY_LOGO_DIMENSIONS_PAT = (r'[ ]width[=]["]([0-9]+[.]?[0-9]*)["][ ]' r'height[=]["]([0-9]+[.]?[0-9]*)["][ ][>]') # The official Hamilton logo splits the path into multiple # lines with the last one having the absolute location # ("C") of a bezier curve. HAMILTON_CHANGE_LINE_PAT = r'.*C[-]?[0-9]+[.]?[0-9]*' HAMILTON_TRANSFORM_FMT = (' transform="scale({scale:f}) ' 'translate({translate_x:f},{translate_y:f})" />') # One line of paths in Inkscape generated file. FAUXCOMPANY_CHANGE_LINE_PAT = r'.*d[=]["]m[ ]' # Inkscape put the closing tag /> on the following line. FAUXCOMPANY_TRANSFORM_FMT = (' transform="scale({scale:f}) ' 'translate({translate_x:f},{translate_y:f})"') # * - get rid of first 6 lines. # * - get rid of any line starting with: # # "
Categories: FLOSS Project Planets

Terri Oda: Best practices in practice: Black, the Python code formatter

Fri, 2024-09-27 14:00
This is crossposted from Curiousity.ca, my personal maker blog. If you want to link to this post, please use the original link since the formatting there is usually better.


I’m starting a little mini-series about some of the “best practices” I’ve tried out in my real-life open source software development. These can be specific tools, checklists, workflows, whatever. Some of these have been great, some of them have been not so great, but I’ve learned a lot. I wanted to talk a bit about the usability and assumptions made in various tools and procedures, especially relative to the wider conversations we need to have about open source maintainer burnout, mentoring new contributors, and improving the security and quality of software.





So let’s start with a tool that I love: Black.





Black’s tagline is “the uncompromising Python code formatter” and it pretty much is what it says on the tin: it can be used to automatically format Python code, and it’s reasonably opinionated about how it’s done with very few options to change. It starts with pep8 compliance (that’s the python style guide for those of you don’t need to memorize such things) and takes it further. I’m not going to talk about the design decisions they made but the black style guide is actually an interesting read if you’re into this kind of thing.





I’m probably a bit more excited about style guides than the average person because I spent several years reading and marking student code, including being a teaching assistant for a course on Perl, a language that is famously hard to read. (Though I’ve got to tell you, the first year undergraduates’ Java programs were absolutely worse to read than Perl.) And then in case mounds of beginner code wasn’t enough of a challenge, I also was involved in a fairly well-known open source project (GNU Mailman) with a decade of code to its name even when I joined so I was learning a lot about the experience of integrating code from many contributors into a single code base. Both of these are… kind of exhausting? I was young enough to not be completely set in my ways, but especially with the beginner Java code, it became really clear that debugging was harder when the formatting was adding a layer of obfuscation to the code. I’d have loved to have an autoformatter for Java because so many students could find their bugs easier once I showed them how to fix their indents or braces.





And then I spent years as an open source project maintainer rather than just a contributor, so it was my job to enforce style as part of code reviews. And… I kind of hated that part of it? It’s frustrating to have the same conversation with people over and over about style and be constantly leaving the same code review comments, and then on top of that sometimes people don’t *agree* with the style and want to argue about it, or people can’t be bothered to come back and fix it themselves so I either have to leave a potentially good bug fix on the floor or I have to fix it myself. Formatting code elegantly can be fun once in a while, but doing it over and over and over and over quickly got old for me.





So when I first heard about Black, I knew it was a thing I wanted for my projects.





Now when someone submits a thing to my code base, Black runs alongside the other tests, and they get feedback right away if their code doesn’t meet our coding standards. It hardly any time to run so sometimes people get feedback very fast. Many new contributors even notice failing required test and go do some reading and fix it before I even see it, and for those that don’t fix issues before I get there I get a much easier conversation that amounts to “run black on your files and update the pull request.” I don’t have to explain what they got wrong and why it matters — they don’t even need to understand what happens when the auto-formatter runs. It just cleans things up and we move on with life.





I feel like the workflow might actually be better if Black was run in our continuous integration system and automatically updated the submitted code, but there’s some challenges there around security and permissions that we haven’t gotten around to solving. And honestly, it’s kind of nice to have an easy low-stress “train the new contributors to use the tools we use” or “share a link to the contributors doc” opening conversation, so I haven’t been as motivated as I might be to fix things. I could probably have a bot leave those comments and maybe one of those days we’ll do that, but I’m going to have to look at the code for code review anyhow so I usually just add it in to the code review comments.





The other thing that Black itself calls out in their docs is that by conforming to a standard auto-format, we really reduce the differences between existing code and new code. It’s pretty obvious when the first attempt has a pile of random extra lines and is failing the Black check. We get a number of contributors using different integrated development environments (IDEs) that are pretty opinionated themselves, and it’s been freeing to not to deal with whitespace nonsense in pull requests or have people try to tell me on the glory if their IDE of choice when I ask them to fix it. Some python IDEs actually support Black so sometimes I can just tell them to flip a switch or whatever and then they never have to think about it again either. Win for us all!





So here’s the highlights about why I use Black:





As a contributor:






  1. Black lets me not think about style; it’s easy to fix before I put together a pull request or patch.




  2. It saves me from the often confusing messages you get from other style checkers.




  3. Because I got into the habit of running it before I even run my code or tests, it serves as a quick mistake checkers.




  4. Some of the style choices, like forcing trailing commas in lists, make editing existing code easier and I suspect increase code quality overall because certain types of bug are more obvious.





As a an open source maintainer:






  1. Black lets me not think about style.




  2. It makes basic code quality conversations easier. I used to have a *lot* of conversations about style and people get really passionate about it, but it wasted a lot of time when the end result was usually going to be “conform to our style if you want to contribute to this project”




  3. Fixing bad style is fast, either for the contributor or for me as needed.




  4. It makes code review easier because there aren’t obfuscating style issues.




  5. It allows for very quick feedback for users even if all our maintainers are busy. Since I regularly work with people in other time zones, this can potentially save days of back and forth before code can be used.




  6. It provides a gateway for users to learn about code quality tools. I work with a lot of new contributors through Google Summer of Code and Hacktoberfest, so they may have no existing framework for professional development. But also even a lot of experienced devs haven’t used tools like Black before!




  7. It provides a starting point for mentoring users about pre-commit checks, continuous integration tests, and how to run things locally. We’ve got other starting points but Black is fast and easy and it helps reduce resistance to the harder ones.




  8. It reduces “bike shedding” about style. Bikeshedding can be a real contributor to burnout of both maintainers and contributors, and this reduces one place where I’ve seen it occur regularly.




  9. It decreases the cognitive overhead of reading and maintainin a full code base which includes a bunch of code from different contributors or even from the same contributor years later. If you’ve spent any time with code that’s been around for decades, you know what I’m talking about.




  10. In short: it helps me reduce maintainer burnout for me and my co-maintainers.





So yeah, that’s Black. It improves my experience as an open source maintainer and as a mentor for new contributors. I love it, and maybe you would too? I highly recommend trying it out on your own code and new projects. (and it’s good for existing projects, even big established ones, but choosing to apply it to an existing code base gets into bikeshedding territory so proceed with caution!)





It’s only for Python, but if you have similar auto-formatters for other languages that you love, let me know! I’d love to have some to recommend to my colleagues at work who focus on other languages.



comments
Categories: FLOSS Project Planets

Real Python: The Real Python Podcast – Episode #222: Using Virtual Environments in Docker & Comparing Python Dev Tools

Fri, 2024-09-27 08:00

Should you use a Python virtual environment in a Docker container? What are the advantages of using the same development practices locally and inside a container? Christopher Trudeau is back on the show this week, bringing another batch of PyCoder's Weekly articles and projects.

[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Categories: FLOSS Project Planets

Python GUIs: Introduction to the QGraphics framework — Creating vector interfaces using the QGraphics View framework

Fri, 2024-09-27 03:00

The Qt Graphics View Framework allows you to develop fast and efficient 2D vector graphic scenes. Scenes can contain millions of items, each with their own features and behaviors. By using the Graphics View via PySide6 you get access to this highly performant graphics layer in Python. Whether you're integrating vector graphics views into an existing PySide6 application, or simply want a powerful vector graphics interface for Python, Qt's Graphics View is what you're looking for.

Some common uses of the Graphics View include data visualization, mapping applications, 2D design tools, modern data dashboards and even 2D games.

In this tutorial we'll take our first steps looking at the Qt Graphics View framework, building a scene with some simple vector items. This will allow us to familiarize ourselves with the API and coordinate system, which we'll use later to build more complex examples.

Table of Contents The Graphics View Framework

The Graphics View framework consists of 3 main parts QGraphicsView, QGraphicsScene, and QGraphicsItem, each with different responsibilities.

The framework can be interpreted using the Model-View paradigm, with the QGraphicsScene as the Model and the QGraphicsView as the View. Each scene can have multiple views. The QGraphicsItems within the scene can be considered as items within the model, holding the visual data that the scene combines to define the complete image.

QGraphicsScene is the central component that glues everything together. It acts as a whiteboard on which all items are drawn (circles, rectangles, lines, pixmaps, etc). The QGraphicsView has the responsibility of rendering a given scene -- or part of it, with some transformation (scaling, rotating, shearing) -- to display it to the user. The view is a standard Qt widget and can be placed inside any Qt layout.

QGraphicsScene provides some important functionalities out of the box, so we can use them to develop advanced applications without struggling with low-level details. For example --

  • Collision Detection, detect a graphics item is collided with another item.
  • Item Selection, gives us the ability to deal with multiple items at the same time, for example, the user can select multiple items, and when pressing delete, a function asks the scene to give the list for all selected items, and then delete them.
  • Items discovery, the scene can tell us what items are present (or part of them) at a specific point or inside some defined region, for example, if the user adds an item that intersects with a forbidden area, the program will detect them and give them another (mostly red) color.
  • Events Propagation, the scene receives the events and then propagates them to items.

To define a QGraphicsScene you define it's boundaries or sceneRect which defines the x & y origins and dimensions of the scene. If you don't provide a sceneRect it will default to the minimum bounding rectangle for all child items -- updating as items are added, moved or removed. This is flexible but less efficient.

Items in the scene are represented by QGraphicsItem objects. These are the basic building block of any 2D scene, representing a shape, pixmap or SVG image to be displayed in the scene. Each item has a relative position inside the sceneRect and can have different transformation effects (scale, translate, rotate, shear).

Finally, the QGraphicsView is the renderer of the scene, taking the scene and displaying it -- either wholly or in part -- to the user. The view itself can have transformations (scale, translate, rotate and shear) applied to modify the display without affecting the underlying scene. By default the view will forward mouse and keyboard events to the scene allowing for user interaction. This can be disabled by calling view.setInteractive(False).

A simple scene

Let's start by creating a simple scene. The following code creates QGraphicsScene, defining a 400 x 200 scene, and then displays it in a QGraphicsView.

python import sys from PySide6.QtWidgets import QGraphicsScene, QGraphicsView, QApplication app = QApplication(sys.argv) # Defining a scene rect of 400x200, with it's origin at 0,0. # If we don't set this on creation, we can set it later with .setSceneRect scene = QGraphicsScene(0, 0, 400, 200) view = QGraphicsView(scene) view.show() app.exec()

If you run this example you'll see an empty window.

The empty graphics scene, shown in a QGraphicsView window.

Not very exciting yet -- but this is our QGraphicsView displaying our empty scene.

As mentioned earlier, QGraphicsView is a widget. In Qt any widgets without a parent display as windows. This is why our QGraphicsView appears as a window on the desktop.

Adding items

Let's start adding some items to the scene. There are a number of built-in graphics items which you can customize and add to your scene. In the example below we use QGraphicsRectItem which draws a rectangle. We create the item passing in it's dimensions, and then set it's position pen and brush before adding it to the scene.

python import sys from PySide6.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsRectItem, QApplication from PySide6.QtGui import QBrush, QPen from PySide6.QtCore import Qt app = QApplication(sys.argv) # Defining a scene rect of 400x200, with it's origin at 0,0. # If we don't set this on creation, we can set it later with .setSceneRect scene = QGraphicsScene(0, 0, 400, 200) # Draw a rectangle item, setting the dimensions. rect = QGraphicsRectItem(0, 0, 200, 50) # Set the origin (position) of the rectangle in the scene. rect.setPos(50, 20) # Define the brush (fill). brush = QBrush(Qt.red) rect.setBrush(brush) # Define the pen (line) pen = QPen(Qt.cyan) pen.setWidth(10) rect.setPen(pen) scene.addItem(rect) view = QGraphicsView(scene) view.show() app.exec()

Running the above you'll see a single, rather ugly colored, rectangle in the scene.

A single rectangle in the scene

Adding more items is simply a case of creating the objects, customizing them and then adding them to the scene. In the example below we add an circle, using QGraphicsEllipseItem -- a circle is just an ellipse with equal height and width.

python import sys from PySide6.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsRectItem, QGraphicsEllipseItem, QApplication from PySide6.QtGui import QBrush, QPen from PySide6.QtCore import Qt app = QApplication(sys.argv) # Defining a scene rect of 400x200, with it's origin at 0,0. # If we don't set this on creation, we can set it later with .setSceneRect scene = QGraphicsScene(0, 0, 400, 200) # Draw a rectangle item, setting the dimensions. rect = QGraphicsRectItem(0, 0, 200, 50) # Set the origin (position) of the rectangle in the scene. rect.setPos(50, 20) # Define the brush (fill). brush = QBrush(Qt.red) rect.setBrush(brush) # Define the pen (line) pen = QPen(Qt.cyan) pen.setWidth(10) rect.setPen(pen) ellipse = QGraphicsEllipseItem(0, 0, 100, 100) ellipse.setPos(75, 30) brush = QBrush(Qt.blue) ellipse.setBrush(brush) pen = QPen(Qt.green) pen.setWidth(5) ellipse.setPen(pen) # Add the items to the scene. Items are stacked in the order they are added. scene.addItem(ellipse) scene.addItem(rect) view = QGraphicsView(scene) view.show() app.exec()

The above code will give the following result.

A scene with two items

The order you add items affects the stacking order in the scene -- items added later will always appear on top of items added first. However, if you need more control you can set the stacking order using .setZValue.

python ellipse.setZValue(500) rect.setZValue(200)

Now the circle (ellipse) appears above the rectangle.

Using Zvalue to order items in the scene

Try experimenting with setting the Z value of the two items -- you can set it before or after the items are in the scene, and can change it at any time.

Z in this context refers to the Z coordinate. The X & Y coordinates are the horizontal and vertical position in the scene respectively. The Z coordinate determines the relative position of items toward the front and back of the scene -- coming "out" of the screen towards the viewer.

There are also the convenience methods .stackBefore() and .stackAfter() which allow you to stack your QGraphicsItem behind, or in front of another item in the scene.

python ellipse.stackAfter(rect) Making items moveable

Our two QGraphicsItem objects are currently fixed in position where we place them, but they don't have to be! As already mentioned Qt's Graphics View framework allows items to respond to user input, for example allowing them to be dragged and dropped around the scene at will. Simple functionality like is actually already built in, you just need to enable it on each QGraphicsItem. To do that we need to set the flag QGraphicsItem.GraphicsItemFlags.ItemIsMoveable on the item.

The full list of graphics item flags is available here.

python import sys from PySide6.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsItem, QGraphicsRectItem, QGraphicsEllipseItem, QApplication from PySide6.QtGui import QBrush, QPen from PySide6.QtCore import Qt app = QApplication(sys.argv) # Defining a scene rect of 400x200, with it's origin at 0,0. # If we don't set this on creation, we can set it later with .setSceneRect scene = QGraphicsScene(0, 0, 400, 200) # Draw a rectangle item, setting the dimensions. rect = QGraphicsRectItem(0, 0, 200, 50) # Set the origin (position) of the rectangle in the scene. rect.setPos(50, 20) # Define the brush (fill). brush = QBrush(Qt.red) rect.setBrush(brush) # Define the pen (line) pen = QPen(Qt.cyan) pen.setWidth(10) rect.setPen(pen) ellipse = QGraphicsEllipseItem(0, 0, 100, 100) ellipse.setPos(75, 30) brush = QBrush(Qt.blue) ellipse.setBrush(brush) pen = QPen(Qt.green) pen.setWidth(5) ellipse.setPen(pen) # Add the items to the scene. Items are stacked in the order they are added. scene.addItem(ellipse) scene.addItem(rect) ellipse.setFlag(QGraphicsItem.ItemIsMovable) view = QGraphicsView(scene) view.show() app.exec()

In the above example we've set ItemIsMovable on the ellipse only. You can drag the ellipse around the scene -- including behind the rectangle -- but the rectangle itself will remain locked in place. Experiment with adding more items and configuring the moveable status.

If you want an item to be selectable you can enable this by setting the ItemIsSelectable flag, for example here using .setFlags() to set multiple flags at the same time.

python ellipse.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)

If you click on the ellipse you'll now see it surrounded by a dashed line to indicate that it is selected. We'll look at how to use item selection in more detail in a later tutorial.

A selected item in the scene, highlighted with dashed lines

Another way to create objects.

So far we've been creating items by creating the objects and then adding them to the scene. But you can also create an object in the scene directly by calling one of the helper methods on the scene itself, e.g. scene.addEllipse(). This creates the object and returns it so you can modify it as before.

python import sys from PySide6.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsRectItem, QApplication from PySide6.QtGui import QBrush, QPen from PySide6.QtCore import Qt app = QApplication(sys.argv) scene = QGraphicsScene(0, 0, 400, 200) rect = scene.addRect(0, 0, 200, 50) rect.setPos(50, 20) # Define the brush (fill). brush = QBrush(Qt.red) rect.setBrush(brush) # Define the pen (line) pen = QPen(Qt.cyan) pen.setWidth(10) rect.setPen(pen) view = QGraphicsView(scene) view.show() app.exec()

Feel free to use whichever form you find most comfortable in your code.

You can only use this approach for the built-in QGraphicsItem object types.

Building a more complex scene

So far we've built a simple scene using the basic QGraphicsRectItem and QGraphicsEllipseItem shapes. Now let's use some other QGraphicsItem objects to build a more complex scene, including lines, text and QPixmap (images).

python from PySide6.QtCore import QPointF, Qt from PySide6.QtWidgets import QGraphicsRectItem, QGraphicsScene, QGraphicsView, QApplication from PySide6.QtGui import QBrush, QPainter, QPen, QPixmap, QPolygonF import sys app = QApplication(sys.argv) scene = QGraphicsScene(0, 0, 400, 200) rectitem = QGraphicsRectItem(0, 0, 360, 20) rectitem.setPos(20, 20) rectitem.setBrush(QBrush(Qt.red)) rectitem.setPen(QPen(Qt.cyan)) scene.addItem(rectitem) textitem = scene.addText("QGraphics is fun!") textitem.setPos(100, 100) scene.addPolygon( QPolygonF( [ QPointF(30, 60), QPointF(270, 40), QPointF(400, 200), QPointF(20, 150), ]), QPen(Qt.darkGreen), ) pixmap = QPixmap("cat.jpg") pixmapitem = scene.addPixmap(pixmap) pixmapitem.setPos(250, 70) view = QGraphicsView(scene) view.setRenderHint(QPainter.Antialiasing) view.show() app.exec()

If you run the example above you'll see the following scene.

Scene with multiple items including a rectangle, polygon, text and a pixmap.

Let's step through the code looking at the interesting bits.

Polygons are defined using a series of QPointF objects which give the coordinates relative to the items position. So, for example if you create a polygon object with a point at 30, 20 and then move this polygon object X & Y coordinates 50, 40 then the point will be displayed at 80, 60 in the scene.

Points inside an item are always relative to the item itself, and item coordinates are always relative to the scene -- or the item's parent, if it has one. We'll take a closer look at the Graphics View coordinate system in the next tutorial.

To add an image to the scene we can open it from a file using QPixmap(). This creates a QPixmap object, which can then in turn add to the scene using scene.addPixmap(pixmap). This returns a QGraphicsPixmapItem which is the QGraphicsItem type for the pixmap -- a wrapper than handles displaying the pixmap in the scene. You can use this object to perform any changes to item in the scene.

The multiple layers of objects can get confusing, so it's important to choose sensible variable names which make clear the distinction between, e.g. the pixmap itself and the pixmap item that contains it.

Finally, we set the flag RenderHint,Antialiasing on the view to smooth the edges of diagonal lines. You almost always want to enable this on your views as otherwise any rotated objects will look very ugly indeed. Below is our scene without antialiasing enabled, you can see the jagged lines on the polygon.

Scene with antialiasing disabled.

Antialiasing has a (small) performance impact however, so if you are building scenes with millions of rotated items it may in some cases make sense to turn it off.

Adding graphics views to Qt layouts

The QGraphicsView is subclassed from QWidget, meaning it can be placed in layouts just like any other widget. In the following example we add the view to a simple interface, with buttons which perform a basic effect on the view -- raising and lowering selected item's ZValue. This has the effect of allowing us to move items in front and behind other objects.

The full code is given below.

python import sys from PySide6.QtCore import Qt from PySide6.QtGui import QBrush, QPainter, QPen from PySide6.QtWidgets import ( QApplication, QGraphicsEllipseItem, QGraphicsItem, QGraphicsRectItem, QGraphicsScene, QGraphicsView, QHBoxLayout, QPushButton, QSlider, QVBoxLayout, QWidget, ) class Window(QWidget): def __init__(self): super().__init__() # Defining a scene rect of 400x200, with it's origin at 0,0. # If we don't set this on creation, we can set it later with .setSceneRect self.scene = QGraphicsScene(0, 0, 400, 200) # Draw a rectangle item, setting the dimensions. rect = QGraphicsRectItem(0, 0, 200, 50) rect.setPos(50, 20) brush = QBrush(Qt.red) rect.setBrush(brush) # Define the pen (line) pen = QPen(Qt.cyan) pen.setWidth(10) rect.setPen(pen) ellipse = QGraphicsEllipseItem(0, 0, 100, 100) ellipse.setPos(75, 30) brush = QBrush(Qt.blue) ellipse.setBrush(brush) pen = QPen(Qt.green) pen.setWidth(5) ellipse.setPen(pen) # Add the items to the scene. Items are stacked in the order they are added. self.scene.addItem(ellipse) self.scene.addItem(rect) # Set all items as moveable and selectable. for item in self.scene.items(): item.setFlag(QGraphicsItem.ItemIsMovable) item.setFlag(QGraphicsItem.ItemIsSelectable) # Define our layout. vbox = QVBoxLayout() up = QPushButton("Up") up.clicked.connect(self.up) vbox.addWidget(up) down = QPushButton("Down") down.clicked.connect(self.down) vbox.addWidget(down) rotate = QSlider() rotate.setRange(0, 360) rotate.valueChanged.connect(self.rotate) vbox.addWidget(rotate) view = QGraphicsView(self.scene) view.setRenderHint(QPainter.Antialiasing) hbox = QHBoxLayout(self) hbox.addLayout(vbox) hbox.addWidget(view) self.setLayout(hbox) def up(self): """ Iterate all selected items in the view, moving them forward. """ items = self.scene.selectedItems() for item in items: z = item.zValue() item.setZValue(z + 1) def down(self): """ Iterate all selected items in the view, moving them backward. """ items = self.scene.selectedItems() for item in items: z = item.zValue() item.setZValue(z - 1) def rotate(self, value): """ Rotate the object by the received number of degrees """ items = self.scene.selectedItems() for item in items: item.setRotation(value) app = QApplication(sys.argv) w = Window() w.show() app.exec()

If you run this, you will get a window like that shown below. By selecting an item in the graphics view and then clicking either the "Up" or "Down" button you can move items up and down within the scene -- behind and in front of one another. The items are all moveable, so you can drag them around too. Clicking on the slider will rotate the currently selected items by the set number of degrees.

A graphics scene with some custom controls

The raising and lowering is handled by our custom methods up and down, which work by iterating over the currently selected items in the scene -- retrieved using scene.selectedItems() and then getting the items z value and increasing or decreasing it respectively.

python def up(self): """ Iterate all selected items in the view, moving them forward. """ items = self.scene.selectedItems() for item in items: z = item.zValue() item.setZValue(z + 1)

While rotation is handled using the item.setRotation method. This receives the current angle from the QSlider and again, applies it to any currently selected items in the scene.

python def rotate(self, value): """ Rotate the object by the received number of degrees. """ items = self.scene.selectedItems() for item in items: item.setRotation(value)

Take a look at the QGraphicsItem documentation for some other properties you can control with widgets and try extending the interface to allow you to change them dynamically.

Hopefully this quick introduction to the Qt Graphics View framework has given you some ideas of what you can do with it. In the next tutorials we'll look at how events and user interaction can be handled on items and how to create custom & compound items for your own scenes.

Categories: FLOSS Project Planets

Matt Layman: Postgres to SQLite - Building SaaS #204

Thu, 2024-09-26 20:00
In this episode, we worked on the cloud migration’s data strategy. I focused on how to convert a Postgres database into a SQLite database and planned out the process that I will follow to do the actual migration.
Categories: FLOSS Project Planets

PyCharm: Django Project Ideas

Thu, 2024-09-26 05:04

Ready to get started on your Django project? Whether you want to build a personal project, improve your portfolio, or just experiment with a new technology, Django provides the perfect foundation.

We’ve compiled a list of Django project ideas that will help you expand your competence, whether you’re new to the framework or looking to advance your existing skills.

In this guide, we’ll explore a range of Django project ideas to help you get hands-on experience, ranging from simple apps to more advanced, full-stack applications.

When should I use Django?

Known as the “web framework for perfectionists with deadlines”, Django is a great choice for building robust, production-ready websites, apps, or services.

It’s an open-source and highly versatile Python web framework that can be used for everything from blogs and social media platforms to e-commerce sites, content management systems, and fitness trackers.

First released in 2005, Django continues to receive regular updates and enjoys a considerable following. Its “batteries included” design means you get what you need to build a full-stack web app right out of the box. You can also pair Django with a front-end JavaScript framework for projects that demand richly interactive user interfaces.

Where to find Django project ideas

Working on a real-world project is one of the best ways to get to grips with Django. You can start with something simple and extend it as you learn more about the framework. In the process, you’ll develop your skills and build a portfolio of work showcasing your abilities.

Stuck for ideas? Previous projects (both professional and personal), forums, dev meetups and hackathons, as well as platforms like GitHub, can all provide sources of inspiration.

GitHub is also useful for hosting and collaborating on open-source projects – this is where the source code or design documents are made publicly available for anyone to access, modify, and distribute. You can use and build upon various software, making them a great jumping-off point for a Django project.

Some GitHub open-source Django projects include: 

  • Django packages: a directory of reusable apps, sites, tools, and more for your Django projects.
  • Django project: source code for djangoproject.com.
  • Django social: code for a social network or community site built using Django.

Are you unfamiliar with a particular area, such as routing or database design? You could choose to design a project that addresses this knowledge gap while learning Django simultaneously.

Django project ideas for beginners

If this is your first time building with Django, look no further. We’ve compiled a list of project ideas that are ideal for anyone using Django for the first time. These will help you find your feet as you explore what Django offers.

Before you start, reading this Django tutorial for beginners is useful. It walks through building a simple but non-trivial project by starting with a single file. The tutorial is an excellent starting point for anyone new to Django, as it introduces core concepts and helps you build a functional project from the ground up.

Once you’re familiar with Django’s principal elements and are comfortable building within the framework, you can get going with these more dynamic yet straightforward projects.

Air temperature app

This tutorial will walk you through creating a simple Django app using PyCharm that displays the current air temperature at your location. The app also allows users to explore weather conditions at random locations to add interactivity.

By following this guide, you’ll learn to:

  • Set up a Django project.
  • Develop models, views, and templates.
  • Make and process API calls.
  • Connect to and populate databases.

This hands-on task will give you practical experience with core Django concepts and show how PyCharm can help you develop more efficiently while working with Django. It’s a great place to begin building web applications and sets you up for further starter projects.

Follow the step-by-step tutorial Blog

Required tech stack:

  • Django admin: for managing blog posts.
  • Django templates: for rendering HTML pages.
  • TinyMCE or CKEditor (optional): for rich text editing.

Building your own blogging platform with Django is a great way to cover the basics of create, read, update, and delete (CRUD), setting up routes, and serving a site locally. To keep things simple, use the Django Admin site to add and update blog posts. To explore further, extend the basic blog functionality by implementing tags and filters, adding a search option, or enabling comments. 

We highly recommend the Django Girls Tutorial for beginners, which takes you through creating a small blog.

To-do list app

Required tech stack:

  • Django templates: for rendering the UI.
  • Single-page applications (SPA)/JavaScript: for updating the status of pages and components without reloading the page.

A simple task list with an option to check items off when complete is an excellent way to learn the basics of developing with Django. You’ll need a UI to add tasks and update their status and a simple database to record each entry. You can also add the ability to record due dates or set priorities.

Recipe organizer

Required tech stack:

  • Django templates: for rendering the frontend.
  • Django ORM: for database interactions.
  • JavaScript (optional): for additional security.

Familiarize yourself with Django’s ORM (object-relational mapper) and database support by building an app to keep track of your favorite recipes. Add a web-based frontend with options to filter recipes by category, ingredients, and user ratings so you can easily browse for inspiration.

Quiz app

Required tech stack:

  • Django templates: for rendering the UI.
  • Django ORM: for managing the question bank and user scores.
  • JavaScript: for interactive quiz elements.

Create a quiz app or website to familiarize yourself with each layer of Django’s architecture. Build a question bank, give players a simple UI to select their answer, and calculate the score. Enhance the functionality by sorting questions into categories and adding options to choose topics or change the number of questions asked. You could also add a leaderboard to celebrate your highest scorers.

Authentication system

Required tech stack:

  • Django built-in authentication: for handling login, registration, and password resetting.
  • Email service: for email validation and password reset (e.g. SMTP server).

Login systems are a feature of most websites and apps. Fortunately, adding authentication and authorization to a web app is easier with Django’s built-in authentication system. Creating one as a practice project helps familiarize yourself with the functionality and provides a reusable pattern for future projects. Don’t forget to include key features such as email validation and a password reset option.

URL shortener

Required tech stack:

  • Django templates: for the frontend.
  • Django ORM: for storing URLs.
  • Bitly API (optional): if using an external shortening service.

You can approach this project in a couple of ways, depending on your interests. One option is to build a simple app that sends requests to an existing URL-shortening service, such as Bitly’s API. Alternatively, create your own service by generating shortened URLs, storing the URLs in a database, and defining routing logic to match incoming requests to the appropriate URL. Add a simple frontend so you can generate new shortened URLs on demand.

Expense tracker

Required tech stack:

  • Django templates: for the UI.
  • Django ORM: for managing expense data.
  • Chart.js or D3.js: for visualizing expense trends.

Expense management apps provide an opportunity to define different data models for expenses, such as invoices, payroll, and cash expenditures. As well as creating forms for users to enter the costs, you might have existing data to import. Consider creating graphs to show expense trends in each category.

Simple social network

Required tech stack:

  • Django templates: for rendering the UI.
  • Django ORM: for managing user data and posts.
  • Media storage: for images and videos (e.g. Amazon S3 or local storage).
  • JavaScript: For interactive elements.

Use Django to create your version of Instagram or Facebook and learn about user account management, security, and privacy along the way. Users will need to be able to manage connections, share images and videos, and view each other’s posts.

Personal portfolio

Required tech stack:

  • Django templates: for the frontend.
  • Django ORM: for managing project data.
  • Deployment tools: for deploying the website (e.g. Heroku, AWS, or DigitalOcean).

Sharing personal projects on your website enables you to showcase your skills as a developer. Create an engaging website for users to browse your work and view the source code. This project will also allow you to try deploying Django to a production web server. 

Learn how to build a personal portfolio using Django with this helpful guide.

Event calendar

Required tech stack:

  • Django templates: for rendering the frontend.
  • JavaScript libraries: for interactive calendar functionalities (e.g. FullCalendar).
  • Django ORM: for storing event data.

An enjoyable beginner project with a frontend focus, building an event calendar gives you a reason to work with Django’s built-in templates or Jinja2. You can also combine Django with JavaScript libraries to make your calendar interactive.

Survey tool

Required tech stack:

  • Django templates: for the frontend.
  • Django forms: for creating and submitting surveys.
  • Django ORM: for storing survey data.
  • Chart.js or D3.js: for visualizing survey results.

An online survey tool requires UIs for building and responding to surveys, storage of the responses, and logic to aggregate answers. This project is an ideal opportunity to try out Django forms. If you want to extend it, add user authentication and/or visualizations for the survey data.

Advanced Django projects

If you’re familiar with Django or looking for more advanced project ideas, the following suggestions are for you. Get ideas to help you use more of Django’s rich capabilities and grow your developer skills.

Multi-user classifieds site

Build a site where multiple users can post classified ads. You’ll need to create pages for listing, creating, and editing ads. Since the site supports multiple users, you’ll also need to create and manage user accounts, manage permissions so only the ad owner can edit their ads, and create paginated ad listings to avoid overloading the browser. 

For the full experience, include a self-sign-up page and password reset management. For additional practice, consider adding an API to the site and/or using a frontend library like HTMX or Vue.js to make your pages more dynamic.

The book “Django In Action” guides you through building a musicians’ classified ad site called RiffMates. It also covers deeper topics such as testing, management commands, and Django power tools.

Chat app

Building a messaging app is an excellent way to explore real-time two-way communication using WebSockets and Django Channels. 

You’ll also need to create and manage user accounts, add contacts, and view message history. You can model the functionality on existing chat platforms like WhatsApp, X, or Telegram. Adding support for audio messages helps to extend your project further. 

This tutorial on Django Channels will help you create your chat app, going beyond the basics for a more comprehensive application. For an extra challenge, consider incorporating WebRTC and webcam video chat.

E-commerce store

Building an online retail site is a sizable project that demonstrates various skills. In addition to a database of products and a website that lets customers browse and search, you’ll need to handle user accounts, shopping carts, payments, and order updates. 

There is plenty to consider, from generating product image thumbnails to managing abandoned checkout processes. This project will help you understand advanced Django features like signals, middleware, and caching.

Medical records system

This project requires you to consider user authentication and authorization to ensure only those with the correct permissions can view a patient’s records. 

When defining your templates, ensure you present the data reverse chronologically (most recent first). You will also need to provide forms for authorized users to add new patients and update patients’ records. 

You may even connect to another system to import test results and associate them with the right patient, integrating third-party APIs for extended functionality.

Financial dashboard

Dashboards make fantastic projects for combining Django with a frontend JavaScript framework. You can pull in data from an API or add an option to import data manually from .csv files. For real-time data, use Django Channels. 

Leverage Django to manipulate the data in different ways and JavaScript to create visually appealing UIs. This project can also integrate with data visualization libraries such as D3.js or Chart.js for dynamic, interactive charts.

Property management system

Building a system to keep track of rented properties, record rent payments, and manage maintenance tasks and inspections is ideal for Django. 

This project gives you a chance to work with multiple data sources, implement complex business logic, and handle common functionality such as user authentication and notifications. Consider using the Django REST framework to create APIs that mobile apps or other systems can consume.

Interactive dashboards

Developing an interactive dashboard with Django is perfect for visualizing data from various sources. It also allows you to handle large datasets, perform real-time data updates, and integrate frontend visualization libraries with Django.

Use libraries like Plotly or Bokeh to create dynamic charts and graphs. Implement data filtering, time range selection, and report exporting. Add user authentication to provide personalized dashboards based on roles and permissions.

Stock exchange analysis

Build a stock market analysis platform using Django, a project that combines data science, machine learning, and the Django REST framework. It also offers more practical experience in handling lots of real-time data. 

Import historical stock prices, run technical analyses, and generate predictions using machine learning. Create RESTful APIs for data retrieval and analysis results, and integrate external APIs for real-time market data. Implement visualization tools to display trends, predictions, and alerts.

Processing large volumes of data

Use Django with Celery and Redis to process large datasets. This project will involve setting up task queues with Celery and managing them with Redis, helping you understand task scheduling and big data processing.

Implement features to handle server failures and ensure data integrity throughout the pipeline. Demonstrate how to scale the system by running Redis in cluster mode and balancing loads across multiple servers. Create dashboards to monitor task progress and performance metrics.

Generative AI RAG-based application

Django projects are a great way to gain practical experience working with language models. This project involves natural language processing and AI, helping to deepen your understanding.

Build a Django application for advanced AI-driven solutions using retrieval-augmented generation (RAG) techniques. Integrate pre-trained language models like GPT-3 or GPT-4 with a retrieval system to fetch relevant documents. Develop features for generating contextual responses, automated content creation, and personalized recommendations. Implement a feedback loop to continuously improve the system’s accuracy. 

AIOps for server scaling

Take on this assignment to help enhance your skills in AI, cloud computing, and automation.

Develop an AI-based system for predicting traffic and scaling servers automatically. Use machine learning models to analyze historical traffic data and forecast future loads. Integrate this system with cloud platforms like AWS or GCP to automatically provision and de-provision servers based on the predictions. Implement features for monitoring system performance, generating alerts, and providing dashboards for administrators. 

Customized dashboard for MLOps pipeline

Create a dashboard to manage and monitor machine learning pipelines, integrating tools like TensorFlow or PyTorch with Django. 

Allow users to track experiments, visualize training progress, and compare model performance. Implement features for managing datasets, configuring training parameters, and deploying models to production. Provide tools for monitoring model drift and triggering re-training when necessary. 

By the end of the project, you should understand the end-to-end machine learning lifecycle and how to operationalize ML models using Django.

Location-based food recommendation

Level up your skills in recommendation systems, geolocation services, and user personalization. This task involves using machine learning algorithms to recommend food options based on user location and preferences.

Integrate with mapping services like Google Maps or OpenStreetMap to provide location-based search and routing. Implement user profiles to store preferences, dietary restrictions, and past interactions. Use collaborative filtering or content-based filtering techniques to generate recommendations. Provide a user-friendly interface to browse and rate food options.

Langchain-based projects

Build applications utilizing Langchain for advanced natural language processing tasks

Create use cases such as automated summarization, question answering, or text generation. Integrate with Django to provide a web-based interface for users to interact with the system. Implement features for fine-tuning models on specific domains, evaluating model performance, and giving real-time predictions. 

Building these apps will help you explore the capabilities of state-of-the-art language models and how to integrate them with web applications.

Birthday message reminder

Developing a birthday message reminder is a fantastic project for helping to understand chatbot development and automating workflows with Django. You will implement a system using ChatGPT to generate unique birthday messages and send them to employees via Slack. 

Integrate with Slack’s API to send or direct messages to the appropriate channels. Use Django’s scheduling features to automate the message-sending process. Allow administrators to configure message templates, personalize messages based on employee data, and track message delivery. 

Cloud server management

Developing cloud servers is an ideal way to build your skills in cloud computing, infrastructure as code, and DevOps practices. You could begin by creating and managing cloud servers using Django and Pulumi. 

Allow users to provision and configure infrastructure as code, including virtual machines, databases, and networking resources. Implement features for monitoring resource usage, scaling infrastructure based on demand, and generating cost reports. Provide a web-based interface for administrators to manage cloud resources and view infrastructure status. 

Ready to get started?

We hope these project ideas have provided some inspiration to help you start building web apps with Django. Whatever your experience level, using an IDE optimized for your chosen language and framework makes software development more accessible and enjoyable. 

PyCharm Professional is a Python IDE with built-in support for Django, Flask, FastAPI, and more. Intelligent coding assistance, streamlined navigation, and powerful debugging are just some of the features that PyCharm offers to help you code faster and smarter.

Using PyCharm, you will also benefit from:

  • Advanced database management (including PostgreSQL, SQLite, Redis, and more).
  • Frontend support for various languages and frameworks (such as JavaScript, React, Node.js, Tailwind CSS, and more).
  • A built-in HTTP Client – essential for developing Django web applications.
  • The Endpoints tool window – to visualize and navigate through your application’s endpoints.

Use PyCharm’s full-stack development features and database tools to help you build powerful Django projects.

Get started with PyCharm now More tutorials and guides 
Categories: FLOSS Project Planets

Mike Driscoll: Textual – The New MaskedInput Widget

Wed, 2024-09-25 22:11

Textual v0.80.0 was released today, and it included the brand-new MaskedInput widget. If you have used other GUI toolkits, such as wxPython, you might already be familiar with a masked input widget. These widgets allow you to control the user’s input based on a mask string that the developer provides when instantiating the widget.

Let’s spend a few brief moments learning how this new widget works.

Getting the Latest Textual

Before you can use the MaskedInput widget, you must ensure you have version 0.80.0 or greater. If you have an older version, then you’ll need to upgrade by running the following command in your terminal:

python -m pip install textual --upgrade

Now that you have a 0.80.0 or greater, you can use this great widget!

Using the MaskedInput Widget

The Textual documentation has a credit card masked string for their demo application. Let’s start by looking at that example:

from textual.app import App, ComposeResult from textual.widgets import Label, MaskedInput class MaskedInputApp(App): # (1)! CSS = """ MaskedInput.-valid { border: tall $success 60%; } MaskedInput.-valid:focus { border: tall $success; } MaskedInput { margin: 1 1; } Label { margin: 1 2; } """ def compose(self) -> ComposeResult: yield Label("Enter a valid credit card number.") yield MaskedInput( template="9999-9999-9999-9999;0", # (2)! ) if __name__ == "__main__": app = MaskedInputApp() app.run()

The template in this code says that you want four groups of four digits. The template automatically adds a dash after each four digits except the last one.

When you run this code, you will see something like this:

Note that when the widget is empty or not completely filled out, there is a red outline around it. The widget will ignore all keys except for number keys. So if you press A-Z or any special characters like semi-colon or question mark, the MasketInput widget will ignore them and nothing appears in the widget.

When you have entered four groups of four integers though, you will see the red outline turn green. Here’s an example:

Note that this code does NOT actually validate that these are working credit card numbers. It only validates that there four groups of four integers.

A more common mask would be to create something to accept phone numbers. Let’s take the code above and rewrite the mask and the label text accordingly:

from textual.app import App, ComposeResult from textual.widgets import Label, MaskedInput class MaskedInputApp(App): # (1)! CSS = """ MaskedInput.-valid { border: tall $success 60%; } MaskedInput.-valid:focus { border: tall $success; } MaskedInput { margin: 1 1; } Label { margin: 1 2; } """ def compose(self) -> ComposeResult: yield Label("Enter a valid phone number.") yield MaskedInput( template="(999)-999-9999;0", ) if __name__ == "__main__": app = MaskedInputApp() app.run()

If you runt his code, the output it a little different, but the concept is the same:

The MaskedInput widget is using regular expressions to mask characters. If you look at the documentation you can see how it works. Following are a few examples from the documentation:

A [A-Za-z] Yes a [A-Za-z] No N [A-Za-z0-9] Yes n [A-Za-z0-9] No X [^ ] Yes x [^ ] No 9 [0-9] Yes

This widget may get additional updates to include more refined regular expressions, so it is definitely best to review the documentation for the latest information on the masked templates and how they work.

Wrapping Up

The MaskedInput widget is a great addition to the Textual TUI toolkit. There are lots of great widgets included with Textual now. While there may not be as many as wxPython or PyQt, those projects have been around for a couple of decades and have had much more time to mature. You can create really amazing and neat applications in your terminal with Python and Textual. You should try it out today!

The post Textual – The New MaskedInput Widget appeared first on Mouse Vs Python.

Categories: FLOSS Project Planets

Django Weblog: 2025 DSF Board Nominations

Wed, 2024-09-25 12:03

Nominations are open for the 2025 Django Software Foundation Board of Directors.

In 2023 we introduced a staggered term for directors. Of our 7 directors, there are 4 positions currently open, with each position serving for two years.

Decisions around the 2025 officer roles will be made during the meeting of the new board. You don’t need to specify which position you are nominating for.

As you know, the Board guides the direction of the marketing, governance and outreach activities of the Django community. We provide funding, resources, and guidance to Django events on a global level. Further we provide support to the Django community with an established Code of Conduct and make decisions and enforcement recommendations for violations. We work with our corporate and individual members to raise funds to help support our great community.

In order for our community to continue to grow and advance the Django Web framework, we need your help. The Board of Directors consists of seven volunteers who are elected to two year terms. This is an excellent opportunity to help advance Django. We can’t do it without volunteers, such as yourself. Anyone including current Board members, DSF Members, or the public at large can apply to the Board. It is open to all.

2025 DSF Board Nomination Form

If you are interested in helping to support the development of Django we’d enjoy receiving your application for the Board of Directors. Please fill out the 2025 DSF Board Nomination form by October 25, 2024 Anywhere on Earth to be considered.

If you have any questions about applying, the work, or the process in general please don’t hesitate to reach out via email to foundation@djangoproject.com.

Thank you for your time and we look forward to working with you in 2025.

The 2024 DSF Board of Directors.

Categories: FLOSS Project Planets

Real Python: Python 3.13 Preview: A Modern REPL

Wed, 2024-09-25 10:00

One of Python’s strong points is its interactive capabilities. By running python you start the interactive interpreter, or REPL, which allows you to perform quick calculations or explore and experiment with your code. In Python 3.13, the interactive interpreter has been completely redesigned with new modern features.

Python’s REPL has remained largely unchanged for decades. Instead, alternative interpreters like IPython, bpython, and ptpython have addressed some of the built-in REPL’s shortcomings, providing more convenient interactive workflows for developers. As you’re about to learn, Python 3.13 brings many significant improvements to the interactive interpreter.

In this tutorial, you’ll:

  • Run Python 3.13 and explore the new REPL
  • Browse through the help system
  • Work with multiline statements
  • Paste code into your REPL session
  • Navigate through your interpreter history

The upgraded REPL is just one of the new features coming in Python 3.13. You can read about all the changes in the what’s new section of Python’s changelog. Additionally, you can dig deeper into the work done on free threading and a Just-In-Time compiler.

Get Your Code: Click here to download the free sample code that shows you how to use some of the new features in Python 3.13.

A New Interactive Interpreter (REPL) in Python 3.13

To try out the new REPL for yourself, you need to get your hands on a version of Python 3.13. Before the official release in October 2024, you can install a pre-release version. After October 2024, you should be able to install Python 3.13 through any of the regular channels.

A REPL, or a Read-Eval-Print Loop, is a program that allows you to work with code interactively. The REPL reads your input, evaluates it, and prints the result before looping back and doing the same thing again.

Note: To learn about the built-in REPL in Python and how it works in general, check out The Python Standard REPL: Try Out Code and Ideas Quickly.

In any version of Python, you can start this interactive shell by typing the name of your Python executable in your terminal. Typically, this will be python, but depending on your operating system and your setup, you may have to use something like py or python3 instead.

Once you start the REPL in Python 3.13, you’ll see a small but noticeable difference. The familiar Python interactive shell prompt, consisting of three right angle brackets (>>>), is now colored differently from the rest of the text:

The color difference indicates that the shell now supports color. In the new shell, color is mainly used to highlight output in tracebacks. If your terminal doesn’t display color, then the new REPL will automatically detect this and fall back to its plain, colorless display.

If you prefer to keep your interpreter free of color even when it’s supported, you can disable this new feature. One option is to set the new environment variable PYTHON_COLORS to 0:

Shell $ PYTHON_COLORS=0 python Python 3.13.0rc2 (main, Sep 13 2024, 17:09:27) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> Copied!

Setting PYTHON_COLORS=0 disables color in the REPL. If you set the environment variable to 1, you’ll get the colored prompt and output. However, since it’s the default, this is rarely necessary.

Before going any further, you’ll exit the REPL. As you may know, the old REPL, that you’ve used on Python 3.12 and earlier, has been widely commented on for the following idiosyncrasy:

Python >>> exit Use exit() or Ctrl-D (i.e. EOF) to exit Copied!

The shell clearly understands your intention to end the session. Still, it makes you jump through hoops and add parentheses to your command. In Python 3.13, the REPL now understands special commands that you can write without any parentheses:

  • exit or quit: Exit the interpreter
  • help or F1: Access the help system
  • clear: Clear the screen

Having these commands more easily available is a small thing, but it removes some friction when using the interactive interpreter. You can still use parentheses and type something like exit() if you prefer. These commands are not reserved though. That means that you could shadow them with variable assignments:

Python >>> exit = True >>> exit True >>> exit() Traceback (most recent call last): ... TypeError: 'bool' object is not callable >>> del exit >>> exit Copied!

Here you create a variable named exit, effectively disabling the exit command. To end your REPL session, you can either delete the exit variable or use one of the alternative ways to exit the interpreter.

Note: Even with the new improvements, the built-in REPL is not as powerful as some of the third-party alternatives. If you’re already using any of these, there’s no immediate reason to revert back to the standard interactive interpreter.

The main advantage of the new REPL is that it’s installed together with Python. In other words, it’ll always be available for you. This is great if you find yourself in a situation where you can’t or don’t want to install a third-party library.

You can learn more about the alternative interactive interpreters in the following tutorials:

You can also read more about alternative REPLs in the guide to the standard REPL.

Read the full article at https://realpython.com/python313-repl/ »

[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Categories: FLOSS Project Planets

Mike Driscoll: JupyterLab 101 Kickstarter Stretch Goal

Wed, 2024-09-25 09:26

My Kickstarter for my latest Python book is still going on for another eight days. Now is a great time to pre-order the book as well as get my other Python books.

The project is fully funded, and I added a stretch goal.

Stretch Goal

The stretch goal is $5000 or 350 backers. If we reach either of those, I will create a video course version of the book and release it to all backers. I think being able to show JupyterLab in a video format may be an even better medium for teaching this type of topic, although the book may be better for quick browsing.

What You’ll Learn

In this book, you will learn how about the following:

  • Installation and setup of JupyterLab
  • The JupyterLab user interface
  • Creating a Notebook
  • Markdown in Notebooks
  • Menus in JupyterLab
  • Launching Other Applications (console, terminal, text files, etc)
  • Distributing and Exporting Notebooks
  • Debugging in JupyterLab
  • Testing your notebooks
Rewards to Choose From

As a backer of this Kickstarter, you have some choices to make. You can receive one or more of the following, depending on which level you choose when backing the project:

  • An early copy of JupyterLab 101 + all updates including the final version (ALL BACKERS)
  • signed paperback copy (If you choose the appropriate perk)
  • Get all by Python courses hosted on Teach Me Python or another site  (If you choose the appropriate perk)
  • T-shirt with the book cover  (If you choose the appropriate perk)

Get the book on Kickstarter today!

The post JupyterLab 101 Kickstarter Stretch Goal appeared first on Mouse Vs Python.

Categories: FLOSS Project Planets

Talk Python to Me: #478: When and how to start coding with kids

Wed, 2024-09-25 04:00
Do you have kids? Maybe nieces and nephews? Or maybe you work in a school environment? Maybe it's just friend's who know you're a programmer and ask about how they should go about introducing programming concepts with them. Anna-Lena Popkes is back on the show to share her research on when and how to teach kids programming. We spend the second half of the episode talking about concrete apps and toys you might consider for each age group. Plus, some of these things are fun for adults too. ;)<br/> <br/> <strong>Episode sponsors</strong><br/> <br/> <a href='https://talkpython.fm/workos'>WorkOS</a><br> <a href='https://talkpython.fm/training'>Talk Python Courses</a><br/> <br/> <strong>Links from the show</strong><br/> <br/> <div><b>Anna-Lena</b>: <a href="https://alpopkes.com?featured_on=talkpython" target="_blank" >alpopkes.com</a><br/> <br/> <b>Magical universe repo</b>: <a href="https://github.com/zotroneneis/magical_universe?featured_on=talkpython" target="_blank" >github.com</a><br/> <b>Machine learning basics repo</b>: <a href="https://github.com/zotroneneis/machine_learning_basics?featured_on=talkpython" target="_blank" >github.com</a><br/> <br/> <b>PyData recording "when and how to start coding with kids"</b>: <a href="https://www.youtube.com/embed/aTum4tV4geY" target="_blank" >youtube.com</a><br/> <br/> <b>Robots and devices</b><br/> <b>Bee Bot</b>: <a href="https://www.terrapinlogo.com/beebot-ss.html?featured_on=talkpython" target="_blank" >terrapinlogo.com</a><br/> <b>Cubelets</b>: <a href="https://modrobotics.com?featured_on=talkpython" target="_blank" >modrobotics.com</a><br/> <b>BBC Microbit</b>: <a href="https://microbit.org?featured_on=talkpython" target="_blank" >microbit.org</a><br/> <b>RaspberryPi</b>: <a href="https://www.raspberrypi.com?featured_on=talkpython" target="_blank" >raspberrypi.com</a><br/> <b>Adafruit Qualia ESP32 for CircuitPython</b>: <a href="https://www.adafruit.com/product/5800?featured_on=talkpython" target="_blank" >adafruit.com</a><br/> <b>Zumi</b>: <a href="https://www.robolink.com/products/zumi?featured_on=talkpython" target="_blank" >robolink.com</a><br/> <br/> <b>Board games</b><br/> <b>Think Fun Robot Turtles Board Game</b>: <a href="https://www.amazon.com/Think-Fun-Turtles-Coding-Preschoolers/dp/B00HN2BXUY/?featured_on=talkpython" target="_blank" >amazon.com</a><br/> <br/> <b>Visual programming:</b><br/> <b>Scratch Jr.</b>: <a href="https://www.scratchjr.org?featured_on=talkpython" target="_blank" >scratchjr.org</a><br/> <b>Scratch</b>: <a href="https://www.scratch.org?featured_on=talkpython" target="_blank" >scratch.org</a><br/> <b>Blocky</b>: <a href="https://developers.google.com/blockly?featured_on=talkpython" target="_blank" >google.com</a><br/> <b>Microbit's Make Code</b>: <a href="https://makecode.microbit.org?featured_on=talkpython" target="_blank" >microbit.org</a><br/> <b>Code Club</b>: <a href="https://codeclubworld.org?featured_on=talkpython" target="_blank" >codeclubworld.org</a><br/> <br/> <b>Textual programming</b><br/> <b>Code Combat</b>: <a href="https://codecombat.com?featured_on=talkpython" target="_blank" >codecombat.com</a><br/> <b>Hedy</b>: <a href="https://www.hedycode.com?featured_on=talkpython" target="_blank" >hedycode.com</a><br/> <b>Anvil</b>: <a href="https://anvil.works?featured_on=talkpython" target="_blank" >anvil.works</a><br/> <br/> <b>Coding classes / summer camps (US)</b><br/> <b>Portland Community College Summer Teen Program</b>: <a href="https://www.pcc.edu/community/special-classes/teen/?featured_on=talkpython" target="_blank" >pcc.edu</a><br/> <b>Watch this episode on YouTube</b>: <a href="https://www.youtube.com/watch?v=bPaseFf1Hio" target="_blank" >youtube.com</a><br/> <b>Episode transcripts</b>: <a href="https://talkpython.fm/episodes/transcript/478/when-and-how-to-start-coding-with-kids" target="_blank" >talkpython.fm</a><br/> <br/> <b>--- Stay in touch with us ---</b><br/> <b>Subscribe to us on YouTube</b>: <a href="https://talkpython.fm/youtube" target="_blank" >youtube.com</a><br/> <b>Follow Talk Python on Mastodon</b>: <a href="https://fosstodon.org/web/@talkpython" target="_blank" ><i class="fa-brands fa-mastodon"></i>talkpython</a><br/> <b>Follow Michael on Mastodon</b>: <a href="https://fosstodon.org/web/@mkennedy" target="_blank" ><i class="fa-brands fa-mastodon"></i>mkennedy</a><br/></div>
Categories: FLOSS Project Planets

PyCoder’s Weekly: Issue #648 (Sept. 24, 2024)

Tue, 2024-09-24 15:30

#648 – SEPTEMBER 24, 2024
View in Browser »

Python 3.13 Preview: Free Threading and a JIT Compiler

Get a sneak peek at the upcoming features in Python 3.13 aimed at enhancing performance. In this tutorial, you’ll make a custom Python build with Docker to enable free threading and an experimental JIT compiler. Along the way, you’ll learn how these features affect the language’s ecosystem.
REAL PYTHON

Let’s Build and Optimize a Rust Extension for Python

Python code too slow? You can quickly create a Rust extension to speed it up. This post shows you how to re-implement some Python code as Rust, connect the Rust to Python, and optimize the Rust for even further performance improvements.
ITAMAR TURNER-TRAURING

Join John Hammond & Daniel Miessler at DevSecCon 2024

Snyk is thrilled to announce DevSecCon 2024, Developing AI Trust Oct 8-9, a FREE virtual summit designed for DevOps, developer and security pros of all levels. Hear from industry pros John Hammond & Daniel Miessler for some practical tips on devsecops approach to secure development. Save our spot →
SNYK.IO sponsor

Document Intended Usage Through Tests With doctest

This post covers Python’s doctest which allows you to write tests within your code’s docstrings. This does two things: it gives you tests, but it also documents how your code can be used.
JUHA-MATTI SANTALA

Quiz: Python 3.13: Free-Threading and a JIT Compiler

REAL PYTHON

Quiz: Lists vs Tuples in Python

REAL PYTHON

Articles & Tutorials Customizing VS Code Through Color Themes

A well-designed coding environment enhances your focus and productivity and makes coding sessions more enjoyable. In this Code Conversation, your instructor Philipp Ascany will guide you step-by-step through the process of finding, installing, and adjusting color themes in VS Code.
REAL PYTHON course

Thriving as a Developer With ADHD

What are strategies for being a productive developer with ADHD? How can you help your team members with ADHD to succeed and complete projects? This week on the show, we speak with Chris Ferdinandi about his website and podcast “ADHD For the Win!”
REAL PYTHON podcast

Build AI Apps with More Flexibility for the Edge

Intel makes it easy to develop AI applications on open frameworks and libraries. Seamlessly integrate into an open ecosystem and build AI solutions with more flexibility and choice. Explore the possibilities available with Intel’s OpenVINO toolkit →
INTEL CORPORATION sponsor

Things I’ve Learned Serving on the Board of the PSF

Simon has been on the Python Software Foundation Board for two years now and has recently returned from a board retreat. This post talks about what he has learned along the way, including just what does the PSF do, PyPI, PyCons, and more.
SIMON WILLISON

Goodhart’s Law in Software Engineering

Goodhart’s law states: “When a measure becomes a target, it ceases to be a good measure.” Whether that’s test coverage, cyclomatic complexity, or code performance, all metrics are proxies and proxies can be gamed.
HILLEL WAYNE

AI-Extracted Asian Building Footprints

This post covers how Mark did a deep dive on a large dataset covering building footprints in Asia. He uses a variety of tools including duckdb and the multiprocessing module in Python.
MARK LITWINTSCHIK

Why We Wrote a New Form Library for Django

“Django comes with a form library, and yet we wrote a total replacement library… Django forms were fundamentally not usable for what we wanted to do.”
KODARE.NET

Case-Insensitive String Class

This article shows how you can create a case-insensitive string class using some basic meta programming with the dunder method __new__.
RODRIGO GIRÃO SERRÃO

7 Ways to Use Jupyter Notebooks Inside PyCharm

Discover seven ways you can use Jupyter notebooks in PyCharm to explore and work with your data more quickly and effectively.
HELEN SCOTT

Unified Python Packaging With uv

Talk Python interviews Charlie Marsh, the maintainer of ruff, and they talk about uv and other projects at Astral.
KENNEDY & MARSH podcast

It’s Time to Stop Using Python 3.8

Python 3.8 will stop getting security updates in November 2024. You really should upgrade!
ITAMAR TURNER-TRAURING

Projects & Code LightAPI: Lightweight API Framework

GITHUB.COM/IKLOBATO

peepdb: CLI Tool to View Database Tables

GITHUB.COM/EVANGELOSMEKLIS

dante: Zero-Setup Document Store for Python

GITHUB.COM/SENKO

cookiecutter-uv: A Modern Template for Python Projects

GITHUB.COM/FPGMAAS • Shared by Florian Maas

Django Content Settings: Advanced Admin Editable Settings

DJANGO-CONTENT-SETTINGS.READTHEDOCS.IO • Shared by oduvan

Events Weekly Real Python Office Hours Q&A (Virtual)

September 25, 2024
REALPYTHON.COM

PyData Paris 2024

September 25 to September 27, 2024
PYDATA.ORG

PiterPy 2024

September 26 to September 27, 2024
PITERPY.COM

PyConf Mini Davao 2024

September 26 to September 27, 2024
DURIANPY.ORG

PyCon JP 2024

September 27 to September 30, 2024
PYCON.JP

PyCon Niger 2024

September 28 to September 30, 2024
PYCON.ORG

PyConZA 2024

October 3 to October 5, 2024
PYCON.ORG

PyCon ES 2024

October 4 to October 6, 2024
PYCON.ORG

Happy Pythoning!
This was PyCoder’s Weekly Issue #648.
View in Browser »

[ Subscribe to 🐍 PyCoder’s Weekly 💌 – Get the best Python news, articles, and tutorials delivered to your inbox once a week >> Click here to learn more ]

Categories: FLOSS Project Planets

Real Python: Advanced Python import Techniques

Tue, 2024-09-24 10:00

In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.

This video course provides a comprehensive overview of Python’s import statement and how it works. The import system is powerful, and this course will teach you how to harness this power. While you’ll cover many of the concepts behind Python’s import system, this video course is mostly example driven, so you’ll learn from the numerous code examples shared throughout.

In this video course, you’ll learn how to:

  • Use modules, packages, and namespace packages
  • Manage namespaces and avoid shadowing
  • Avoid circular imports
  • Import modules dynamically at runtime
  • Customize Python’s import system

[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Categories: FLOSS Project Planets

PyCharm: PyCharm vs. Jupyter Notebook

Tue, 2024-09-24 08:52

Jupyter notebooks are an important tool for data scientists, providing an easy option for conducting experiments and presenting results. According to our Developer Ecosystem Survey 2023, at least 35% of data professionals use Jupyter notebooks. Furthermore, over 40% of these users spend more than 20% of their working time using these resources.

There are several implementations of notebook technology available to data professionals. At first we’ll look at the well-known Jupyter Notebook platforms by Project Jupyter. For the purposes of this article, we’ll refer to the Project Jupyter implementations of notebooks as “vanilla Jupyter” in order to avoid confusion, since there are several other implementations of the tool.

While vanilla Jupyter notebooks can be sufficient for some tasks, there are other cases where it would be better to rely on another tool for working with data. In this article, we’ll outline the key differences between PyCharm Professional and vanilla Jupyter when it comes to data science applications.

What is Jupyter Notebook?

Jupyter Notebook is an open-source platform that allows users to create and share code, visualizations, and text. It’s primarily used for data analysis and scientific research. Although JupyterLab offers some plugins and tools, its capabilities and user experience are significantly more limited than PyCharm’s.

What is PyCharm Professional?

PyCharm is a comprehensive integrated development environment (IDE) that supports a wide range of technologies out of the box, offering deep integration between them. In addition to enhanced support for Jupyter notebooks, PyCharm Professional also provides superior database support, Python script editing, and GitHub integration, as well as support for AI Assistant, Hugging Face, dbt-Core, and much more. 

Feature comparison: PyCharm Pro vs. Jupyter Language support

While Jupyter notebooks claim to support over 40 programming languages, their usage is limited to the .ipynb format, which makes working with traditional file extensions like .py, .sql, and others less convenient. On the other hand, while PyCharm offers support for fewer languages – Python, JavaScript and TypeScript, SQL, and R (via plugin), along with several markup languages like HTML and CSS – the support is much more comprehensive.

Often, Jupyter notebooks and Python scripts serve different purposes. Notebooks are typically used for prototyping and experimentation, while Python scripts are more suitable for production. In PyCharm Professional, you can work with both of these formats and it’s easy to convert .ipynb files into .py files. See the video below for more information.

The smartest code completion

If you’ve ever written code in PyCharm Professional, you’ll have definitely noticed its code completion capabilities. In fact, the IDE offers several different types of code completion. In addition to the standard JetBrains completion, which provides suggestions based on an extensive understanding of your project and libraries, there’s also runtime completion, which can suggest names of data objects like columns in a pandas or Polars DataFrame, and ML-powered, full-line completion that suggests entire lines of code based on the current file. Additionally, you can enhance these capabilities with LLM-powered tools such as JetBrains AI Assistant, GitHub Copilot, Amazon Whisper, and others.

In contrast, code completion in Jupyter notebooks is limited. Vanilla Jupyter notebooks lack awareness of your project’s context, there’s no local ML-based code completion, and there’s no runtime code completion for database objects.

Code quality features and debugger

PyCharm offers several tools to enhance your code quality, including smart refactorings, quick-fixes, and AI Assistant – none of which are available in vanilla Jupyter notebooks. 

If you’ve made a mistake in your code, PyCharm Professional will suggest several actions to fix it. These become visible when you click on the lightbulb icon.

PyCharm Professional also inspects code on the file and project level. To see all of the issues you have in your current file, you can click on the image in the top right-hand corner.

While vanilla Jupyter notebooks can highlight any issues after a code cell has been executed (as seen below), it doesn’t have features that allow you to analyze your entire file or project.   

PyCharm provides a comprehensive and advanced debugging environment for both Python scripts and Jupyter notebooks. This debugger allows you to step into your code, running through the execution steps line by line, and pinpointing exactly where an error was made. If you’ve never used the debugger in PyCharm, you can learn how to debug a Jupyter notebook in PyCharm with the help of this blog by Dr. Jodie Burchell. In contrast, vanilla Jupyter offers basic debugging tools such as cell-by-cell execution and interactive %debug commands.

Refactorings

Web-based Jupyter notebooks lack refactoring capabilities. If you need to rename a variable, introduce a constant, or perform any other operation, you have to do it manually, cell by cell. In PyCharm Professional, you can access the Refactoring menu via Control + T and use it to make changes in your file faster. More information about refactorings in PyCharm you can find in the video.

Other code-related features

If you forget how to work with a library in vanilla Jupyter notebooks, you need to open another tab in a browser to look up the documentation, taking you out of your development environment and programming flow.

In PyCharm Professional, you can get information about a function or library you’re currently using right in the IDE by hovering over the code.

If you have a subscription to AI Assistant you can also use it for troubleshooting, such as asking it to explain code and runtime errors, as well as finding potential problems with your code before you run it.

Working with tables

DataFrames are one of the most important types of data formats for the majority of data professionals. In vanilla Jupyter notebooks, if you print a pandas or Polars DataFrame, you’ll see a static, output with a limited number of columns and rows shown. Since the DataFrame outputs in Jupyter notebooks are static, this makes it difficult to explore your data without writing additional code.

In PyCharm Professional, you can use interactive tables that allow you to easily view, navigate, sort, and filter data. You can create charts and access essential data insights, including descriptive statistics and missing values – all without writing a single line of code. 

What’s more, the interactive tables are designed to give you a lot of information about your data, including details of: 

  • Data type symbols in the column headers
  • The size of your DataFrame (in our case it is 2390 rows and 82 columns). 
  • Descriptive statistics and missing values and many more. 

If you want to get more information about how interactive tables work in PyCharm, check out the documentation.

Versioning and GitHub integration

In PyCharm Professional, you have several version control options, including Git. 

With PyCharm’s GitHub integration, you can see and revert your changes with the help of the IDE’s built-in visual diff tool. This enables you to compare changes between different commits of your notebooks. You can find an in-depth overview of the functionality in this tutorial.

Another incredibly useful feature is the local history, which automatically saves a version history of your changes. This means that if you haven’t committed something, and you need to roll back to an earlier version, you can do so with the click of a button.  

In vanilla Jupyter notebooks, you have to rely on the CLI git tool. In addition, Git is the only way of versioning your work, meaning there is no way to revert changes if you haven’t committed them.

Navigation

When you work on your project in Jupyter Notebook, you always need to navigate either within a given file or the whole project. On the other hand, the navigation functionality in PyCharm is significantly richer.

 Beyond the Structure view that is also present in JupyterLab, you can also find some additional features to navigate your project in our IDEs. For example, double pressing Shift will help you find anything in your project or settings.

In addition to that, you can find the specific source of code in your project using PyCharm Professional’s Find Usages, Go to Implementation, Go to Declaration, and other useful features. 

Check out this blog post for more information about navigation in Jupyter notebooks in PyCharm.

Visualizations

In addition to libraries that are available in vanilla Jupyter notebooks, PyCharm also provides further visualization possibilities with the help of interactive tables. This means you don’t have to remember or type boilerplate code to create graphs. 

How to choose between PyCharm and vanilla Jupyter notebooks

Vanilla Jupyter notebooks are a lightweight tool. If you need to do some fast experiments, it makes sense to use this implementation. 

On the other hand, PyCharm Professional is a feature-rich IDE that simplifies the process of working with Jupyter notebooks. If you need to work with complex projects with a medium or large codebase, or you want to add a significant boost to productivity, PyCharm Professional is likely to be more suitable, allowing you to complete your data project more smoothly and quickly .

Get started with PyCharm Professional

PyCharm Professional is a data science IDE that supports Python, rich databases, Jupyter, Git, Conda, and other technologies right out of the box. Work on projects located in local or remote development environments. Whether you’re developing data pipelines, prototyping machine learning models, or analyzing data, PyCharm equips you with all the tools you need.

The best way to understand the difference between tools is to try them for yourself. We strongly recommend downloading PyCharm and testing it in your real-life projects. 

Download PyCharm Professional and get an extended 60-day trial by using the promo code “PyCharmNotebooks”. The free subscription is available for individual users only.

Activate your 60-day trial

Below are other blog posts that you might find useful for boosting your productivity with PyCharm Professional.

Categories: FLOSS Project Planets

Python Software Foundation: Service Awards given by the PSF: what are they and how they differ

Tue, 2024-09-24 05:00

Do you know someone in the Python community who inspires you and whose contributions to the Python community are outstanding? Other than saying thank you (definitely do this too!), you can also nominate them to receive recognition given by the PSF. In this blog post, we will explain what each of the awards are and how they differ. We hope this will encourage you to nominate your favorite inspirational community member to receive an award!

PSF Community Service Awards

The most straightforward way to acknowledge someone’s volunteer effort serving the Python community is to nominate them for the PSF Community Service Awards (CSA). The awardee will receive:


  • A cash award of $599 USD

  • Free registration at all future PyCon US events


Recipients need not be PSF members and can receive multiple awards if they have continuous outstanding contributions. Other than individuals, there are also small organizational groups (e.g. PyCon JP Association 2021) who can receive the CSA award.


The PSF Board reviews nominations quarterly. CSA recipients will be recognized at PyCon US every year.

 

CSA Award Winners 

The PSF Community Service Awards are all about the wonderful and dedicated folks in our community, and we had to take this opportunity to show some of their faces! You can find all of the inspiring PSF CSA recipients on our CSA webpage.  

 

CSA Recipients (left to right, top): Jessica Upani, Mariatta Wijaya, Abigail Mesrenyame Dogbe, Lais Carvalho, Mason Egger CSA Recipients (left to right, bottom): Kojo Idrissa, Tereza Iofciu, Jessica Greene, Carol Willing, Vicky Twomey-Lee 
PyCon JP Association CSA Recipients (left to right): Takayuki Shimizukawa,
Shunsuke Yoshida, Jonas Obrist, Manabu Terada, Takanori SuzuPSF Distinguished Service Awards

As the highest award that the PSF bestows, the Distinguished Service Award is the level up of the CSA award described above. Recipients of a DSA need to have made significant, sustained, and exemplary contributions with an exceptionally positive impact on the Python community. Recognition will take the form of an award certificate plus a cash award of $5000 USD. As of the writing of this blog post, there are only 7 awardees of the DSA in history.


Naomi Ceder is the latest Distinguished Service Awards recipient, she received the award in 2022

PSF Fellow Membership

Although it is also a form of recognition, the PSF Fellow Membership is different from the awards above and there’s no comparison of the level of recognition between fellowship and any of the awards above. Fellows are members who have been nominated for their extraordinary efforts and impact upon Python, the community, and the broader Python ecosystem. Fellows are nominated from the broader community and if they meet Fellow criteria, they are elevated by a vote of the Fellows Working Group. PSF Fellows are lifetime voting members of the PSF. That means Fellows are eligible to vote in PSF elections as well as follow the membership rules and Bylaws of the PSF. 

Nominate someone!

We hope this makes the types of recognition given by the PSF clear, as well as gives you confidence in nominating folks in the Python community that you think should be recognized for a CSA, DSA, or as a PSF Fellow. We also hope that this will inspire you to become a Python community member that receives a service award!


Categories: FLOSS Project Planets

Real Python: Python Virtual Environments: A Primer

Mon, 2024-09-23 10:00

In this tutorial, you’ll learn how to work with Python’s venv module to create and manage separate virtual environments for your Python projects. Each environment can use different versions of package dependencies and different versions of Python.

Once you’ve learned to work with virtual environments, you’ll be able to help other programmers reproduce your development setup and make sure that your projects never create dependency conflicts.

By the end of this tutorial, you’ll know how to:

  • Create and activate a Python virtual environment
  • Explain why you want to isolate external dependencies
  • Visualize what Python does when you create a virtual environment
  • Customize your virtual environments using optional arguments to venv
  • Deactivate and remove virtual environments
  • Choose additional tools for managing your Python versions and virtual environments

Working with virtual environments is a common and effective technique used in Python development. Gaining a better understanding of how they work, why you need them, and what you can do with them will help you master your Python programming workflow.

Throughout the tutorial, you can select code examples for either Windows, Ubuntu Linux, or macOS. Pick your platform at the top right of the relevant code blocks to get the commands that you need, and feel free to switch between them if you want to learn how to work with virtual environments on other operating systems.

Free Bonus: Click here to download a free cheat sheet that summarizes the main venv commands you’ll learn about in this tutorial.

Take the Quiz: Test your knowledge with our interactive “Python Virtual Environments: A Primer” quiz. You’ll receive a score upon completion to help you track your learning progress:

Interactive Quiz

Python Virtual Environments: A Primer

In this quiz, you'll test your understanding of Python virtual environments. With this knowledge, you'll be able to avoid dependency conflicts and help other developers reproduce your development environment.

How Can You Work With a Python Virtual Environment?

If you just need to get a virtual environment up and running to continue working on your favorite project, then this section is for you.

This tutorial uses Python’s venv module to create virtual environments. This module is part of Python’s standard library, and it’s been the officially recommended way to create virtual environments since Python 3.5.

Note: There are other great third-party tools for creating virtual environments, such as conda and virtualenv, that you’ll learn more about later in this tutorial. Either of these tools can help you set up a virtual environment and also go beyond just that.

For basic usage, venv is an excellent choice because it already comes packaged with your Python installation. With that in mind, you’re ready to create your first virtual environment.

Create It

Any time you’re working on a Python project that uses external dependencies you’re installing with pip, it’s best to first create a virtual environment:

Windows PowerShell PS> py -m venv venv\ Copied!

This command allows the Python launcher for Windows to select an appropriate version of Python to execute. It comes bundled with the official installation and is the most convenient way to execute Python on Windows.

You can bypass the launcher and run the Python executable directly using the python command, but if you haven’t configured the PATH and PATHEXT variables, then you might need to provide the full path:

Windows PowerShell PS> C:\Users\Name\AppData\Local\Programs\Python\Python312\python -m venv venv\ Copied!

The system path shown above assumes that you installed Python 3.12 using the Windows installer provided by the Python downloads page. The path to the Python executable on your system might be different. Working with PowerShell, you can find the path using the where.exe python command.

Note: You don’t need to include the backslash (\) at the end of the name of your virtual environment, but it’s a helpful reminder that you’re creating a folder.

Shell $ python3 -m venv venv/ Copied!

Many Linux operating systems ship with a version of Python 3. If python3 doesn’t work, then you’ll have to first install Python and you may need to use the specific name of the executable version that you installed, for example, python3.12 for Python 3.12.x. If that’s the case for you, remember to replace mentions of python3 in the code blocks with your specific version number.

Note: You don’t need to include the slash (/) at the end of the name of your virtual environment, but it’s a helpful reminder that you’re creating a folder.

Shell $ python3 -m venv venv/ Copied!

Older versions of macOS come with a system installation of Python 2.7.x that you should never use to run your scripts. If you’re working on macOS < 12.3 and invoke the Python interpreter with python instead of python3, then you might accidentally start up the outdated system Python interpreter.

If running python3 doesn’t work, then you’ll have to first install a modern version of Python.

Note: You don’t need to include the slash (/) at the end of the name of your virtual environment, but it’s a helpful reminder that you’re creating a folder.

This command creates a new virtual environment named venv using Python’s built-in venv module. The first venv that you use in the command specifies the module, and the second venv/ sets the name for your virtual environment. You could name it differently, but calling it venv is a good practice for consistency.

Activate It

Great! Your project now has its own virtual environment. Generally, before you start to use it, you’ll activate the environment by executing a script that comes with the installation:

Windows PowerShell PS> venv\Scripts\activate (venv) PS> Copied!

If your attempt to run this command produces an error, then you’ll first have to loosen the execution policy.

Shell $ source venv/bin/activate (venv) $ Copied!

Before you run this command, make sure that you’re in the folder containing the virtual environment you just created. If you’ve named your virtual environment something other than venv, then you’ll have to use that name in the path instead of venv when you source the activation script.

Note: You can also work with your virtual environment without activating it. To do this, you provide the full path to its Python interpreter when executing a command. However, you’ll likely want to activate the virtual environment after you create it to save yourself the effort of having to repeatedly type long pathnames.

Once you can see the name of your virtual environment in your command prompt—in this case (venv)—then you’ll know that your virtual environment is active. Now you’re all set and ready to install your external packages!

Read the full article at https://realpython.com/python-virtual-environments-a-primer/ »

[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Categories: FLOSS Project Planets

Pages