Skip to content

Commit

Permalink
Updated examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
eunoia-cl committed Sep 6, 2021
1 parent 1235796 commit a2dd594
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 33 deletions.
6 changes: 3 additions & 3 deletions docs/ch12-networking/rest-api.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# REST API

To use a web-service, we first need to create one. We will use Flask ([http://flask.pocoo.org](http://flask.pocoo.org)) a simple HTTP app server based on python to create a simple color web-service. You could also use every other web server which accepts and returns JSON data. The idea is to have a list of named colors, which can be managed via the web-service. Managed in this case means CRUD (create-read-update-delete).
To use a web-service, we first need to create one. We will use Flask ([https://flask.palletsprojects.com](https://flask.palletsprojects.com)) a simple HTTP app server based on python to create a simple color web-service. You could also use every other web server which accepts and returns JSON data. The idea is to have a list of named colors, which can be managed via the web-service. Managed in this case means CRUD (create-read-update-delete).

A simple web-service in Flask can be written in one file. We start with an empty `server.py` file. Inside this file, we create some boiler-code and load our initial colors from an external JSON file. See also the Flask [quickstart](http://flask.pocoo.org/docs/quickstart/) documentation.
A simple web-service in Flask can be written in one file. We start with an empty `server.py` file. Inside this file, we create some boiler-code and load our initial colors from an external JSON file. See also the Flask [quickstart](https://flask.palletsprojects.com/en/2.0.x/quickstart/) documentation.

```py
from flask import Flask, jsonify, request
Expand Down Expand Up @@ -38,7 +38,7 @@ This will return the colors under the ‘/colors’ endpoint. To test this we ca
curl -i -GET http://localhost:5000/colors
```

Which will return us the list of colors as JSON data?
Which will return us the list of colors as JSON data.

## Read Entry

Expand Down
2 changes: 1 addition & 1 deletion docs/ch12-networking/src/http_v1/Button.qml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// M1>>
// Button.qml

import QtQuick 2.5
import QtQuick

Rectangle {
width: 90
Expand Down
2 changes: 1 addition & 1 deletion docs/ch12-networking/src/http_v1/main.qml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// M1>>
// main.qml
import QtQuick 2.5
import QtQuick

Rectangle {
width: 320
Expand Down
4 changes: 2 additions & 2 deletions docs/ch12-networking/src/http_v1/main2.qml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// M1>>
// main2.qml
import QtQuick 2.5
import "http://localhost:8080" 1.0 as Remote
import QtQuick
import "http://localhost:8080" as Remote

Rectangle {
width: 320
Expand Down
2 changes: 1 addition & 1 deletion docs/ch12-networking/src/http_v1/remote.qml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// M1>>
// remote.qml
import QtQuick 2.5
import QtQuick

Loader {
id: root
Expand Down
2 changes: 1 addition & 1 deletion docs/ch12-networking/src/http_v1/remote2.qml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// M1>>
// remote.qml
import QtQuick 2.5
import QtQuick

Loader {
id: root
Expand Down
2 changes: 1 addition & 1 deletion docs/ch12-networking/src/rest/Button.qml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import QtQuick 2.5
import QtQuick

MouseArea {
width: 140
Expand Down
2 changes: 1 addition & 1 deletion docs/ch12-networking/src/rest/StatusLabel.qml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import QtQuick 2.5
import QtQuick

Text {
id: root
Expand Down
2 changes: 1 addition & 1 deletion docs/ch12-networking/src/rest/rest.qml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import QtQuick 2.5
import QtQuick
import "colorservice.js" as Service

Rectangle {
Expand Down
3 changes: 2 additions & 1 deletion docs/ch12-networking/src/restservice/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from flask import Flask, jsonify, request
import json

colors = json.load(file('colors.json', 'r'))
with open('colors.json', 'r') as file:
colors = json.load(file)

app = Flask(__name__)

Expand Down
2 changes: 1 addition & 1 deletion docs/ch12-networking/src/ws/ws_client/ChatInput.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ChatInput.qml
import QtQuick 2.5
import QtQuick

FocusScope {
id: root
Expand Down
2 changes: 1 addition & 1 deletion docs/ch12-networking/src/ws/ws_client/ChatView.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ChatView.qml
import QtQuick 2.5
import QtQuick

ListView {
id: root
Expand Down
2 changes: 1 addition & 1 deletion docs/ch12-networking/src/ws/ws_client/Label.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Label.qml
import QtQuick 2.5
import QtQuick

Text {
color: '#fff'
Expand Down
8 changes: 4 additions & 4 deletions docs/ch12-networking/src/ws/ws_client/ws_client.qml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ws_client.qml
import QtQuick 2.5
import Qt.WebSockets 1.0
import QtQuick
import QtWebSockets

Rectangle {
width: 360
Expand All @@ -20,7 +20,7 @@ Rectangle {
anchors.right: parent.right
anchors.bottom: parent.bottom
focus: true
onAccepted: {
onAccepted: function(text) {
print('send message: ' + text)
socket.sendTextMessage(text)
box.append('>', text)
Expand All @@ -32,7 +32,7 @@ Rectangle {

url: "ws://localhost:3000"
active: true
onTextMessageReceived: {
onTextMessageReceived: function (message) {
box.append('<', message)
}
onStatusChanged: {
Expand Down
6 changes: 3 additions & 3 deletions docs/ch12-networking/src/wsecho/wsecho.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import QtQuick 2.5
import Qt.WebSockets 1.0
import QtQuick
import QtWebSockets

Text {
width: 480
Expand All @@ -12,7 +12,7 @@ Text {
id: socket
url: "ws://echo.websocket.org"
active: true
onTextMessageReceived: {
onTextMessageReceived: function (message) {
text = message
}
onStatusChanged: {
Expand Down
20 changes: 10 additions & 10 deletions docs/ch12-networking/web-sockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ WebSocket {
To test your web socket we will use the echo server from [http://websocket.org](http://websocket.org).

```qml
import QtQuick 2.5
import Qt.WebSockets 1.0
import QtQuick
import QtWebSockets
Text {
width: 480
Expand All @@ -31,7 +31,7 @@ Text {
id: socket
url: "ws://echo.websocket.org"
active: true
onTextMessageReceived: {
onTextMessageReceived: function (message) {
text = message
}
onStatusChanged: {
Expand Down Expand Up @@ -100,7 +100,7 @@ We will use a label with white color in the example.

```qml
// Label.qml
import QtQuick 2.5
import QtQuick
Text {
color: '#fff'
Expand All @@ -113,7 +113,7 @@ Our chat view is a list view, where the text is appended to a list model. Each e

```qml
// ChatView.qml
import QtQuick 2.5
import QtQuick
ListView {
id: root
Expand Down Expand Up @@ -148,7 +148,7 @@ The chat input is just a simple text input wrapped with a colored border.

```qml
// ChatInput.qml
import QtQuick 2.5
import QtQuick
FocusScope {
id: root
Expand Down Expand Up @@ -183,8 +183,8 @@ When the web socket receives a message it appends the message to the chat view.

```qml
// ws_client.qml
import QtQuick 2.5
import Qt.WebSockets 1.0
import QtQuick
import QtWebSockets
Rectangle {
width: 360
Expand All @@ -204,7 +204,7 @@ Rectangle {
anchors.right: parent.right
anchors.bottom: parent.bottom
focus: true
onAccepted: {
onAccepted: function (text) {
print('send message: ' + text)
socket.sendTextMessage(text)
box.append('>', text)
Expand All @@ -216,7 +216,7 @@ Rectangle {
url: "ws://localhost:3000"
active: true
onTextMessageReceived: {
onTextMessageReceived: function (message) {
box.append('<', message)
}
onStatusChanged: {
Expand Down

0 comments on commit a2dd594

Please sign in to comment.