Skip to content
Snippets Groups Projects
Commit 2fc68167 authored by Dorothee Hueser's avatar Dorothee Hueser
Browse files

inputs from client by form

parent 91df0876
Branches
No related tags found
No related merge requests found
...@@ -183,17 +183,17 @@ $:content ...@@ -183,17 +183,17 @@ $:content
</html> </html>
``` ```
and the index.html: and the index.html:
``` ```html
$def with (data) $def with (data)
<h2>${data['one']}</h2> <h2>${data['one']}</h2>
<p>At Hamburg we say ${data['two']}</p> <p>At Hamburg we say ${data['two']}</p>
``` ```
### Several pages of a site ### Several pages of a site; rendering html
If another page shall be reached via some link or submitting a form, e.g. by If another page shall be reached via some link or submitting a form, e.g. by
``` ```html
$def with (data) $def with (data)
<h2>${data['one']}</h2> <h2>${data['one']}</h2>
...@@ -227,3 +227,150 @@ if __name__ == "__main__": ...@@ -227,3 +227,150 @@ if __name__ == "__main__":
app.run() app.run()
``` ```
In this example, the `/` and `tst` denote the url names and `index` the class which will be called if http requests `/` and `dosomething` the class which will be called if http requests `tst`. In this example, the `/` and `tst` denote the url names and `index` the class which will be called if http requests `/` and `dosomething` the class which will be called if http requests `tst`.
### From submission with file upload
The python script webapp.py gets a new class called, e.g. `upload`, and among the templates a new html-files is added, e.g. called `uploadinputfiles.html`.
webapp.py:
```python
#!/usr/bin/env python3
import web
import os
#from cheroot.server import HTTPServer
#from cheroot.ssl.builtin import BuiltinSSLAdapter
#HTTPServer.ssl_adapter = BuiltinSSLAdapter(
# certificate='server.crt',
# private_key='server.key')
urls = (
'/', 'index',
'/upload', 'upload'
)
render = web.template.render('templates', base='base')
class upload:
def GET(self):
return render.uploadinputfiles()
def POST(self):
files = web.webapi.rawinput().get('upload')
if not isinstance(files, list):
files = [files]
for f in files:
# here in this example files to go to the directory usr/thisuser
target = os.path.join('usr', 'thisuser', f.filename)
content = f.file.read().decode('ascii', errors='ignore')
# here code could go to sanitize the content or to parse/convert the file or so ....
with open(target, 'w') as f:
f.write(content)
return "Data files have successfully been uploaded"
class index:
def GET(self):
data = {}
data['indextitle'] = 'Welcome to Foo'
data['indexmessage1'] = 'Areal surface topography analysis'
data['indexmessage2'] = 'go to upload page'
return render.index(data)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
```
The link address in index.html has now been changed from `tst` to `upload`, the route in the structure `urls` accordingly.
uploadinputfiles.html:
```html
<form method="POST" enctype="multipart/form-data" action="">
Select ascii files: <input type="file" name="upload" multiple><br>
<input type="submit">
</form>
```
### Formsubmission with user input
Another class is included into `webapp.py` and and another template, for example denoted `getusrparams.html` is added accordingly. This example uses an external file named `paramtemplates/config_template.txt` with the list of input parameters to be filled by the user. The parameter names are read as strings and then converted into JSON storages, eg. by declaring `params = {}` and then filling the strings read from file into the storage list in JSON format by `params[ictr] = line.lower().strip()`, somehow python does it by itself....
webapp.py:
```python
...
urls = (
'/', 'index',
'/upload', 'upload',
'/getusrparams', 'getusrparams'
)
...
class getusrparams:
def GET(self):
title = 'Input of simulation parameters'
material = {}
detector = {}
params = {}
with open('paramtemplates/config_template.txt', "r") as fp:
ictr = 0
for line in fp:
if (line.lower().find('material') > -1):
ileft = line.find('[')
iright = line.find(']')
thelist = line[ileft+1:iright].split(',')
for k in range(0, len(thelist)):
material[k] = thelist[k].strip()
elif (line.lower().find('detector') > -1):
ileft = line.find('[')
iright = line.find(']')
thelist = line[ileft+1:iright].split(',')
for k in range(0, len(thelist)):
detector[k] = thelist[k].strip()
else:
params[ictr] = line.lower().strip()
print(params[ictr])
ictr = ictr + 1
return render.getusrparams(title, material, detector, params)
def POST(self):
with open('usr/thisuser/config_thisuser.txt', 'w') as fin:
allwebinput = web.input()
print(len(allwebinput), allwebinput)
hlp = json.dumps(allwebinput)
splittedstr = hlp.replace('"','').split(',')
for i in range(0, len(splittedstr)):
outstr = splittedstr[i].replace(':','').replace('{','').replace('}','')
fin.write(outstr.strip()+"\n")
fin.close()
return 'done'
```
on the viewer's side it looks as follows
getusrparams.html:
```html
$def with (title, material, detector, params)
<h2>${title}</h2>
<form id="form" method="post" action="getusrparams">
<table>
<tr><td>material</td><td>
<select name="material">
$for i in range(0,len(material)):
<option value="${material[i]}">${material[i]}</option>
</select>
<tr><td>detector</td><td>
<select name="material">
$for i in range(0,len(detector)):
<option value="${detector[i]}">${detector[i]}</option>
</select>
</td></tr>
$for i in range(0,len(params)):
<tr><td>${params[i]}</td>
<td><input type="text" name="${params[i]}" size="20" /></td></tr>
</table>
<input type="submit">
</form>
```
#sample film_TiO2 1.0 1.0 0.135 micron 3.9 g/cm3
#detector FEI
#workingdistance 3.0 mm
#Energy 20. keV
#beamFWHM 0. nm
#beamdiv 0. rad
#Startposition 0. 0.0 0.0 micron
#Scanstep 0.005 0.0 0.0 micron
#Numofparticles 500000
#Numslowscanaxis 1
#Numfastscanaxis 1
#Thicknessscan 0. 2 132 nm
\ No newline at end of file
material [SiO2, C, Au, TiO2]
detector [FB42, FEIfreefield, FEIimmersion]
workingdistance
Energy
beamFWHM
beamdiv
Startposition
Scanstep
Numofparticles
Thicknessscan/nm from step to
\ No newline at end of file
...@@ -9,6 +9,6 @@ $def with (content) ...@@ -9,6 +9,6 @@ $def with (content)
$:content $:content
<hr> <hr>
<i>created: Dorothee Hüser, 2020-08-21, last edit: 2020-08-21</i> <i>created: Dorothee Hüser, 2020-08-21, last edit: 2020-08-23</i>
</body> </body>
</html> </html>
\ No newline at end of file
$def with (title, material, detector, params)
<h2>${title}</h2>
<form id="form" method="post" action="getusrparams">
<table>
<tr><td>material</td><td>
<select name="material">
$for i in range(0,len(material)):
<option value="${material[i]}">${material[i]}</option>
</select>
<tr><td>detector</td><td>
<select name="material">
$for i in range(0,len(detector)):
<option value="${detector[i]}">${detector[i]}</option>
</select>
</td></tr>
$for i in range(0,len(params)):
<tr><td>${params[i]}</td>
<td><input type="text" name="${params[i]}" size="20" /></td></tr>
</table>
<input type="submit">
</form>
$def with (data) $def with (data)
<h2>${data['one']}</h2> <h2>${data['indextitle']}</h2>
<p>At Hamburg we say ${data['two']}</p> <p>${data['indexmessage1']}</p>
<p><a href="tst">go to next page</a></p> <p><a href="upload">${data['indexmessage2']}</a></p>
<p><a href="getusrparams">${data['indexmessage3']}</a></p>
<form method="POST" enctype="multipart/form-data" action="">
Select ascii files: <input type="file" name="upload" multiple><br>
<input type="submit">
</form>
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
energy 20 keV
material Au
workingdistance 3 mm
scanstep 1 nm
thicknessscan/nm from step to 0 1.5 100 nm
beamfwhm 2 nm
beamdiv 0.02 mrad
numofparticles 100000
startposition 0 nm
webapp.py 100644 → 100755
#!/usr/bin/env python3
import web import web
import os
import json
#from cheroot.server import HTTPServer #from cheroot.server import HTTPServer
#from cheroot.ssl.builtin import BuiltinSSLAdapter #from cheroot.ssl.builtin import BuiltinSSLAdapter
...@@ -9,22 +12,81 @@ import web ...@@ -9,22 +12,81 @@ import web
urls = ( urls = (
'/', 'index', '/', 'index',
'/tst', 'dosomething' '/upload', 'upload',
'/getusrparams', 'getusrparams'
) )
render = web.template.render('templates', base='base') render = web.template.render('templates', base='base')
class dosomething:
def GET(self):
return "do something else"
class index: class index:
def GET(self): def GET(self):
data = {} data = {}
data['one'] = 'my nice Title' data['indextitle'] = 'Welcome to Foo'
data['two'] = 'moin moin' data['indexmessage1'] = 'Areal surface topography analysis'
data['indexmessage2'] = 'go to upload of ascii files'
data['indexmessage3'] = 'go to user input form'
return render.index(data) return render.index(data)
class upload:
def GET(self):
return render.uploadinputfiles()
def POST(self):
files = web.webapi.rawinput().get('upload')
if not isinstance(files, list):
files = [files]
for f in files:
# here in this example files to go to the directory usr/thisuser
target = os.path.join('usr', 'thisuser', f.filename)
content = f.file.read().decode('ascii', errors='ignore')
# here code could go to sanitize the content or to parse/convert the file or so ....
with open(target, 'w') as f:
f.write(content)
return "Data files have successfully been uploaded"
class getusrparams:
def GET(self):
title = 'Input of simulation parameters'
material = {}
detector = {}
params = {}
with open('paramtemplates/config_template.txt', "r") as fp:
ictr = 0
for line in fp:
if (line.lower().find('material') > -1):
ileft = line.find('[')
iright = line.find(']')
thelist = line[ileft+1:iright].split(',')
for k in range(0, len(thelist)):
material[k] = thelist[k].strip()
elif (line.lower().find('detector') > -1):
ileft = line.find('[')
iright = line.find(']')
thelist = line[ileft+1:iright].split(',')
for k in range(0, len(thelist)):
detector[k] = thelist[k].strip()
else:
params[ictr] = line.lower().strip()
print(params[ictr])
ictr = ictr + 1
return render.getusrparams(title, material, detector, params)
def POST(self):
with open('usr/thisuser/config_thisuser.txt', 'w') as fin:
allwebinput = web.input()
print(len(allwebinput), allwebinput)
hlp = json.dumps(allwebinput)
splittedstr = hlp.replace('"','').split(',')
for i in range(0, len(splittedstr)):
outstr = splittedstr[i].replace(':','').replace('{','').replace('}','')
fin.write(outstr.strip()+"\n")
fin.close()
return 'done'
if __name__ == "__main__": if __name__ == "__main__":
app = web.application(urls, globals()) app = web.application(urls, globals())
app.run() app.run()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment