Virtual Displays in Python For Server Deployment

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in this video today we're going to learn how to use a virtual display in Python to be able to simulate a display without actually providing one for example in server environments so let us get right into [Music] it all right so we're going to learn how to use Virtual displays in Python and the idea is quite simple and straightforward let's say we have a python application that requires a display to be present in order to be able to run its code properly if you run this code without a display being present it's going to crash it's going to give you an error message could not connect to a display could not connect to the X server something like this and whether you actually need to use the display or not is not really of importance here you need a display to be present otherwise it's going to crash uh one example for this could be a selenium web scrape application where you don't have the Headless option enabled if you try to run this on a server without a display it will tell you that it has to stop the script because it cannot connect to a display server and it needs one to open up the browser window to interact with the website to maybe take screenshots something like this and in the case of selenium what you can do is you can easily just enable or add the Headless option which allows you to run it without a graphical user interface in a server environment for example I have a video about this on my channel as well but not in all cases not for all packages not for all applications will you be able to just add a simple headless option so one way that you can deal with this is you can add a so-called or you can use a so-called virtual display and this is what we're going to take a look at in this video today and the first thing I want to do is I want to build a very very simple application that requires a display to be present otherwise it crashes and for this what we're going to do is we're going to open up the terminal and we're going to install first of all the package using pip uh which is called PDF kit and this PDF kit requires a package on your system called um and I'm going to install this here with my package manager pseudo app install uh WK HTML L2 PDF this is what it runs on this is uh what what the package is based on in my case all of this is already installed so you need this python package and this package on your system in general uh of course you can use any other package that requires a display as well this is just the example that I use in this video today this is not about the virtual display yet this is just one application that requires a virtual display um and what we're going to do now is I'm going to create a simple HTML file let's call this file. HTML and I'm going to just add a simple heading with Hello World in here and what I want to do now with PDF kit is I'm going to say import PDF kit as PDF and then I'm going to say pdf. from file file. HTML file.pdf like this uh very simple very straightforward nothing too fancy about this of course I can run it just like this and you will see it works um actually I'm not going to open up the PDF file but trust me it works uh we have this conversion here but I also have this display here being active so what I'm going to do now is I'm going to open up my terminal and I'm going to navigate to this directory and in here what I'm going to do is I'm going to unset the display on my uh terminal instance here basically this means I'm disabling uh the the display so I can run it first of all one more time with a display you can see it works and now I can say unset display and I can run it again and you will see it crashes cannot open display this is exactly what would happen if I run the script on a server so I might have a web application running on a server and on This Server I might have a button in the application that uh should convert something to a PDF it will fail because it cannot open a display on the server um a very simple workaround here is to just use a so-called virtual display and what you need to do for that is you need to first of all install something uh which is called which is a package here again pseudo app install on debm based distributions uh xvfb and I don't know actually what the v stands for I think uh the rest stands for uh X just being the display server and FB for frame buffer or something like that but this is what you need running in the background for the package that we're now going to install and the package we're going to install is called uh Pi virtual display Pi virtual display and once we have this package installed it's super easy to use all we have to do is we need to say from PI virtual display import display and then we can either create an instance and start and stop it or we can just use a context manager we can say with display as display and then basically we're done so that is exactly what we need for this and now I can go back here I still have my display not enabled but it will still work because it will connect to that display so it basically um allows us to have a display even though we don't have an actual monitor dis so this also works on a server um and you can see it works easily and I can also remove the PDF file here and I can run this again and you will see it will be executed and I have the PDF file here again so this is how you can use Virtual displays in Python so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not a single future video for free other than that thank you much for watching see you in the next video and bye
Info
Channel: NeuralNine
Views: 8,331
Rating: undefined out of 5
Keywords: python, pyvirtualdisplay, virtual display, virtual displays, display server, python display server, python virtual display
Id: SR5aBhk1GgY
Channel Id: undefined
Length: 5min 59sec (359 seconds)
Published: Tue Dec 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.