Git Product home page Git Product logo

pyknit's Introduction

pyKnit

pyKnit

A set of tools for knitters to do math, create charts, and eventually more.

pyknit On PyPI

Installing pyKnit

pip install pyknit

If you want to use the latest and greatest grab pyknit from github and use pip install -e to install it in "editable" mode.

git clone https://github.com/terriko/pyknit
pip install -e pyknit/

Note that when you're doing pip install -e pyknit/ here that pyknit/ refers to the directory. (You don't really need the slash but it makes that more clear.) If you get an error about not being able to find setup.py you're probably giving it the wrong directory (and may need to cd .. to go up one directory).

Jupyter-lab Usage

pyKnit works best in conjunction with Jupyter. This allows you to "mess around" with the functions and see the results in your browser. I like this especially for the ability to display the charts inline.

Make sure you install Jupyter and pyknit in the same place (e.g. in the same virtualenv if you're using one) so that you can use import pyknit and have it work. (You can read more about how to install a python Package in Jupyter here.)

I prefer to use virtualenv and pip, so I set up my environment as follows:

virtualenv -p python3.8 venv-3.8-jupyter
source venv-3.8-jupyter/bin/activate
pip install jupyterlab
pip install pyknit
jupyter-lab

Developers may prefer to get pyknit from github and use pip install -e $pyknit_directory in lieu of pip install pyknit Remember to restart the notebook kernel to get any changes you've made in the pyknit directory while you're editing the code!

From there, jupyter lab will open in a browser, and you can create a new notebook to play around. When you're done, you can shut down the notebook server using ^C in the console and typing y when it asks if you want to shut down. You can deactivate the virtualenv by typing deactivate and pressing enter.

If you want to run it again later, you can do the following:

source venv-3.8-jupyter/bin/activate
jupyter-lab

(The virtualenv only needs to be created once, and you don't need to reinstall.)

Using PyKnit

import pyknit

# Sweater fit check math.  My gauge doesn't match the pattern but I like the
# fabric I got in my swatch and don't want to change the needle I'm using.
# What size should I knit?

pattern_g = pyknit.GaugeSwatch(
    stitch_count=27.5, stitch_measure=10, row_count=40, row_measure=4, units="in"
)
my_g = pyknit.GaugeSwatch(
    stitch_count=23.5, stitch_measure=10, row_count=33, row_measure=4, units="in"
)

# The closest size to my measurements is the 42in chest one, let's convert that

size3 = pyknit.convert_stitch_measure(42, pattern_g, my_g)
print(f"{size3=}")

Running that gives a new size of 49 inches. This sweater was designed for +/- 2 inches (it says so in the pattern), so that's too big unless I love a really baggy sweater.

size2 = pyknit.convert_stitch_measure(38, pattern_g, my_g)
size1 = pyknit.convert_stitch_measure(34, pattern_g, my_g)
print(f"{size2=}")
print(f"{size1=}")

Those give me 44.25 inches and 40 inches respectively (rounded to the nearest quarter inch, anyhow)

If I was aiming for exactly 42 inches chest circumference, I'd have the option of either knitting the 44 inch one and having 2 inches positive ease (i.e. the sweater would be a little loose) or the 40 inch one with -2 inches positive ease (i.e. the sweater would stretch to fit me), and I would choose whichever one I think I'd like better assuming my swatch is sufficiently stretchy.

This SweaterFit Example file is here if you want to sub in your own swatch numbers.

Here's an example of how to calculate sweater sleeve decreases using pyknit to get you started.

For those using Jupyter, there are also several full interactive notebooks available:

Why pyKnit?

There's lots of great tools for knitters out there, but they're not open source, which means if I don't like the way they work, I'm often stuck with weird workarounds. I thought it would be fun to make some knitting tools that were a bit more flexible.

I knit during conference talks, and every year at PyCon I meet a few folk who stop by to ask what I'm knitting and or to show me what they're working on. So I know there's enough of an intersection between the knitting and Python communities for this to be fun for more than just me.

And finally, knitting involves a lot of math. Not every knitter loves math, and even those who do can get tired of calculating and recalculating. So why not let the computer do that work? I'd like to imagine a world where I could publish a pattern with an interactive notebook and let people fit sweaters specifically to their measurements, or figure out how to adjust a shawl if you want to use all of a pretty gradient skein of yarn, or have the option to add calculated stitch counts to every single row of your pattern so people can check if they messed something up.

It's going to be a while before we get to the complicated stuff, but you have to start somewhere!

Contributing

Got an idea for a function you wish we had? Think you found a bug? Let us know via the issues.

Know open source and want to just get into it? Make a pull request here

Never contributed to open source before? Not sure what open source is? Here's a guide on How to contribute to open source that includes information on why you might want to and how to do it. I work with new contributors regularly in my day job, so don't be shy! I'm happy to help you figure it out.

If you find a security issue and want to contact me privately, send me an email. It might feel silly to talk about security issues in knitting software, but it does parse things and display things, so it's possible!

License

pyKnit is licensed under the GPL-2.0 License

pyknit's People

Contributors

96radhikajadhav avatar ben-hampson avatar fhightower avatar hippietilley avatar hvfrancis avatar irmgardw avatar jason-clarke avatar jinder1s avatar jugmac00 avatar kenzie-rasmussen avatar obitorasu avatar sorla avatar terriko avatar wilsonify avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pyknit's Issues

Set up black to run in CI / pre-commit

My other projects use black for code formatting, so I think I'd like to use it here as well, but I haven't been doing so yet. Setting it up in github actions would make sure I actually do it. :)

Read charts from .csv files (or other spreadsheet files?)

Currently, we have a (somewhat simplistic) parser for written instructions that generates charts, but we'd like to be able to also generate written instructions from charts. One step for this would be reading charts out of spreadsheets or other formats. I'm not actually entirely sure how this would work, as a typical chart includes colours and stuff that wouldn't necessarily be seen in a .csv export. But let's start with the easiest part of the problem and see if we can at least read symbols from a .csv file and get them into some sort of Chart data structure for further manipulation.

Link to example notebook in docs

In #22 we got a notebook version of my sleeve decrease example. We should probably link it somewhere in the written docs so that people can find it and download it and change the values for their own sleeve calculations.

Set up doctests

@ben-hampson found a doc bug in #18 that definitely wouldn't have happened if I had doctests, so this bug is a reminder that it wouldn't be a bad idea to set up Sphinx Doctests and then automatically build the documentation. (And maybe get it pushed to readthedocs?)

Currently the docs are in Markdown but I'm happy to switch to RestructuredText if that makes this easier.

Improve Jupyter-lab setup instructions

In the setup, the first two lines are:

virtualenv -p python 3.8 venv-3.8-jupyter
source ~/venv-3.8-jupyter/bin/activate
  1. I think it should be python3.8 not python 3.8
  2. I think it would be better to be source venv-3.8-jupyter/bin/activate without the ~/, seeing as most people probably won't set up the virtualenv in ~/

Draw chart symbols correctly

I started with code to draw chart symbols down in the bottom of the chart file:

# draw symbol for each cell

But they don't currently work and I haven't debugged why. If anyone else wants to play with it before I get to it, go right ahead!

Colour charts

Currently the charting software expects a symbol (see #10) , but for colourwork knitting we'd probably want to accept a colour value in the legend and have it fill the square with that colour.

Here's an example of a fairly typical colourwork motif as part of a pattern:

https://www.dummies.com/crafts/knitting/knitting-projects/how-to-knit-a-headband-with-a-fair-isle-snowflake-pattern/

two-colour patterns are often represented as dark grey and white for various reasons (good contrast for most types of vision, less distraction if you're knitting in an alternate colour, cheaper to print) but we'd likely want to accept any arbitrary colour so folks doing interactive patterns could change the legend to suit their preferences in the moment.

pyknit.py main() fails to run

The main() function isn't working properly.

Attempt 1: in the CLI: pyknit 'k2 p4':

pyKnit 0.0.3
Traceback (most recent call last):
  File "/Users/bhampson/code/learn/pycon-us-2021/pyknit/venv-3.8-jupyter/bin/pyknit", line 33, in <module>
    sys.exit(load_entry_point('pyknit', 'console_scripts', 'pyknit')())
  File "/Users/bhampson/code/learn/pycon-us-2021/pyknit/pyknit/pyknit/pyknit.py", line 213, in main
    print(pyknit.parse_written(args.instruction_row, legend))
NameError: name 'pyknit' is not defined

Attempt 2: Running using the venv Python interpreter: /Users/bhampson/code/learn/pycon-us-2021/pyknit/venv-3.8-jupyter/bin/python /Users/bhampson/code/learn/pycon-us-2021/pyknit/pyknit/pyknit/pyknit.py:

Traceback (most recent call last):
  File "/Users/bhampson/code/learn/pycon-us-2021/pyknit/pyknit/pyknit/pyknit.py", line 13, in <module>
    from pyknit import GaugeSwatch, Chart
  File "/Users/bhampson/code/learn/pycon-us-2021/pyknit/pyknit/pyknit/pyknit.py", line 13, in <module>
    from pyknit import GaugeSwatch, Chart
ImportError: cannot import name 'GaugeSwatch' from partially initialized module 'pyknit' (most likely due to a circular import) (/Users/bhampson/code/learn/pycon-us-2021/pyknit/pyknit/pyknit/pyknit.py)

Related error:
https://github.com/terriko/pyknit/blame/main/pyknit/pyknit.py#L200
parse_written is not imported.

Parse repeated instructions as in the output of sleeve_decrease

The parser for written instructions is very basic and can't handle repeats. I'd like to make sure it can at least handle parsing the output of our math functions (e.g. sleeve_decrease and increase_evenly), and maybe venture into other common ways of written repeats later.

Drawn diagrams to help people understand measurements

It would be nice to have some creative commons licensed artwork for explaining certain types of measurement in our documentation and in interactive patterns. Here's an example of a non-creative-commons (but free) pattern helper blog post that includes extensive diagrams:

https://blog.tincanknits.com/2013/10/25/lets-knit-a-sweater/

The idea here is to have some artwork as part of pyknit that people could re-use without copyright concerns, so finding something free on the internet that has an explicit permissive license would be ok, but including diagrams from free (but not permissively licensed) patterns would not. It's probably easiest to draw some new stuff since knitting diagrams are not typically creative commons licensed.

Some diagrams we could use right now:

  • How to measure gauge.
  • sweater sleeve measurements

Support for Japanese Chart Symbols

I am delighted to find this open-source project! Python is not a language I often code in, but I will attempt to provide support for this issue myself. Any help on it or if someone can easily add this feature - that would be greatly appreciated.

I enjoy the charting feature as demonstrated by the Cabled Cowl demo and I noticed thanks to another issue listed that the chart symbols were pulled from this site by the Craft Yarn Council - Site Link.

That site does show the Japanese symbol alongside an 'other' set, but this project currently supports only the 'other' set of symbols. I'd love the stitch legend import to support both types of symbols, perhaps creating a toggle on which ones to use. I was thinking that the 'other' symbols could remain the default, and a command could be used such as
stitch legend = setStitchLegend(Japanese)
(Forgive my Java accent :) )

An alternative method would be to create a duplicate that could be imported instead of stitch_legend, called stitch_legend_japanese. I'm not sure which would be an easier or cleaner implementation yet, as I haven't dived into the code.

I am very fond of Japanese Knitting Charts and use them almost exclusively. I would heavily use pyknit to convert verbal instructions I found into a Japanese chart given the opportunity! I think this software has amazing potential for a code savvy knitter, and I'm looking foward to using it more.

Convert error messages from print statements to error statements

Currently, the pyknit error messages are all print statements that go to standard io. Before we get too entrenched in our ways, it might be nice to change them to "proper" error messages. (I don't really think anyone cares too much at this stage, but I'd like to model best practices)

Likely options:

  1. Use the logging facility for errors: https://docs.python.org/3/library/logging.html
  2. Raise exceptions and handle them per https://docs.python.org/3/tutorial/errors.html

Other projects i'm on use a combo of both. However, part of the fun of having a tiny project is being able to try new things, so if there's any brilliant new error handling system we might want to check out, this issue is a good place to mention it.

Add error handling for nonsensical gauge swatches.

If you make a gauge swatch with measurements set to 0, it's going to give you bad math. We should probably handle that with an appropriate error when you initially try to set something to 0 so you don't get surprised later.

We might also want to make some way for a user to specify "I never measured this part of my gauge" if they want to do measurement conversions for only rows or only stitches.

do something fun with pyscript?

I haven't really done much reading yet, but pyscript sounds fun and it would be cool if we could get pyknit running in the brower without jupyter for folk who don't have it installed.

Estimate the time it would take to knit a pattern

Hello, I'm a rookie knitter. I can barely finish a knitted headband, but I've just got this idea from researching how long it takes to knit a pair of socks:

Is there a way to write a script that calculates the number of stitches a project requires (X)? Then multiply X by the amount of time it takes to do that one stitch (N seconds)? The final result is X * N, the approximate time it takes to finish the project?

Of course that doesn't take into account the time spent on consulting the pattern, learning a new technique, ripping mistakes etc.

Create Pi Shawl function (and also half-pi)

This is one of the shawl shapes mentioned in #11, but I'm describing it here in more detail so it's a more bite-sized issue.

A pi shawl is a circular shape with regular increases as the radius of the shawl grows:

https://www.craftsy.com/post/pi-shawl-patterns/

But pi shawls, while still worked from the center out, only require a few shaping rows in the entire project. Basically, every time you double your distance from the last increase round worked, you work another increase round. These infrequent increase rounds make pi shawls a great option for lace knitting.

It would be nice to have a function that, given a gauge swatch, could tell you how many rows to work between increase rows to get the desired flat circle, printed as a pattern or as a set of numbers, and maybe also have it calculate how many rounds you'd need to reach a desired size (given the gauge).

Similarly, a half-pi shawl is the same idea but only half the circle is worked. The math would be the same for how many rows to work but the instructions would be different. You'd probably want to make half-pi an option on the full-pi function.

Extend default sitch dictionary & symbols we can draw

The chart function has a fairly small initial stitch dictionary, focusing mostly on stuff we could easily do with a default font. It would be great to have a larger default library for folk to use.

The craft yarn council has a list of knit chart symbols that would be a good initial library. We'd need to find a way to draw all those symbols. I think what we'd want is little SVGs for each symbol so they could be resized as needed, but I'm open to other suggestions of things that can be drawn from Python. (Knitting fonts do exist, but I haven't seen something with a compatible license, and we'd want something we could distribute with this GPL2.0+ project)

Add error handling for nonsensical sleeves and increases

I didn't put much in the way of error handling into the math functions, and we should probably fix that so people get a warning if they enter something that doesn't make sense.

Examples:

  • The way increasing stitches with m1 (make 1) usually works, you can only add as many stitches as you had to start. So if increase_number > starting_count in the increase_evenly() function the instructions don't actually make sense. We should throw an error instead
  • Entering 0s isn't going to make sense in a bunch of these either
  • Negative numbers either. We could maybe import some sort of unsigned type, but for now I think just an error would be fine.

Swatch class has no tests

We should add some basic unittests in the tests directory to make sure the calculations make sense.

Generate various component shapes

Generator functions for various component shapes. Similar to the sleeve decrease function we already have, you'd want these to take starting/ending stitch counts so they could be customized using actual gauge as needed. Eventually, we'd want to be able to apply pattern fill to all of these.

Some ideas:

Common components (similar to the sweater sleeve decrease function we already have):

  • sock toes
  • sock heels
  • raglan sweater yoke
  • round sweater yoke (top down/bottom up?)
  • pockets?
  • hat - beanie style
  • hat - brimmed style
  • hat - slouchy style
  • hat - other styles?

Shawl shapes:

  • square
  • rectangle
  • biased rectangle
  • triangle
  • elongated triangle
  • asymmetrical triangle(s)
  • pi shawl
  • half pi shawl
  • crescent

More component ideas welcome in the comments, and links describing how these shapes are made would also be helpful!

I'm going to file some shapes individually as I find them so they're easier bugs to work on at one go.

Example notebook

It would be nice to have an example notebook that people could download to run pyknit and see how it works.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.