repl.it
@HristoGueorguie/

Flask test app - CYOA Creator

Python

No description

fork
loading
Files
  • main.py
  • static
  • templates
  • forms.py
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
77
78
from flask import Flask, render_template, request
from forms import AddChapterForm

flaskapp = Flask(__name__)
flaskapp.config['SECRET_KEY'] = 'not so secret'


#Sample book data for testing
Book = [
  {
    'id'   : 1, 
    'name' :'Chaper 1.',
    'text' : """chapter one yada yada yada yada y
              ada yada yada dsslakfl ssadfsafsaf
              asdfsafsd afsdafsa fsafsdaf""",
    'links': [2,3]
  },
  {
    'id'  : 2, 
    'name':'Chaper 2.',
    'text': """chapter two yada yada yada yada y
              ada yada yada dsslakfl ssadfsafsaf
              asdfsafsd afsdafsa fsafsdaf""",
    'links':[1]
  },
  {
    'id'  : 3, 
    'name':'Chaper 3.',
    'text': """chapter three yada yada yada yada y
              ada yada yada dsslakfl ssadfsafsaf
              asdfsafsd afsdafsa fsafsdaf""",
    'links':[2]
  }
  
]

#Get chapter method will retrive chapter data from 
#appropriate source
def get_chapter(chapterNumber):
  return Book[chapterNumber-1]

def set_chapter(chapterNumber, data):
  Book.append({'id':data['idNum'],
   'name':data['name'],
   'text':data['text'],
   'links':data['links']
  })
  print(Book)


#Home page
@flaskapp.route('/')
def home():
  return render_template('home.html',title='Home')

#Read current book
@flaskapp.route('/read')
def readbook():
  ch = request.args.get('chapter', default = 1, type = int)
  print(ch)
  return render_template('readbook.html',title='Book reader',chapter=get_chapter(ch))

#Add chapter to current book 
@flaskapp.route('/addchapter', methods=['GET','POST'])
def addchapter():
  #Test form
  form = AddChapterForm()
  
  #print(request.method)

  if form.validate_on_submit():
    print('yey')
    set_chapter(1, request.form)
    return request.method + request.form['name']

  return render_template('addchapter.html',title='Add Chapter', form=form)

flaskapp.run(host='0.0.0.0')
Fetching token
?