repl.it
@AtlasTK/

Hello compact (Python)

Python

"Hello, World!" with a GUI, made in Python with the Atlas toolkit. Compact form (all in one file).

fork
loading
Files
  • main.py
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# coding: utf-8
"""
To launch the application:
- click on the green button,
- open, in a web browser, the URL displayed
  in the black console (above the '^^…^^' line).

You can also:
- click the 'fork' button, left to the green button,
- uncomment below '#REPLit = True' line (remove
  the leading '#' character),
- click on the green button,
- click on the then displayed QR code.

For more details: http://q37.info/s/zbgfjtp9

Copyright (c) 2019 Claude SIMON (http://q37.info/s/rmnmqd49)
MIT License
"""

#REPLit = True

import atlastk as Atlas
import os

try:
    if REPLit:
        os.environ["ATK"]="REPLit"
except:
    pass

# Will be put in the 'head' section of the HTML page. You can put here CSS definitions.
# Here, contains the title of the web page and the favicon.
head = """
<title>"Hello, World !" example</title>
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAAMFBMVEUEAvyEhsxERuS8urQsKuycnsRkYtzc2qwUFvRUVtysrrx0ctTs6qTMyrSUksQ0NuyciPBdAAABHklEQVR42mNgwAa8zlxjDd2A4POfOXPmzZkFCAH2M8fNzyALzDlzg2ENssCbMwkMOsgCa858YOjBKxBzRoHhD7LAHiBH5swCT9HQ6A9ggZ4zp7YCrV0DdM6pBpAAG5Blc2aBDZA68wCsZPuZU0BDH07xvHOmAGKKvgMP2NA/Zw7ADIYJXGDgLQeBBSCBFu0aoAPYQUadMQAJAE29zwAVWMCWpgB08ZnDQGsbGhpsgCqBQHNfzRkDEIPlzFmo0T5nzoMovjPHoAK8Zw5BnA5yDosDSAVYQOYMKIDZzkoDzagAsjhqzjRAfXTmzAQgi/vMQZA6pjtAvhEk0E+ATWRRm6YBZuScCUCNN5szH1D4TGdOoSrggtiNAH3vBBjwAQCglIrSZkf1MQAAAABJRU5ErkJggg==" />
"""

# Initial page of the interface. 'data-xdh-onevent' associates actions to HTML elements.
body = """
<div style="display: table; margin: 50px auto auto auto;">
 <fieldset>
  <input id="input" maxlength="20" placeholder="Enter a name here" type="text"
         data-xdh-onevent="Submit" value="World"/>
  <div style="display: flex; justify-content: space-around; margin: 5px auto auto auto;">
   <button data-xdh-onevent="Submit">Submit</button>
   <button data-xdh-onevent="Clear">Clear</button>
  </div>
 </fieldset>
</div>
"""

# Called on a new session.
def acConnect(dom, id):
	dom.setLayout("", body )  # Sets the initial page.
	dom.focus( "input") # Gives the focus to the texbox.

# Called when clicking on the submission button or when hitting 'Enter' in the textbox.
def acSubmit(dom, id):
	dom.alert("Hello, " + dom.getContent("input") + "!")  # Retrieves and displays the content of the textbox.
	dom.focus( "input") # Gives the focus to the texbox.

# Called when clicking on the 'Clear' button.
def acClear(dom, id):
	if ( dom.confirm("Are you sure?" ) ): # Ask for confirmation.
		dom.setContent("input", "" )  # Clears the textbox.
	dom.focus( "input") # Gives the focus to the texbox.

callbacks = {
	"": acConnect,  # Callback for a new sessions.
	"Submit": acSubmit, # Callback for the 'Submit' action (label defined in a 'data-xdh-onevent' attribute)
	"Clear": acClear, # Callback to call for the 'Clear' action (label defined in a 'data-xdh-onevent' attribute)
}

# UI loop, with the list of callbacks for each action, the callback to create an  object for each new session (no object needed here), and the content of the 'head' section of the HTML page.
Atlas.launch(callbacks, None, head)