Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building SPAs with Django and HTML Over the Wire

You're reading from   Building SPAs with Django and HTML Over the Wire Learn to build real-time single page applications with Python

Arrow left icon
Product type Paperback
Published in Aug 2022
Publisher Packt
ISBN-13 9781803240190
Length 264 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Andros Fenollosa Andros Fenollosa
Author Profile Icon Andros Fenollosa
Andros Fenollosa
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Part 1: Getting Started with Python
2. Chapter 1: Setting up the Virtual Environment FREE CHAPTER 3. Chapter 2: Creating a Django Project around Docker 4. Part 2: WebSockets in Django
5. Chapter 3: Adding WebSockets to Django 6. Chapter 4: Working with the Database 7. Chapter 5: Separating Communication in Rooms 8. Part 3: HTML over WebSockets
9. Chapter 6: Creating SPAs on the Backends 10. Chapter 7: Creating a Real-Time Blog Using Only Django 11. Part 4: Simplifying the frontend with Stimulus
12. Chapter 8: Simplifying the Frontend 13. Other Books You May Enjoy

Configuring the IDE

PyCharm is highly popular because it’s a tool specially prepared to work with Python and it also includes interesting integrations with databases, Git, HTTP clients, environments, and the like. One of the most used is certainly the one related to Docker, so I will use this fantastic IDE in future examples. However, as I said before, it is not mandatory to use it; there are enough alternatives to please everyone. All code and activities shown in this chapter will work independently of the editor.

To set up the IDE, follow these steps:

  1. Open the folder where you want to work using PyCharm (File | Open). A directory tree will be displayed on the left.
  2. Click on the Python file (hello.py). It’s not possible to run the Python code if you don’t use the terminal; PyCharm doesn’t know where the Python interpreter, or executable, is otherwise. It’s inside a Docker image that the operating system can’t access, for now.
Figure 1.1 – Open the Python file

Figure 1.1 – Open the Python file

  1. PyCharm may open a popup that suggests creating a virtual environment. You can skip this step or close the window; we will use Docker for the job. If you don’t see the window, you can continue without fear.
  2. We then check whether we have the Docker plugin installed. Go to File | Settings | Plugins and look for Docker.
  3. If it is installed, it will appear in the Installed tab. If not, you will have to look for it in Marketplace and then click on the Install button. Then, restart PyCharm. Ensure you do this. Otherwise, we won’t be able to continue.
Figure 1.2 – Installing the Docker plugin

Figure 1.2 – Installing the Docker plugin

  1. Now open File | Settings | Build, Execution, Deployment | Docker and press the + button. Then, search for Docker.
Figure 1.3 – Connecting with Docker

Figure 1.3 – Connecting with Docker

  1. Enter Docker in the Name field, for example, and activate Unix socket. At the bottom, you will see the Connection successful message.
  2. We only need to tell PyCharm not to look for the Python interpreter, or executable, on the machine (if there is one) and to use the Docker service we have created instead.
  3. Go to File | Settings | Project: Executable Python. Here, we deploy the Python Interpreter, select Remote Python xxx Docker Compose, and click on Apply. The interpreter name may change depending on the folder name.
Figure 1.4 – Adding the Python interpreter

Figure 1.4 – Adding the Python interpreter

  1. It will automatically detect the dependencies that are already installed on the machine but we will ignore them. By the way, this is a good place to manage all Python dependencies visually.
  2. Now, it’s time to run the Python code using the configuration you just made.
  3. Close Settings and focus on the directory tree (on the left side). Right-click on hello.py and then Run ‘hello’.
Figure 1.5 – Running Python with PyCharm

Figure 1.5 – Running Python with PyCharm

  1. At the bottom of the editor, an area with the log of the execution will open. As proof that it has been done successfully, we can see the print statement.
Figure 1.6 – Viewing the Python execution log through Docker integration

Figure 1.6 – Viewing the Python execution log through Docker integration

  1. Also, if we open the docker-compose.yaml file, we can run the containers individually.
Figure 1.7 – Launching containers through Docker integration

Figure 1.7 – Launching containers through Docker integration

  1. On line 5 in Figure 1.7, you can see a green arrow; when you click on it, it will launch the service and, again, the Python code.

PyCharm is already integrated with Docker and is able to launch Python with its dependencies in isolation from the operating system. We are ready to work directly with Django. We are going to create a simple project using the official Django client to have a minimal structure when developing.

You have been reading a chapter from
Building SPAs with Django and HTML Over the Wire
Published in: Aug 2022
Publisher: Packt
ISBN-13: 9781803240190
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime