Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
berlin-mapping-application
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Noah Jefferson Baumann
berlin-mapping-application
Commits
1460bbd5
Commit
1460bbd5
authored
5 months ago
by
Noah Jefferson Baumann
Browse files
Options
Downloads
Patches
Plain Diff
combining node and edges api into graph one
parent
d289b741
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend/app.py
+13
-37
13 additions, 37 deletions
backend/app.py
with
13 additions
and
37 deletions
backend/app.py
+
13
−
37
View file @
1460bbd5
from
flask
import
Flask
,
jsonify
,
request
from
flask
import
Flask
,
jsonify
,
request
from
sqlalchemy.orm
import
sessionmaker
from
sqlalchemy.orm
import
sessionmaker
from
sqlalchemy
import
create_engine
from
sqlalchemy
import
create_engine
from
backend.
models
import
Node
,
Edge
from
models
import
Node
,
Edge
from
dotenv
import
load_dotenv
from
dotenv
import
load_dotenv
import
os
import
os
from
flask_cors
import
CORS
from
flask_cors
import
CORS
...
@@ -23,46 +23,24 @@ if not DATABASE_URL:
...
@@ -23,46 +23,24 @@ if not DATABASE_URL:
engine
=
create_engine
(
DATABASE_URL
)
engine
=
create_engine
(
DATABASE_URL
)
Session
=
sessionmaker
(
bind
=
engine
)
Session
=
sessionmaker
(
bind
=
engine
)
@app.route
(
'
/
nodes
'
,
methods
=
[
'
GET
'
])
@app.route
(
'
/
graph
'
,
methods
=
[
'
GET
'
])
def
get_
nodes
():
def
get_
graph
():
try
:
try
:
year
=
request
.
args
.
get
(
'
year
'
)
year
=
request
.
args
.
get
(
'
year
'
)
with
Session
()
as
session
:
query
=
session
.
query
(
Node
)
if
year
:
query
=
query
.
filter
(
Node
.
year
==
year
)
nodes
=
query
.
all
()
node_list
=
[
node
.
to_dict
()
for
node
in
nodes
]
return
jsonify
(
node_list
)
except
Exception
as
e
:
return
jsonify
({
'
error
'
:
str
(
e
)}),
500
@app.route
(
'
/edges
'
,
methods
=
[
'
GET
'
])
def
get_edges
():
try
:
year
=
request
.
args
.
get
(
'
year
'
)
if
year
:
if
year
:
try
:
year
=
int
(
year
)
year
=
int
(
year
)
# Convert year to integer
except
ValueError
:
return
jsonify
({
'
error
'
:
'
Invalid year format
'
}),
400
with
Session
()
as
session
:
with
Session
()
as
session
:
query
=
session
.
query
(
Edge
)
# Fetch nodes
nodes
=
session
.
query
(
Node
).
filter
(
Node
.
year
==
year
).
all
()
if
year
:
node_list
=
[
node
.
to_dict
()
for
node
in
nodes
]
query
=
query
.
filter
(
Edge
.
year
==
year
)
edges
=
query
.
all
()
# Fetch edges
edges
=
session
.
query
(
Edge
).
filter
(
Edge
.
year
==
year
).
all
()
edge_list
=
[]
edge_list
=
[]
for
edge
in
edges
:
for
edge
in
edges
:
# Fetch source and target nodes for each edge
source_node
=
session
.
query
(
Node
).
filter
(
Node
.
id
==
edge
.
source
).
one_or_none
()
source_node
=
session
.
query
(
Node
).
filter
(
Node
.
id
==
edge
.
source
).
one_or_none
()
target_node
=
session
.
query
(
Node
).
filter
(
Node
.
id
==
edge
.
target
).
one_or_none
()
target_node
=
session
.
query
(
Node
).
filter
(
Node
.
id
==
edge
.
target
).
one_or_none
()
if
source_node
and
target_node
:
if
source_node
and
target_node
:
edge_list
.
append
({
edge_list
.
append
({
"
id
"
:
edge
.
id
,
"
id
"
:
edge
.
id
,
...
@@ -81,14 +59,12 @@ def get_edges():
...
@@ -81,14 +59,12 @@ def get_edges():
"
distance
"
:
edge
.
distance
,
"
distance
"
:
edge
.
distance
,
"
year
"
:
edge
.
year
"
year
"
:
edge
.
year
})
})
else
:
print
(
f
"
Skipping edge
{
edge
.
id
}
due to missing source or target node.
"
)
return
jsonify
({
"
nodes
"
:
node_list
,
"
edges
"
:
edge_list
})
return
jsonify
(
edge_list
)
except
Exception
as
e
:
except
Exception
as
e
:
return
jsonify
({
'
error
'
:
str
(
e
)}),
500
return
jsonify
({
'
error
'
:
str
(
e
)}),
500
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
app
.
run
(
debug
=
True
)
app
.
run
(
debug
=
True
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment