





















































Hi ,
Welcome to a brand new issue of PythonPro!
In today’sExpert Insight we bring you an excerpt from the book, Mastering spaCy, whichintroduces displaCy, spaCy's built-in visualizer, demonstrating how to visualize syntactic structures and named entities through interactive demos, local servers, and Jupyter notebooks.
News Highlights: LM Studio launches open-source SDKs for Python and TypeScript with an .act()
API for AI tasks; Python now runs on ESP32-S3 with NuttX RTOS, enabling direct hardware interaction; and a malicious Python package ‘set-utils’ targeted Ethereum wallets, exfiltrating keys via the Polygon RPC endpoint.
My top 5 picks from today’s learning resources:
And, in From the Cutting Edge, we introduce optimizn, a Python library that helps users easily build customized optimization algorithms—such as simulated annealing and branch and bound—for efficiently solving complex combinatorial optimization problems with features like continuous training and customisation capabilities.
Stay awesome!
Divya Anne Selvaraj
Editor-in-Chief
lmstudio-python
andlmstudio-js
: LM Studio's open-source SDKs enable developers to integrate advanced AI capabilities into Python and TypeScript applications, featuring an innovative .act()
API for agent-oriented tasks and support for complex system management features.itertools.product()
and itertools.starmap()
in a narrative about Yteria, who must adapt her coding strategies after losing the ability to use the word "for" due to a mugging.help()
function, detailing its ability to provide documentation for functions, modules, classes, and other objects directly within the Python environment.In "optimizn: a Python Library for Developing Customized Optimization
Algorithms," Sathiya and Pandey from Azure Core Insights Data Science, introduces a Python library designed to facilitate the creation of tailored optimization algorithms for combinatorial problems.
Combinatorial optimization involves selecting the best possible arrangement or ordering of a finite set of elements, crucial for solving complex operational and logistical issues in various industries, including cloud computing. Such problems are often classified as NP-hard, meaning they are computationally intensive, and no known methods solve them efficiently within a reasonable timeframe.
Traditional algorithms might either fail to find optimal solutions or require excessive computational resources, making heuristic approaches essential. Common optimization paradigms used in these contexts include simulated annealing—a method inspired by the controlled cooling process of metals, which iteratively improves a solution while occasionally accepting less optimal steps to avoid getting trapped in local minima—and branch and bound, an approach systematically examining subsets of possible solutions by branching them into more constrained versions and pruning those unlikely to yield optimal solutions. These methods are effective in producing good, near-optimal solutions in practical, resource-constrained settings.
The optimizn library introduced in this study addresses the lack of comprehensive Python tools for effectively implementing these techniques, particularly for combinatorial and constrained optimization scenarios.
The optimizn library is particularly relevant for data scientists, engineers, and researchers dealing with complex optimization scenarios. Practically, it addresses real-world combinatorial optimization problems efficiently, especially where resources and computational time are limited or intermittent. Industries dealing with large-scale, complex decision-making tasks, such as cloud computing environments, could benefit significantly from the efficiency and customizability of optimizn.
The authors validated optimizn's performance through experiments involving two notable NP-hard problems: the symmetric traveling salesman problem and Azure's "environment design" problem. In both cases, simulated annealing and various branch and bound algorithms were implemented and compared against existing solutions, such as python-tsp.
For the traveling salesman problem, optimizn’s depth-first-best-first branch and bound approach achieved substantial improvements from initial solutions, closely following the python-tsp library's simulated annealing implementation, indicating its competitive performance. For Azure's environment design problem, optimizn's simulated annealing algorithms consistently produced superior solutions compared to other tested methods.
Users can implement optimizn by creating subclasses for specific problems, defining essential methods like get initial solution
, cost
, and problem-specific methods such as next_candidate
, branch
, and lbound
. These implementations allow precise customization according to problem constraints and input types. The library's architecture is clearly structured, offering methods that encapsulate standard optimization logic while allowing users significant flexibility in algorithm customisation.
You can learn more by reading the entire paper or accessing the library on GitHub.
Here’s an excerpt from “Chapter 1: Getting Started with spaCy” in the book, Mastering spaCy by Déborah Mesquita and Duygu Altınok, published in February 2025.
Visualization is the easiest way to explain some concepts to your colleagues, your boss, and any technical or non-technical audience. Visualization of language data is specifically useful and allows you to identify patterns in your data ata glance.
There are many Python libraries and plugins such asmatplotlib
,seaborn
,tensorboard
, and so on. spaCy also comes with its own visualizer – displaCy. In this subsection, you’ll learn how to spin up a displaCy server on your machine, in a Jupyter notebook, and in a web application. We’ll start by exploring the easiest way – using displaCy’sinteractive demo.
Go ahead and navigate tohttps://demos.explosion.ai/displacyto use the interactive demo. Enter your text in theText to parsebox andthen click the search icon on the right to generate the visualization. The result might look likeFigure 1.3.
Figure 1.3 – displaCy’s online demo
The visualizer performs two syntactic parses,POS tagging, and adependency parser. We’ll explore them in the upcoming chapters. For now, just think of the result as a sentence structure. Now, let’s see how to visualize named entitieswith displaCy.
displaCy’s entity visualizer highlights the named entities in your text. The online demo is athttps://demos.explosion.ai/displacy-ent. We haven’t gone through named entities yet, but you can think of them as proper nouns for important entities such as people’s names, company names, dates, city and country names, andso on.
The online demo works similar to the syntactic parser demo. Enter your text into the textbox and hit theSearchbutton.Figure 1.4showsan example.
Figure 1.4 – displaCy’s named entity visualizer
The right side contains checkboxes for entity types. You can check the boxes that match your text type such as, for instance,MONEYandQUANTITYfor a financial text. Just like in the syntactic parser demo, you can choose from theavailable models.
The displaCy visualizers are integrated into the core library. This means that you can start using displaCy immediately after installing spaCy on your machine. Let’s go throughsome examples:
import spacy
from spacy import displacy
nlp = spacy.load('en_core_web_sm')
doc= nlp("One step forward and you're no longer in the same place")
serve()
method to run a server locally, specifying that we want to see the dependencyparser visualization:displacy.serve(doc, style='dep')
Figure 1.5 – Firing up displaCy locally
http://0.0.0.0:5000
is the local address where displaCy renders your visualization. You can open this URL in a browser to see it. When you want to shut down the server, you can pressCtrl+Cto shut down the displaCy server and go back to thecommand line.5000
is already in use, you can use theport
parameter of displaCy with another port number. Replacing the last line of the preceding code block with the following linewill suffice:displacy.serve(doc, style='dep', port=5001)
Here, we provide the port number5001
explicitly. In this case, displaCy will render the graphicsonhttp://0.0.0.0:5001
.
ent
to the style parameter insteadofdep
:import spacy
from spacy import displacy
nlp = spacy.load('en_core_web_sm')
doc= nlp("spaCy's main developers are Matthew Honnibal and Ines Montani, the founders of the software company Explosion")
displacy.serve(doc, style='ent')
Let’s move on to other platforms we can use for displayingthe results.
Jupyter Notebook is an important part of daily data science work. To display the visualizations in Jupyter notebooks, we can change theserve()
method torender()
. The rest of the code is all the same.Figure 1.6shows the result of runningdisplaCy
in aJupyter notebook.
Figure 1.6 – displaCy rendering results in a Jupyter notebook
If you wish to find out how to use different background images, background colors, and fonts, you can visit the displaCy documentationathttp://spacy.io/usage/visualizers.
Mastering spaCy was published in February 2025. Packt library subscribers can continue reading the entire book for free.
And that’s a wrap.
We have an entire range of newsletters with focused content for tech pros. Subscribe to the ones you find the most usefulhere. The complete PythonPro archives can be foundhere.
If you have any suggestions or feedback, or would like us to find you a Python learning resource on a particular subject, just respond to this email!