• chriswilson

    @Phuket2

    Ah I see. Good to know. I tried this and it got me a nice transparent background to my widget, but of course setting alpha to zero would do the same thing!

    I was basing this on the Swift iOS colours (UIColor class which has a UIColor.clear) rather than CSS. I’m not sure if this is what is going on behind the scenes of the UI module though.

    posted in Pythonista read more
  • chriswilson

    @Phuket2

    Regarding the clear background thing: I’ve just started playing with the Today Widget as well and I used this code to load a standard PYUI file (as might be done for a normal script). It also sets a clear background. I hope it’s helpful!

    v = ui.load_view()
    v.background_color = ui.set_color("clear")
    appex.set_widget_view(v)
    

    posted in Pythonista read more
  • chriswilson

    Well done @omz!

    posted in Pythonista read more
  • chriswilson

    Seconded! Thanks omz!

    posted in Pythonista read more
  • chriswilson

    @edsuom
    This is great. Thanks!

    posted in Pythonista read more
  • chriswilson

    Thanks @ccc

    Happy Christmas all!

    posted in Pythonista read more
  • chriswilson

    @lance1994

    @ccc has been a big help on my journey too! :)

    posted in Pythonista read more
  • chriswilson

    The hitbox you created is just a Rect() object, which is simply coordinates and not actually something rendered on the screen. You could make a Shapenode() or SpriteNode() of the same size which would be visible.

    I'm not at my computer just now, but can show some code later if you like.

    posted in Pythonista read more
  • chriswilson

    @Webmaster4o I'm starting to learn JS now, so hopefully it won't be a problem!

    posted in Pythonista read more
  • chriswilson

    @Webmaster4o Looks great! I'll check it out.

    posted in Pythonista read more
  • chriswilson

    I'm currently learning flask and hoping to make a web app that will pull user-selected data from a MySQL database.

    Graphing the data would be great, and I have just discovered Bokeh and Plotly, both of which have Python APIs. I am not familiar (yet) with Matplotlib.

    Has anyone any experience of using any of these options (particularly within Flask)?

    posted in Pythonista read more
  • chriswilson

    @happy_variable @bistrot

    I have gone through this process a few months ago and can give you some tips if you like. The Xcode template itself is great but there might be a few things need sorting out to get it to work (depending on the complexity of the app).

    You do have to make sure the app adapts to the screen sizes of different devices, as Apple will turn it down otherwise.

    Also, I might be wrong, but because the Python code is essentially inside a wrapper, you cannot interact with Apple frameworks like GameCentre etc (I'm open to being corrected on this - maybe it just takes some clever code).

    posted in Pythonista read more
  • chriswilson

    This code will present a very basic button that will run the matplotlib stuff when pressed. No pyui file used here.

    As I mentioned, the matplotlib graph appears in the console rather than the UI. You might need to use a different UI element to display a matplotlib graph (maybe a ui.ImageView()) but I'm not sure of the specifics as regards working with matplotlib.

    import ui
    import matplotlib.pyplot as plt
    from random import choice, randint
    
    def button_tapped(sender):
        button.title = "Tapped!"
        show_walk()
    
    def fill_x_values(walk_length):
        x_values = [0]
        points = walk_length
        while len(x_values) < points:
            x_direction = choice([1, -1])
            x_distance = choice([1, 2, 3, 4])
            x_step = x_direction * x_distance
            next_x = x_values[-1] + x_step
            x_values.append(next_x)
        return x_values
    
    def fill_y_values(walk_length):
        y_values = [0]
        points = walk_length
        while len(y_values) < points:
            y_direction = choice([1, -1])
            y_distance = choice([1, 2, 3, 4])
            y_step = y_direction * y_distance
            next_y = y_values[-1] + y_step
            y_values.append(next_y)
        return y_values
    
    def show_walk():
        walk_length = 50000
        x_values = fill_x_values(walk_length)
        y_values = fill_x_values(walk_length)
            
        plt.scatter(x_values, y_values,c='red', edgecolor='none', alpha=0.2, s=7)
        plt.axes().get_xaxis().set_visible(False)
        plt.axes().get_yaxis().set_visible(False)
        plt.show()
        
    button = ui.Button()
    button.present()
    button.title = "Press here!"
    button.action = button_tapped
    

    posted in Pythonista read more
  • chriswilson

    Hi @p_atrick
    Welcome to Pythonista.

    While I'm not familiar with matplotlib, I can suggest a couple of points regarding the UI.

    The lines...

    v = ui.load_view()
    v.present('sheet')
    

    ...need to go towards the end of your code, after the function definitions like def button_tapped(sender) so that the UI can "see" these definitions before it is initialised. Presenting the view should be the last thing you do.

    I see you use ui.load_view() which will load a pyui file with the same name as your script. It might be easier to define your button entirely in the interface builder (including setting its action attribute).

    Currently your last two lines of code only create a button object but do not place it anywhere within the UI (and as your UI is already "presented" it cannot be added anyway). These lines are unnecessary if the button is defined in the pyui file.

    Finally, I understand matplotlib draws things in the console, so you might not see anything on pressing the button unless you exit to the console.

    I hope this is helpful!

    posted in Pythonista read more
  • chriswilson

    If it's not working and you're using Pythonista 3, try putting the scripts in the site-packages-2 folder instead. It's for modules that don't work with Python 3.x (mysqldb might be compatible, but I'm not sure).

    Here's a quick example using INSERT:

    import mysqldb
    
    db = mysqldb.connect(host= "mysql.my_domain.co.uk", user = "my_username", passwd="my_password", db="my_database", port = 3306)
    
    c = db.cursor()
        
    c.execute("INSERT INTO my_table (my_column1, my_column_2) VALUES (%s, %s)", (my_value_1, my_value_2))
    
    db.commit()
    
    db.disconnect()
    
    

    And another using SELECT:

    import mysqldb
    
    db = mysqldb.connect(host= "mysql.my_domain.co.uk", user = "my_username", passwd="my_password", db="my_database", port = 3306)
    
    c = db.cursor()
    
    c.execute("SELECT * FROM my_table ORDER BY my_column_1 DESC LIMIT 0, 100")
    
    my_variable = c.fetchall()
    
    db.disconnect()
    

    posted in Pythonista read more
  • chriswilson

    Hi @skaboy71

    I use the same mysqldb module and it's great. Try putting the files in site-packages without them being in a subfolder. This works for me.

    I use it in a Python 2.x environment, so not sure if it works in Python 3.x or not.

    Be sure to import mysqldb at the start. This thread has some useful advice and code examples to get it going.

    posted in Pythonista read more
  • chriswilson

    @adessler

    I think the update() method gets called once per frame; at least 60 times per second usually, but I understand this is variable so it's not useful in itself for counting.

    As regards your second question, you can certainly use a SceneView to put a scene within a ui. I'm not sure about the other way around, but I think it can be done.

    I'll mess around with this and let you know if I work it out!

    posted in Pythonista read more
  • chriswilson

    @AtomBombed

    This is awesome. Thanks!

    posted in Pythonista read more
  • chriswilson

    Hi @adessler

    While ui is more natural as an interface, scene has some useful methods that you can use to make a timer and then bundle your scene in a SceneView in a ui like @abcabc suggested.

    For example, scene.Scene.t is the time (in seconds) since a scene was started. Setting a variable equal to this will give you a 'timestamp' to compare the current scene.Scene.t to in the update method.

    In my (limited) experience, this causes fewer problems than using things like time.sleep(), ui.delay() or recursion.

    I hope this helps!

    posted in Pythonista read more
Internal error.

Oops! Looks like something went wrong!