Python Revisited 2024: Difference between revisions
Line 66: | Line 66: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[File:Flask running.png| 600px]] | [[File:Flask running.png| 600px]] | ||
=Header Files In Python= | |||
I have found it a real struggle to keep this in my mind, maybe because of dyslexia or just it is odd. So I write it here to help. Basically the __init__.py is the .def, .h or even the d.ts. If you see | |||
<syntaxhighlight lang="py"> | |||
TypeError: 'module' object is not callable | |||
</syntaxhighlight> | |||
This probably means you have not export the function or type but have used it in the code. There seems to be a naming convention where they do not use the function name as the name of the file. For example [https://github.com/hrimov/flask-template flask-template] uses session.py for the function create_session_maker. There is only one function in it so I don't understand this approach. |
Revision as of 04:49, 27 October 2024
Introduction
A quick revisit to python to maybe improve/refresh my own knowledge.
Project Creation (Poetry)
Guess most folks know about this and knowing IT, some people will hate it, and some will love it. Coming fresh out of NodeJS and React/Nextjs, this seemed a good idea to me. I am starting to feel with AI, that technologies are going to be changing quickly and being able to get going quickly is useful. This looked easy but when I tried it, it was harder. Here goes
Make a Project
poetry new my-project
Make a Fask App
Now lets make a Flask app. We will start by putting the code in src/app
cd my-project
mkdir -p src/app
touch src/app/__init__.py
touch src/app/__main__.py
Now we can make the src/app/__main__.py
from flask import Flask
def main() -> Flask:
app = Flask(__name__)
return app
def run():
app = main()
app.run("localhost", 5000)
if __name__ == "__main__":
run()
Modify the pyproject.toml
The main thing is to change the packages line. I had a bit of trouble getting pylance to find my packages because, maybe, I did not poetry install after this. See below.
[tool.poetry]
name = "my-project"
version = "0.1.0"
description = ""
authors = ["Bill Wiseman <bw@bibble.co.nz>"]
readme = "README.md"
packages = [{ include = "app", from = "src" }]
[tool.poetry.dependencies]
python = "^3.12"
[tool.poetry.group.dev.dependencies]
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Install Flask
So we install Flask add run install poetry
poetry add Flask
poetry install
poetry run python3 -m app
When I ran the code it picked up a previous project and I had to remove the cache with
rm -rf ~/.cache/pypoetry/virtualenvs/
Header Files In Python
I have found it a real struggle to keep this in my mind, maybe because of dyslexia or just it is odd. So I write it here to help. Basically the __init__.py is the .def, .h or even the d.ts. If you see
TypeError: 'module' object is not callable
This probably means you have not export the function or type but have used it in the code. There seems to be a naming convention where they do not use the function name as the name of the file. For example flask-template uses session.py for the function create_session_maker. There is only one function in it so I don't understand this approach.