The Alliance Code Space provides you with a complete growth environment directly in your browser. This code is great to write and run, but it has a large limit: it does not support graphical applications out of the box, especially for the Code.
If you try to run a Pigm, Technician, or PEQT -like GUI library inside the code space, you will have a mistake. The reason for this is that the code operates in the environment without a tone. Your app has no physical display to open the window.
In this article, I show you how to fix it. You will learn how to set a virtual desktop using XVFB and stream it in your browser using NONC. Finally, you will be able to operate any GUI application inside the Alliance Code Space.
The table of content
Provisions
Before starting, you should be:
A Got Hub Account and Got Hub Code access access.
Basic familiarity with Azgar.
A GUI app to test (we’ll use a small pagamist example).
When you run the GUI code like:
import pygame
pygame.display.set_mode((800, 600))
On your local machine, Ijar asks to make your operating system a window. But the code operates on the space server with no monitor. Without a display, your GUI app may not render render.
There Xvfb – In the browser to make this screen visible, you can use NunicWhich streams virtual display through a web client.
Together, the code space for XVFB and Nonc GUI apps converts to cloud -based desktop.
Step 1: Create repo and open code space
First, make a gut hub store for your project (or demo repo) and open it in code space:

Step 2: Add Setup Scrupate
Create a file that says start-gui.sh At the root of your project.

