I am reading other people's code and found extensions.py in their package.
I can see the modules imported in the extensions.py are imported in init.py as well.
I could not find how the extensions.py works with init.py and in what situation you need to use the extensions.py.
Could anyone give me some explaination or provide some link that explain it?
In init.py
from flask_app.extensions import cors, guard
In extension.py
from flask_praetorian import Praetorian
cors = CORS()
guard = Praetorian()
According to Python’s package tutorial this is the minimal structure:
packaging_tutorial/
├── LICENSE
├── pyproject.toml
├── README.md
├── setup.cfg
├── src/
│ └── example_package/
│ ├── __init__.py
│ └── example.py
└── tests/
Seems as its just a backup for installing requirements, or for more readability. Maybe provide where you found extensions.py, and I can take a deeper look.
You could also dig deeper into docs and see exactly what flask-praetorian does.
I think It's just a backup for installing things like requirements.
Related
How can I use an external npm library in my theme to use it in the storefront?
To give an example, how would you include https://www.npmjs.com/package/slick-carousel? This gives JS- and SCSS/CSS-files inside node_modules. I know that there is an included slider with tinyslider that one could use, but the question is more about including and using the external ressoures. I couldn't find any guide/documentation about such a case unfortunately.
Please see the documentation regarding adding NPM dependencies.
You need to import the scss/css within your plugins base.scss (f.e.)
We are copying the css as scss withing install of package.json into the Resources/src/scss-Folder to have it easier to import it.
We found a solution to import styles from NPM packages.
storefront
├── build
│ └── webpack.config.js
├── dist
├── node_modules
│ └── #my-organisation
│ └── my-package
│ └── dist
│ ├── style.css
│ └── script.js
├── package-lock.json
├── package.json
└── src
└── scss
└── base.scss
In your base.scss you now can import the styles like this:
#import "../../node_modules/#my-organisation/my-package/dist/style"
The important part for CSS-Files is, that you don NOT specify the file-extension in the import, otherwise the import-statement will count as a regular CSS-import, see https://sass-lang.com/documentation/at-rules/import#importing-css
In addition to importing .sass and .scss files, Sass can import plain old .css files. The only rule is that the import must not explicitly include the .css extension, because that’s used to indicate a plain CSS #import.
In my company, we have a Python project that contains a hierarchy of lots of packages and modules shared by our different applications. But what seemed a good idea to mutualise code has become something horribly difficult to use and maintain.
Depending on the end-project, we use a single module from this library, or a single package, or many. And some modules/packages are independent, but some others depend on other packages and modules from the same library. And of course those modules depend on third-party packages.
I would like to make it as modular as possible, i.e. I would like to deal with the following cases:
use the whole library
use a single package from that library (whether it is a top level package or not)
use a single module from the library
use multiple packages/modules from the library (possibly interdependent)
Moreover, a strong constraint I have is not to break existing code so that I can make the project transformation without breaking all the projects of my coworkers...
Here is an example file tree that represents the situation:
library
├── a
│ └── i
│ ├── alpha.py # each module may depend on any other package / module
│ └── beta.py
├── b
│ ├── delta.py
│ ├── gamma.py
│ └── j
│ └── epsilon.py
├── c
│ ├── mu.py
│ └── nu.py
├── requirements.txt
└── setup.py
The best solution I found is to add a setup.py and a requirements.txt in every folder of the tree. But this has serious limitations:
I cannot use a single module (I have to use a package).
When I use a package, I have to change its import statements. For example if, before any change, I use from library.a.i import alpha, I would like not to modify it afterwards.
Moreover, I am quite sure I am forgetting some of the constraints I have...
So is what I am trying to achieve feasible, or is it utopian?
What you can do is the following:
You need to have PYTHONPATH pointing on library or append it to sys.path
eg sys.path.insert(0, 'path_to_libray')
If you create __init__.py at each level of your folders you will be able to pick whatever level/module of interest eg:
in folder b :
from .delta import *
from .gamma import *
from b.j import *
in folder j:
from .epsilon import *
You can now do in any python script:
from b import * : will import all of b contained modules
from b.j import *: will import only epsilon stuff
I have just published a new TypeScript-based module to the NPM registry, ooafs. However, when I try to install it and import it in another TypeScript project, VSCode gives me the following error on the import statement: Cannot find module 'ooafs'.ts(2307).
This module's source files are compiled to JavaScript to a dist/ folder and definitions (.d.ts) are also generated.
Here's the tree of the published module (the one we download when we npm install):
.
├── dist
│ ├── Entry.d.ts
│ ├── EntryFilter.d.ts
│ ├── EntryFilter.js
│ ├── Entry.js
│ ├── EntryType.d.ts
│ ├── EntryType.js
│ ├── FSTypings.d.ts
│ ├── FSTypings.js
│ ├── index.d.ts
│ └── index.js
├── LICENSE
├── package.json
└── README.md
The package.json does contain the following entries:
{
"main": "dist/index.js",
"types": "dist/index.d.ts",
...
}
Because the module works normally on Runkit (pure JS), I assume the only problem I have is related to TypeScript, and it's not the first time TypeScript tells me a module doesn't exist when missing declaration files are the only problem.
Am I missing a step in the compilation process ?
Are my package.json properties wrong ?
If you need to see more code, the Github link is at the beginning of the question, and the published module structure can be found here: https://unpkg.com/ooafs#0.1.2/dist/.
Actually, the problem didn't come from my module (ooafs). It was a problem with the tsconfig.json of the project I was using the module in: The module property must be set to commonjs apparently.
Very late edit:
Also, I highly recommend setting esModuleInterop to true which allows you to import non-es6 modules in a more natural manner.
The answer is not the fix, and is certainly not ideal when you have to use top-level awaits (which don't work on commonjs).
You want to make sure your import path is the final file that node will try and load. So you cannot rely on folders resolving to folder/index.js and you cannot rely on giving file names without extensions (give the ".js" extension)
How to start the pyscaffold project?
I use this command to create the project putup sampleProject
but i don't know how to start this project?
You don't start a pyscaffold project per say -- Its goal is simply to create the files and folder that you will commonly need for your project. See my structure below from "putup MyTestProject". Look at all the nice stuff already created that you now don't have to do by hand.
To get started, you need to start adding packages/code to "..src/mytestproject" and run that code like you normally would.
Might I recommend for you the use of a good IDEA, such as pycharm? I think you will find it makes starting your journey much easier.
A second recommendation -- if you are just getting started, you might skip pyscaffold for now. While a great tool, it might add confusion that you don't need right now.
MyTestProject/
├── AUTHORS.rst
├── CHANGELOG.rst
├── docs
│ ├── authors.rst
│ ├── changelog.rst
│ ├── conf.py
│ ├── index.rst
│ ├── license.rst
│ ├── Makefile
│ └── _static
├── LICENSE.txt
├── README.rst
├── requirements.txt
├── setup.cfg
├── setup.py
├── src
│ └── mytestproject
│ ├── __init__.py
│ └── skeleton.py
└── tests
├── conftest.py
└── test_skeleton.py
[Edit]
With respect to why "python skeleton.py" gives an output, the library is simply providing an example to show the user where to start adding code, and how the code relates to the tests (test_skeleton.py). The intent is that skeleton.py will be erased and replaced with your code structure. This may be some python.py files or packages and sub packages with python.py files. Read it this way; "Your Code goes here ... and here is an arbitrary example to get you started."
But you have to ask yourself what you are trying to accomplish? If you are just creating a few scripts for yourself -- for nobody else in the world to see, do you need the additional stuff (docs, setup, licensing, etc?) If the answer is no - don't use pyscaffold, just create your scripts in a venv and be on your way. This scaffolding is meant to give you most of what you need to create a full, github worthy, project to potentially share with the world. Based on what I gather your python experience to be, I don't think you want to use pyscaffold.
But specific to your question. Were I starting with pyscaffold, I would erase skeleton.py, replace it with "mytester.py", use the begins library to parse my incoming command arguments, then write individual methods to respond to my command line calls.
I want to create a library in Typescript that I can share via npm. Specifically, I want to use webpack to generate a js bundle along with a definition file to share the types with the js. So I'd have a tree of files like:
├── lib
│ ├── lib.d.ts
│ └── lib.min.js
├── test
...
├── ts
│ ├── errors
│ │ ├── CannotModifyAlteredObject.ts
│ ├── Lib.ts
│ ├── PostProcessors.ts
│ ├── Serializers.ts
├── tsconfig.json
├── typings.json
├── LICENSE
├── package.json
├── README.md
└── webpack.lib.config.js
And all the types exported by ts/Lib.ts would be exported to a single .d.ts in the lib directory to sit next to the js bundle.
I've looked at the following questions/sources:
Writing npm modules in typescript
How to create a typescript library (and the question it duplicates)
This unanswered question
The offical typescript guide to creating packages
This example typescript library project
And another SO question
However, none of these provide an example using webpack. Being able to bundle everything you need to use the library (apart from the nodejs runtime) into a single file is pretty important for my use case, so webpack fits this role well. I'd like to be able to generate a .d.ts file that maps to what webpack creates. However, I want to avoid creating the .d.ts file manually - it should be possible to automatically extract the types without having manually created .d.ts files get out of sync with my source code. Is there a way of doing this?