Skip to content

Commit b2938c3

Browse files
author
tsv2013
committed
Initial commit
0 parents  commit b2938c3

23 files changed

+6334
-0
lines changed

.gitignore

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# UV
98+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
#uv.lock
102+
103+
# poetry
104+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105+
# This is especially recommended for binary packages to ensure reproducibility, and is more
106+
# commonly ignored for libraries.
107+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108+
#poetry.lock
109+
110+
# pdm
111+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112+
#pdm.lock
113+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114+
# in version control.
115+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116+
.pdm.toml
117+
.pdm-python
118+
.pdm-build/
119+
120+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121+
__pypackages__/
122+
123+
# Celery stuff
124+
celerybeat-schedule
125+
celerybeat.pid
126+
127+
# SageMath parsed files
128+
*.sage.py
129+
130+
# Environments
131+
.env
132+
.venv
133+
env/
134+
venv/
135+
ENV/
136+
env.bak/
137+
venv.bak/
138+
139+
# Spyder project settings
140+
.spyderproject
141+
.spyproject
142+
143+
# Rope project settings
144+
.ropeproject
145+
146+
# mkdocs documentation
147+
/site
148+
149+
# mypy
150+
.mypy_cache/
151+
.dmypy.json
152+
dmypy.json
153+
154+
# Pyre type checker
155+
.pyre/
156+
157+
# pytype static type analyzer
158+
.pytype/
159+
160+
# Cython debug symbols
161+
cython_debug/
162+
163+
# PyCharm
164+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166+
# and can be added to the global gitignore or merged into this file. For a more nuclear
167+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
168+
#.idea/
169+
170+
# Ruff stuff:
171+
.ruff_cache/
172+
173+
# PyPI configuration file
174+
.pypirc
175+
176+
# Custom
177+
__pycache__
178+
.venv
179+
flask_session

.vscode/launch.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python Debugger: Flask",
9+
"type": "debugpy",
10+
"request": "launch",
11+
"module": "flask",
12+
"env": {
13+
"FLASK_APP": "app.py",
14+
"FLASK_DEBUG": "1"
15+
},
16+
"args": [
17+
"run",
18+
"--no-debugger",
19+
"--no-reload"
20+
],
21+
"jinja": true,
22+
"autoStartBrowser": false
23+
}
24+
]
25+
}

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SurveyJS + Python/Flask Demo Example
2+
3+
This demo shows how to integrate [SurveyJS](https://surveyjs.io/) components with a Python/Flask backend.
4+
5+
[View Demo Online](https://surveyjs-flask.azurewebsites.net/)
6+
7+
## Disclaimer
8+
9+
This demo must not be used as a real service as it doesn't cover such real-world survey service aspects as authentication, authorization, user management, access levels, and different security issues. These aspects are covered by backend-specific articles, forums, and documentation.
10+
11+
## Run the Application
12+
13+
Install [NodeJS](https://nodejs.org/) on your machine. After that, run the following commands:
14+
15+
```bash
16+
git clone https://github.com/surveyjs/surveyjs-flask.git
17+
cd surveyjs-flask
18+
19+
# python -m venv .venv # create venv in working folder if needed - windows
20+
# python3 -m venv .venv # macos/linux
21+
pip install -r requirements.txt
22+
23+
```
24+
25+
Open http://localhost:5000 in your web browser.
26+
27+
## Client-Side App
28+
29+
The client-side part is the `surveyjs-react-client` React application. The current project includes only the application's build artifacts. Refer to the [surveyjs-react-client](https://github.com/surveyjs/surveyjs-react-client) repo for full code and information about the application.

app.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
import os
3+
from flask import Flask, jsonify, request, session, send_from_directory
4+
from flask_session import Session
5+
from inmemorydbadapter import InMemoryDBAdapter
6+
7+
app = Flask(__name__, static_folder="public/static")
8+
# Set the secret key to some random bytes. Keep this really secret!
9+
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
10+
app.config['SESSION_TYPE'] = 'filesystem'
11+
app.config['SESSION_PERMANENT'] = False
12+
Session(app)
13+
14+
API_BASE_ADDRESS = "/api"
15+
16+
def get_db_adapter():
17+
return InMemoryDBAdapter(session)
18+
19+
@app.route(API_BASE_ADDRESS + "/getActive", methods=['GET'])
20+
def get_active():
21+
db = get_db_adapter()
22+
return jsonify(db.get_surveys())
23+
24+
@app.route(API_BASE_ADDRESS + "/getSurvey", methods=['GET'])
25+
def get_survey():
26+
db = get_db_adapter()
27+
survey_id = request.args.get('surveyId')
28+
return jsonify(db.get_survey(survey_id))
29+
30+
@app.route(API_BASE_ADDRESS + "/changeName", methods=['GET'])
31+
def change_name():
32+
db = get_db_adapter()
33+
obj_id = request.args.get('id')
34+
new_name = request.args.get('name')
35+
return jsonify(db.change_name(obj_id, new_name))
36+
37+
@app.route(API_BASE_ADDRESS + "/create", methods=['GET'])
38+
def create():
39+
db = get_db_adapter()
40+
name = request.args.get('name')
41+
return jsonify(db.add_survey(name))
42+
43+
@app.route(API_BASE_ADDRESS + "/changeJson", methods=['POST'])
44+
def change_json():
45+
db = get_db_adapter()
46+
data = request.get_json()
47+
obj_id = data.get('id')
48+
json_data = data.get('json')
49+
return jsonify(db.store_survey(obj_id, None, json_data))
50+
51+
@app.route(API_BASE_ADDRESS + "/post", methods=['POST'])
52+
def post_results():
53+
db = get_db_adapter()
54+
data = request.get_json()
55+
post_id = data.get('postId')
56+
survey_result = data.get('surveyResult')
57+
return jsonify(db.post_results(post_id, survey_result))
58+
59+
@app.route(API_BASE_ADDRESS + "/delete", methods=['GET'])
60+
def delete():
61+
db = get_db_adapter()
62+
obj_id = request.args.get('id')
63+
db.delete_survey(obj_id)
64+
return jsonify({'id': obj_id})
65+
66+
@app.route(API_BASE_ADDRESS + "/results", methods=['GET'])
67+
def get_results():
68+
db = get_db_adapter()
69+
post_id = request.args.get('postId')
70+
return jsonify(db.get_results(post_id))
71+
72+
@app.route('/', defaults={'path': 'index.html'})
73+
@app.route('/about', defaults={'path': 'index.html'})
74+
@app.route('/run/<path:path>')
75+
@app.route('/edit/<path:path>')
76+
@app.route('/results/<path:path>')
77+
def serve_static(path):
78+
return send_from_directory('public', path)
79+
80+
if __name__ == '__main__':
81+
if not os.path.exists('flask_session'):
82+
os.makedirs('flask_session')
83+
84+
app.run(debug=True)

0 commit comments

Comments
 (0)