Paste the code below start-gui.sh File:
#!/usr/bin/env bash
set -e
echo "Installing dependencies..."
sudo apt-get update -y
sudo apt-get install -y xvfb x11vnc fluxbox websockify novnc
echo "Starting virtual display..."
Xvfb :1 -screen 0 1024x768x24 &
export DISPLAY=:1
fluxbox &
echo "Starting VNC server..."
x11vnc -display :1 -nopw -forever -shared -rfbport 5900 &
echo "Starting noVNC on port 6080..."
websockify --web=/usr/share/novnc 6080 localhost:5900 &
echo ""
echo "GUI environment is ready!"
echo "Go to the Ports tab, set port 6080 to Public, and open the link."
Let’s explain this script so you can understand what it does:
set -e
It tells the shell that if a command fails, get out immediately.
Without it, the script continues even if something goes wrong (like a failed install).
Install dependent
sudo apt-get update -y: Updates your package list.
sudo apt-get install -y We installed the packages we entered (XVFB, X11Vnc, Fluxbox, WebSockify, and Nonnc)
Virtual Display
Xvfb :1 -screen 0 1024x768x24 &: Virtual Frame Buffer in Display begins:1With the resolution1024x768And 24 -bit colors.export DISPLAY=:1: Apps (like Azigar Goyce) ask to draw on this virtual screen rather than search for a real display unit.fluxbox &: The window manager launches so GUI apps have a desktop to sit.
VNC server
x11vnc -display :1: You connect to a dummy display (:1,-nopw: It ensures that a password is not needed.-forever: It keeps running VNC even if the client is disconnected.-shared: Allows many clients.-rfbport 5900: Exposes the interior server at the standard port of VNC.
Nonnc server
websockifyWorks as a bridge that converts web socket traffic into VNC protocol (Port 5900).--web=/usr/share/novnc: NONC uses web client files.6080: The port where you will contact in your browser (it is publicly accessible).localhost:5900: Traffic forwardes the VNC server that was previously started.
You should only be exposed to Port 6080 (NONC) as AS Public And keep 5900 (RAWNC) Private because:
PRT 5900 (VNC) Uses the raw VNC protocol, which is Not encrypted And this setup does not require a password. If exposed, anyone can connect directly and control your code spacetop.
PRT 6080 (NONC) Ends up Web sockets + httpsSo the traffic is encrypted and the gut hub is stored through the codes space connection. It also serves the NonC Web Client, not the raw VNC protocol.
5900 = unsafe to exposeFor, for, for,. 6080 = GUI viewing way of viewing from the browser.
The next step is for you to make the Bash File Following the Code below you in the terminal:
chmod +x start-gui.sh
Step 3: Start the GUI environment
Run the script:
./start-gui.sh

This will:
Install all dependents (XVFB, Fluxbox, X11Vnc, Nonnc).
Start Virtual Display (
DISPLAY=:1,Launch a lightweight window manager (Flux box).
Stream to desktop on your browser via nonic on the port
6080.
Step 4: Open Nonc Desktop
In code space, open the port tab.
Find Port 6080 and convert it into public. (Right click on the private word)
Open the URL in a new browser tab.
Click
vnc.htmlOrvnc_auto.htmlIf indicated.

Now you should see a lightweight Linux desktop inside your browser.

Step 5: Run your GUI app
In a new code space terminal, run:
export DISPLAY=:1
python3 your_script.py
Your GUI app should appear within the Nonc Desktop 🎉.
For example, here is a simple pegium script test.py:
import pygame
from pygame import display, font, event
from pygame.locals import *
pygame.init()
screen = display.set_mode()
display.set_caption("Capstone 2")
myFont = font.SysFont('arial', 12)
directions = "Please press the 'Y' key for yes and the 'N' key for no."
currentQuestion = 0
def story(answer, count):
screen.fill("white")
if count == 0:
question1(answer)
elif count == 1:
question2(answer)
elif count == 2:
question3(answer)
elif count == 3:
end(answer)
def intro():
intro1 = "Once upon a time lived a brave hero named Anya."
intro2 = "She lived a simple life in a small village, making biscuits for the village people."
intro3 = "One day, late at night, she hears a loud noise outside the village."
q1 = "Should she go outside to investigate? Yes or no?"
screen.fill("white")
textSurface = myFont.render(intro1, True, "black")
screen.blit(textSurface, (10, 10))
textSurface = myFont.render(intro2, True, "black")
screen.blit(textSurface, (10, 24))
textSurface = myFont.render(intro3, True, "black")
screen.blit(textSurface, (10, 38))
textSurface = myFont.render(q1, True, "black")
screen.blit(textSurface, (10, 52))
textSurface = myFont.render(directions, True, "black")
screen.blit(textSurface, (10, 66))
def question1(answer):
if answer == K_y:
yes1 = "She ventures into the dark, prepared for danger."
yes2 = "Eventually, she sees an army of ogres coming toward her village!"
q2 = "Should she fight the ogres? Yes or no?"
textSurface = myFont.render(yes1, True, "black")
screen.blit(textSurface, (10, 10))
textSurface = myFont.render(yes2, True, "black")
screen.blit(textSurface, (10, 24))
textSurface = myFont.render(q2, True, "black")
screen.blit(textSurface, (10, 38))
textSurface = myFont.render(directions, True, "black")
screen.blit(textSurface, (10, 52))
elif answer == K_n:
no1 = "She chooses the safety of her home and stays inside."
no2 = "However, the sounds do not go away."
no3 = "She can tell something is very wrong..."
no4 = "Eventually, she sees an army of ogres coming toward her village!"
q2 = "Should she fight the ogres? Yes or no?"
textSurface = myFont.render(no1, True, "black")
screen.blit(textSurface, (10, 10))
textSurface = myFont.render(no2, True, "black")
screen.blit(textSurface, (10, 24))
textSurface = myFont.render(no3, True, "black")
screen.blit(textSurface, (10, 38))
textSurface = myFont.render(no4, True, "black")
screen.blit(textSurface, (10, 52))
textSurface = myFont.render(q2, True, "black")
screen.blit(textSurface, (10, 66))
textSurface = myFont.render(directions, True, "black")
screen.blit(textSurface, (10, 80))
def question2(answer):
if answer == K_y:
yes1 = "She bravely confronts the ogres, hoping to protect her village from harm."
textSurface = myFont.render(yes1, True, "black")
screen.blit(textSurface, (10, 10))
elif answer == K_n:
no1 = "The ogres raid the village but Anya manages to escape with her life."
textSurface = myFont.render(no1, True, "black")
screen.blit(textSurface, (10, 10))
story2 = "The ogres decide to leave but she knows they will be back."
story3 = "Anya decides to talk with a village elder about what she should do."
story4 = "The elder says there is a powerful sword hidden in the Ancient Forest."
q3 = "Should Anya risk her life to retrieve it? Yes or no?"
textSurface = myFont.render(story2, True, "black")
screen.blit(textSurface, (10, 24))
textSurface = myFont.render(story3, True, "black")
screen.blit(textSurface, (10, 38))
textSurface = myFont.render(story4, True, "black")
screen.blit(textSurface, (10, 52))
textSurface = myFont.render(q3, True, "black")
screen.blit(textSurface, (10, 66))
textSurface = myFont.render(directions, True, "black")
screen.blit(textSurface, (10, 80))
def question3(answer):
if answer == K_y:
yes1 = "Although Anya almost died in the Ancient Forest,"
yes2 = "she returns with the Sword of Legends!"
yes3 = "In the dead of winter, the ogres come back."
yes4 = "This time they are being led by their evil king."
q4 = "Should Anya fight the ogre king now that she has the Sword of Legends?"
textSurface = myFont.render(yes1, True, "black")
screen.blit(textSurface, (10, 10))
textSurface = myFont.render(yes2, True, "black")
screen.blit(textSurface, (10, 24))
textSurface = myFont.render(yes3, True, "black")
screen.blit(textSurface, (10, 38))
textSurface = myFont.render(yes4, True, "black")
screen.blit(textSurface, (10, 52))
textSurface = myFont.render(q4, True, "black")
screen.blit(textSurface, (10, 66))
textSurface = myFont.render(directions, True, "black")
screen.blit(textSurface, (10, 80))
elif answer == K_n:
no1 = "Anya decides it's too risky to go into the forest alone."
no2 = "She hopes for the best with the weapons she has."
no3 = "In the dead of winter, the ogres come back."
no4 = "This time they are being led by their evil king."
q4 = "Should Anya fight the king even though she doesn't have the Sword of Legends?"
textSurface = myFont.render(no1, True, "black")
screen.blit(textSurface, (10, 10))
textSurface = myFont.render(no2, True, "black")
screen.blit(textSurface, (10, 24))
textSurface = myFont.render(no3, True, "black")
screen.blit(textSurface, (10, 38))
textSurface = myFont.render(no4, True, "black")
screen.blit(textSurface, (10, 52))
textSurface = myFont.render(q4, True, "black")
screen.blit(textSurface, (10, 66))
textSurface = myFont.render(directions, True, "black")
screen.blit(textSurface, (10, 80))
def end(answer):
if answer == K_y:
yes1 = "Tension fills the air as she prepares to fight the king. The duel commences..."
end1 = "After an intense battle, Anya strikes the final blow!"
end2 = "The king surrenders and pleads for mercy."
end3 = "Anya is a true hero, who shows mercy to the king."
end4 = "This act of kindness warms the evil king's heart,"
end5 = "who promises to leave the village alone for eternity."
end6 = "The end!"
textSurface = myFont.render(yes1, True, "black")
screen.blit(textSurface, (10, 10))
textSurface = myFont.render(end1, True, "black")
screen.blit(textSurface, (10, 24))
textSurface = myFont.render(end2, True, "black")
screen.blit(textSurface, (10, 38))
textSurface = myFont.render(end3, True, "black")
screen.blit(textSurface, (10, 52))
textSurface = myFont.render(end4, True, "black")
screen.blit(textSurface, (10, 66))
textSurface = myFont.render(end5, True, "black")
screen.blit(textSurface, (10, 80))
textSurface = myFont.render(end6, True, "black")
screen.blit(textSurface, (10, 94))
elif answer == K_n:
no1 = "Anya refuses to duel the king, who laughs at her cowardice."
end1 = "This buys some time for the villagers to escape."
end2 = "Sadly, the ogre king takes over Anya's village."
end3 = "She is just thankful that the villagers were able to get to safety."
end4 = "The end!"
textSurface = myFont.render(no1, True, "black")
screen.blit(textSurface, (10, 10))
textSurface = myFont.render(end1, True, "black")
screen.blit(textSurface, (10, 24))
textSurface = myFont.render(end2, True, "black")
screen.blit(textSurface, (10, 38))
textSurface = myFont.render(end3, True, "black")
screen.blit(textSurface, (10, 52))
textSurface = myFont.render(end4, True, "black")
screen.blit(textSurface, (10, 66))
while True:
if currentQuestion == 0:
intro()
currentEvent = event.poll()
if currentEvent.type == KEYDOWN:
story(currentEvent.key, currentQuestion)
currentQuestion = currentQuestion + 1
display.update()
Code Source: Kodakombut (developing Game)
The aforementioned code is a simple interactive story game that is written with PIMM. In the first few lines, you import Pigm And its display, font, and event modules.
pygame.locals Brings permanently K_y (y key), K_n (n key), and KEYDOWN.
Then the previous line starts the pigium and makes the window (screen) It also sets the title of the window and then loads a font to present the text.
Then you have a section for functions that give strength to the story (introFor, for, for,. question1For, for, for,. question2For, for, for,. question3And the player’s response terms).
To summarize, the code is one of your own residential text games with the same character, Anya. The player’s choice determines which text is shown.
Install, to drive this script pygame.
sudo apt-get update
sudo apt-get install -y python3-pygame
pip install pygame
The aforementioned code will be installed pygame In your environment, after which you can run the script.
python3 test.py
When you run it inside the code space, the window will appear in the Nonic tab. If it does not open automatically, click connect.

Indicator

Ignore ALSA’s mistakes: There is no sound output in code space, so audio warnings are normal.
Adjust the resolutionChange
1024x768x24In the script if you want a big (or small) screen.Use with other libraries: tkinter, pyqt, and matplotlib interactive plot. Everyone will work with this setup.
Automatic Display Export: Add
export DISPLAY=:1If you don’t want to type it every time, your brush file.
Conclusion
You have just turned the bothered code space into the GUI environment. Using XVFB and NONC, you can run apps that usually require a desktop environment inside your browser.
Whether you’re making a game, checking the interface, or teaching graphics, you can now leave all this cloud and do in code space.
Want to try it yourself? Clone it RepoDrive,
./start-gui.shAnd launch your first GUI app in Code Space today.
