Skip to content

Commit 99bad19

Browse files
committed
Initial commit
0 parents  commit 99bad19

File tree

9 files changed

+395
-0
lines changed

9 files changed

+395
-0
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FLASK_APP=app
2+
FLASK_ENV=development
3+
4+
# Keep your API Key a secret!
5+
API_KEY=

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
venv/
2+
3+
*.pyc
4+
__pycache__/
5+
6+
instance/
7+
8+
.pytest_cache/
9+
.coverage
10+
htmlcov/
11+
12+
dist/
13+
build/
14+
*.egg-info/
15+
16+
.DS_STORE
17+
18+
.env

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# TheBrain API Quickstart - Python
2+
3+
This is an example project that demonstrates how to make a call to TheBrain's API to create a thought. It uses the [Flask](https://flask.palletsprojects.com/en/3.0.x/) web framework. Follow the instructions below to get set up.
4+
5+
## Setup
6+
7+
1. If you don’t have Python installed, [install it from here](https://www.python.org/downloads/).
8+
9+
2. Clone this repository.
10+
11+
3. Navigate into the project directory:
12+
13+
```bash
14+
$ cd thebrain-api-quickstart-python
15+
```
16+
17+
4. Create a new virtual environment:
18+
19+
Linux/macOS:
20+
```bash
21+
$ python -m venv venv
22+
$ . venv/bin/activate
23+
```
24+
Windows:
25+
```cmd
26+
$ python -m venv venv
27+
$ venv/Scripts/activate
28+
```
29+
30+
5. Install the requirements:
31+
32+
```bash
33+
$ pip install -r requirements.txt
34+
```
35+
36+
6. Make a copy of the example environment variables file:
37+
38+
```bash
39+
$ cp .env.example .env
40+
```
41+
42+
7. Add your [API key](https://app.thebrain.com/apiKeys) to the newly created `.env` file.
43+
44+
8. Run the app:
45+
46+
```bash
47+
$ flask run
48+
```
49+
50+
You should now be able to access the app at [http://localhost:5000](http://localhost:5000).

app.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
import requests
3+
import json
4+
5+
from flask import Flask, render_template, url_for
6+
from flask_socketio import SocketIO
7+
8+
app = Flask(__name__)
9+
socketio = SocketIO(app)
10+
11+
apiKey = os.getenv("API_KEY")
12+
13+
@app.route("/", methods=("GET", "POST"))
14+
def index():
15+
return render_template("index.html")
16+
17+
18+
@socketio.on('submit_form')
19+
def handle_form_submission(data):
20+
brainId = data['brainId']
21+
thtName = data['thtName']
22+
kind = 1
23+
thtLabel = data['thtLabel']
24+
typeId = None
25+
srcThtId = data['srcThtId']
26+
relation = 1
27+
acType = 0
28+
29+
url = f'https://api.bra.in/thoughts/{brainId}'
30+
headers = {
31+
'Authorization': f'Bearer {apiKey}',
32+
'Content-Type': 'application/json'
33+
}
34+
body = {
35+
'name': thtName,
36+
'kind': kind,
37+
'label': thtLabel,
38+
'typeId': typeId,
39+
'sourceThoughtId': srcThtId,
40+
'relation': relation,
41+
'acType': acType
42+
}
43+
44+
response = requests.post(url, headers=headers, json=body)
45+
46+
if response.status_code == 200:
47+
response_data = response.json()
48+
newThoughtId = response_data
49+
socketio.emit('form_response', {'message': f'Success! New Thought ID: {newThoughtId}'})
50+
else:
51+
errorMessage = response.text
52+
socketio.emit('form_response', {'message': f'Error: {response.text}'})

requirements.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
bidict==0.22.1
2+
blinker==1.6.3
3+
certifi==2023.7.22
4+
charset-normalizer==3.3.0
5+
click==8.1.7
6+
colorama==0.4.6
7+
Flask==3.0.0
8+
Flask-SocketIO==5.3.6
9+
h11==0.14.0
10+
idna==3.4
11+
itsdangerous==2.1.2
12+
Jinja2==3.1.2
13+
MarkupSafe==2.1.3
14+
python-dotenv==1.0.0
15+
python-engineio==4.8.0
16+
python-socketio==5.10.0
17+
requests==2.31.0
18+
simple-websocket==1.0.0
19+
urllib3==2.0.7
20+
Werkzeug==3.0.0
21+
wsproto==1.2.0

static/favicon.ico

284 KB
Binary file not shown.

static/global.css

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
body,
2+
input {
3+
font-size: 16px;
4+
line-height: 24px;
5+
color: #ffffff;
6+
background: #1f2937;
7+
font-family:ui-sans-serif, system-ui, Roboto, "Segoe UI";
8+
}
9+
10+
body {
11+
display: flex;
12+
flex-direction: column;
13+
align-items: center;
14+
margin: 0;
15+
}
16+
17+
input {
18+
background: #313a47;
19+
border: 1px solid #5f6a79;
20+
color: #ffffff;
21+
width: 100%;
22+
padding: 0.5rem;
23+
border-radius: 0.25rem;
24+
box-sizing: border-box;
25+
}
26+
27+
ol {
28+
list-style: auto;
29+
padding: 0;
30+
}
31+
32+
code {
33+
padding: 0 0.25rem 0 0.25rem;
34+
background: #313a47;
35+
border-radius:0.25rem;
36+
border: 1px solid #ffffff0f
37+
}
38+
39+
hr {
40+
margin-bottom: 1rem;
41+
border-color: #6b7280;
42+
}
43+
44+
h3 {
45+
text-align: center;
46+
font-size: 2.25rem;
47+
line-height: 2.25rem;
48+
font-weight: 700;
49+
margin-top: 0;
50+
margin-bottom: 1rem;
51+
}
52+
53+
h4 {
54+
text-align: center;
55+
font-size: 1.25rem;
56+
line-height: 1.75rem;
57+
font-weight: 700;
58+
margin-bottom: 1rem;
59+
margin-top: 0;
60+
}
61+
62+
.main-wrapper {
63+
display: flex;
64+
flex-direction: column;
65+
min-height: 100vh;
66+
align-items: center;
67+
justify-content: center;
68+
width: 800px;
69+
}
70+
71+
.description {
72+
margin-bottom: 2rem;
73+
}
74+
75+
.createThoughtCard {
76+
box-sizing: border-box;
77+
display: flex;
78+
flex-direction: column;
79+
background-color: #4b5563;
80+
width: 100%;
81+
max-width: 800px;
82+
border-radius: 0.5rem;
83+
padding: 2rem;
84+
margin-bottom: 3rem;
85+
}
86+
87+
.inputs-wrapper {
88+
display: flex;
89+
flex-direction: row;
90+
justify-content: center;
91+
}
92+
93+
.input-section {
94+
display: flex;
95+
flex-direction: column;
96+
width: 100%;
97+
}
98+
99+
.submit-row {
100+
display: flex;
101+
flex-direction: row;
102+
justify-content: center;
103+
margin-top: 1rem;
104+
}
105+
106+
.submit-button {
107+
font-size: 1rem;
108+
background-color: #3b82f6;
109+
color: #ffffff;
110+
border: none;
111+
border-radius: 0.25rem;
112+
width: 100%;
113+
padding-top: 0.5rem;
114+
padding-bottom: 0.5rem;
115+
transition-property: color, background-color;
116+
transition-duration: 150ms;
117+
transition-timing-function: cubic-bezier(0, 0, 0.2, 1);
118+
cursor: pointer;
119+
}
120+
121+
.submit-button:hover {
122+
background-color: #2563eb;
123+
}
124+
125+
.status-message {
126+
text-align: center;
127+
margin-top: 1rem;
128+
}
129+
130+
.web-client {
131+
width: 100%;
132+
max-width: 800px;
133+
height: 600px;
134+
background-color: #1f2125;
135+
border: 1px solid #4b5563;
136+
border-radius: 0.5rem;
137+
}
138+
139+
.success-message {
140+
margin-top: 1rem;
141+
text-align: center;
142+
color: #22c55e;
143+
}
144+
145+
.error-message {
146+
margin-top: 1rem;
147+
text-align: center;
148+
color: #ef4444;
149+
}

static/requestHelper.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
var socket = io.connect('http://' + document.domain + ':' + location.port);
3+
4+
function submitForm(event) {
5+
event.preventDefault();
6+
7+
var formData = {
8+
brainId: document.getElementsByName('brainId')[0].value,
9+
srcThtId: document.getElementsByName('srcThtId')[0].value,
10+
thtName: document.getElementsByName('thtName')[0].value,
11+
thtLabel: document.getElementsByName('thtLabel')[0].value
12+
};
13+
14+
socket.emit('submit_form', formData);
15+
}
16+
17+
socket.on('form_response', function(response) {
18+
19+
var existingMessageDiv = document.getElementById('message');
20+
if (existingMessageDiv) {
21+
existingMessageDiv.remove();
22+
}
23+
24+
var messageDiv = document.createElement('div');
25+
messageDiv.id = 'message';
26+
messageDiv.textContent = response.message;
27+
28+
if (response.message.startsWith('Success')) {
29+
messageDiv.className = 'success-message';
30+
} else {
31+
messageDiv.className = 'error-message';
32+
}
33+
34+
var formElement = document.querySelector('.createThoughtCard');
35+
formElement.appendChild(messageDiv);
36+
});

0 commit comments

Comments
 (0)