Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IIS, Python3.7 and classes
#1
Just beginning to learn about classes (I have been reading automate the boring).
Anyway I am using windows running IIS localhost to try out python in a web environment. I think I have managed to set it up to run python, struggling a bit with getting it to read a database. But before I need the database, I would like to write some classes in files and be able to import them.

However it seems it not as easy as just write the py file and import it... or perhaps it is and I`m doing it wrong.
I have got python 2.7 and 3.7 installed, as I couldnt get the python 3 to use mysql/mssql... So I mapped the scripts py2 for python 2.7 and py3 for python 3.7

File Structure:
py3
---- whattodo.py3
---- classes
-------- Person.py

whattodo.py3
# get a passed command and do something with it...
'''
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
'''
from classes import Person

print('Content-Type: text/html')
print('CharSet: ISO-8859-1')
print('')
print('<meta charset="ISO-8859-1" content="charset=ISO-8859-1">')

print('<html><head><title>.P.</title></head><body>')

p = Person('John',42)
print(p.name+' '+str(p.age))

print('</body')
print('</html>')
and the person.py:
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
So obviously I've done something wrong... is it an easy fix?
Reply
#2
(Mar-13-2019, 02:50 PM)Ecniv Wrote: and the person.py:
Then import is like this.
from person import Person

print('Content-Type: text/html')
print('CharSet: ISO-8859-1')
print('')
print('<meta charset="ISO-8859-1" content="charset=ISO-8859-1">')
print('<html><head><title>.P.</title></head><body>')
p = Person('John', 42)
print(f'<p>Name {p.name} and age {p.age}</p>')
print('</body')
print('</html>')
Output:
Content-Type: text/html CharSet: ISO-8859-1 <meta charset="ISO-8859-1" content="charset=ISO-8859-1"> <html><head><title>.P.</title></head><body> <p>Name John and age 42</p> </body </html>
Use only Python 3.7 install tutorial,and we not not write HTML like this in anymore in Python.
Look Flask for starting web-development in Python,eg a tutorial here.
Reply
#3
I had seen some things about flask.
I'll read up on it.

Thanks
Reply
#4
Hi

Sorry to bother you again.
I used the pip install for flask (seems to have done something) but on the page I found as a tutorial it uses $ and on the idle/cmd>python it doesnt recognise $ so I cannot follow it.
Something about setting up an environment.
Do you happen to have some links to good tutorials before I go off and browse some more sites?

Thanks in advance

Vince
PS. I'll try your example of the classes shortly, to see if they work

Ok, I changed the from ... import and got it working :) Thanks :)
(at least I can see it works .. now to look at flask)
Reply
#5
Hi again. Sorry.
I'm following a tutorial : http://flask.pocoo.org/docs/1.0/tutorial/
Seems to have installed flask in python, and runs. but when I go to the page it errors instead of 'hello world'
I think its the environment/folder hasnt got a copy of flask in, but I am unsure how to install it there...
copy in the development folder...

So to understand this. Flask runs alongside Python on a server (iis) and listens at a port then returns text, pages etc. ?
Instead of me creating html manualy as I posted above...?
Reply
#6
(Mar-14-2019, 11:20 AM)Ecniv Wrote: So to understand this. Flask runs alongside Python on a server (iis) and listens at a port then returns text, pages etc. ?
Instead of me creating html manualy as I posted above...?
Flask has own web-server running that you use.
Here is a basic setup,and run in browser.
Folder setup:
my_app
|- app.py
  templates\
    |- index.html
    |- about.html
  static\
    css\
      |- style.css
    js\
    img\
app.py:
from flask import Flask, render_template, jsonify, request
 
app = Flask(__name__)
@app.route('/')
def index():
   return render_template("index.html")
 
@app.route('/about',  methods=['GET'])
def about():
   return render_template("about.html") 

if __name__ == '__main__':
   app.run(debug=True)
index.html:
<!doctype html>
<html>
  <head>
     <meta charset="UTF-8">
    <title>Some title</title>
    <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" />
  </head>
  <body>
    <div id="start">
      <h1>Start page</h1>
      <ul>
        <li class="page_1"><a href="/about">About</a></li>
      </ul>
    </div>
  </body>
</html>
about.html:
<div class="foo">
  <h1>The About Page</h1>
  <a href="https://python-forum.io/">Python forum</a>
</div>
style.css:
body { 
  background: #67B26F;  
  background: -webkit-linear-gradient(to right, #4ca2cd, #67B26F);
  background: linear-gradient(to right, #4ca2cd, #67B26F);
  font-size: 100%;
  padding:0; 
  margin:0;		
}

html {
  height: 100%
}

#wrapper {
width: auto;
}
Now from command line in folder where app.py is.
Run python app.py.
Then it look like this:
E:\all_flask\2019\my_app
λ python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 334-187-997
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
See address copy that to browser http://127.0.0.1:5000/.
Then should show the page and with a link to about page.
Reply
#7
Hi

Ah I understand.
Ok copied your example, works perfectly.
Ill find a better tutorial/training on flask.

Thanks for all your help
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020