Skip to content

Instantly share code, notes, and snippets.

@BransonGitomeh
Created September 11, 2016 19:25
Show Gist options
  • Save BransonGitomeh/06a76179c590fd99ca417d92cf8638d0 to your computer and use it in GitHub Desktop.
Save BransonGitomeh/06a76179c590fd99ca417d92cf8638d0 to your computer and use it in GitHub Desktop.
pyramid framework stuff. with endpoints that return json .trying to connect to a db
# -*- coding: UTF-8 -*-
import os
import logging
import json
from pyramid.config import Configurator
from pyramid.events import NewRequest
from pyramid.events import subscriber
from pyramid.events import ApplicationCreated
from pyramid.exceptions import NotFound
from pyramid.httpexceptions import HTTPFound
from pyramid.session import UnencryptedCookieSessionFactoryConfig
from pyramid.view import view_config
from wsgiref.simple_server import make_server
from neo4j.v1 import GraphDatabase, basic_auth
from pyramid.renderers import JSON
logging.basicConfig()
log = logging.getLogger(__file__)
here = os.path.dirname(os.path.abspath(__file__))
# views
@view_config(route_name='list', renderer='json')
def list_view(request):
return {
'project': 'this will return a list',
"user":"branson"
}
@view_config(route_name='new', renderer='json')
def new_view(request):
return {
'project': request.POST.get('ajax_id'),
"user":"branson"
}
@view_config(route_name='test', renderer='json')
def test_view(request):
from neo4j.v1 import GraphDatabase, basic_auth
CITIES = {
'paris': {
'name': 'Paris',
'population': '2,234,105'
},
'sf': {
'name': 'San Francisco',
'population': '812,826'
}
}
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "a10101995"))
session = driver.session()
session.run("CREATE (a:Person {name:'Bob'})")
result = session.run("MATCH (a:country) RETURN a")
country = {}
for record in result:
print(vars(record))
# here
return country
@view_config(context='pyramid.exceptions.NotFound', renderer='notfound.mako')
def notfound_view(request):
request.response.status = '404 Not Found'
return {}
if __name__ == '__main__':
# configuration settings
settings = {}
settings['reload_all'] = True
settings['debug_all'] = True
# session factory
session_factory = UnencryptedCookieSessionFactoryConfig('itsaseekreet')
# configuration setup
config = Configurator(settings=settings, session_factory=session_factory)
# add mako templating
config.include('pyramid_mako')
# routes setup
config.add_route('list', '/')
config.add_route('new', '/new')
config.add_route('test', '/test')
config.add_route('close', '/close/{id}')
# static view setup
config.add_static_view('static', os.path.join(here, 'static'))
# scan for @view_config and @subscriber decorators
config.scan()
# serve app
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
print("server has started at 8080")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment