Spacebar and Enter were autocompleting with the sugestions if VS code and I wanted to change that behavior, because my suggestions are rarely aligning with my development.
Anyways, this is the setting you want to look for.
Uncheck “Accept Suggestion on Commit Character”
&
Turn off “Accept Suggestion on Enter”
This week I’ve been making progress on the Huggable Cloud Pillow website.
In the process I’ve learned about some sweet stuff you can do with Javascript, Python, and Flask-SocketIO.
The first thing to take note of is Flask.
Flask is the tiny server that allows us to host websites using Python to deliver content to the client. While on the server side you can manage complicated back ends or other processes using Python in conjunction with Flask.
This type of development is nice, because you can start seeing results right away but can take on big projects easily.
It might not be the most robust Framework, but its great for small projects…
If you want to get into Flask Web Development checkout this extensive MVA.
Small and simple, Flask is static on its own. This allows us to present static content, like templates and images easily and deals with input from the user using RESTful calls to receive input. This is great for static things with lots of user actions, but if we want something a bit more dynamic we’re going to need another tool.
In this case I’ve found Flask-SocketIO, similar to Flask-Sockets but with a few key differences highlighted by the creator (Miguel Grinberg) here.
Sockets are great for is providing live information with low latency. Basically, you can get info on the webpage without reloading or waiting for long-polling.
There are lots of ways you can extend this functionality to sharing rooms and providing communication with users and all sorts of fun stuff that is highlighted on GitHub with a great chunk of sample code. The following demo is based off of these samples.
For my project, I need the webpage to regularly check for differences in the state of the cloud and present them to the client, while also changing the image the user sees.
At first I tried to implement it using sockets passing information back and forth, but that wasn’t very stable.
The solution I’ve found, uses a background thread that is constantly running while the Flask-SocketIO Application is running, it provides a loop that I use to constantly check state of our queue.
Let’s break it down…
a. I need my website to display the current state of the cloud.
b. The Flask application can get the state by query our azure queue.
c. Once we determine a change of state we can display that information to the webpage.
d. To display the new state to the webpage we need to use a socket.
e. And pass the msg to be displayed.
This demo intends to break down problem a, c, d, and e.
I’ve created this little guide to help another developer get going quickly, with a nice piece of code available on GitHub.
The five steps to this little demo project are as follows:
1. Install Flask-SocketIO into our Virtual Environment
2. Create our background thread
3. Have it emit the current state to our client
4. Call the background thread when our page render_template’s
5. Have the Javascript Catch the emit and format our HTML.
Celebrate! Its Friday!
1.
Flask-SocketIO is a python package that is available for download using
*Edit Here’s the top part of the “main.py” for reference:
#main.py
from gevent import monkey
monkey.patch_all()
import time
from threading import Thread
from flask import Flask, render_template, session, request
from flask.ext.socketio import SocketIO, emit, join_room, disconnect
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
thread = None
2.
Create our background thread.
You’ll see in the sample code from Flask-SocketIO’s github a simple way to send data to the client regardless of their requests.
For this example we’ll be changing the current time every second and display that to our client.
Background Thread:
def background_stuff():
""" python code in main.py """
print 'In background_stuff'
while True:
time.sleep(1)
t = str(time.clock())
socketio.emit('message', {'data': 'This is data', 'time': t}, namespace='/test')
3
This is the emit statement from above, but is the meat of our interface with SocketIO. Notice how it breaks down…
This emit will be sending to the client (Javascript) a message called ‘message’.
When the Javascript catches this message it will be able to pull from the python dicionary msg.data and msg.time to get the result of this package.
4
So how do we call background_stuff?
We can call it wherever we want, but for this simple example we’ll put it right in our ‘/’ base route. So when we navigate to 127.0.0.1:5000 (Local Host) we’ll see the result of our background thread call.
Here’s our route:
@app.route('/')
def index():
global thread
if thread is None:
thread = Thread(target=background_stuff)
thread.start()
return render_template('index.html')
Pretty simple… Notice global thread and target=background_stuff
Creating different background threads is a good way to iterate through your changes.
I’ve been working in Visual Studio pretty heavily in the last two weeks, but every once in a while I need to make quick edits to my .gitignore file, which isn’t in my project directory.
I usually open up a small text editor right from PowerShell and now that VS Code is out I thought ‘why not use that?’
Here’s how you can easily open files using code from PowerShell in three steps:
1.Find the path to VS Code
2. Edit your PowerShell profile
3. Open Files!
1. Find the VS Code Path
First thing we need to do is find where VS Code is in our directory.
If you have Code pinned to your start menu or on your desktop simply right click the icon and ‘select open file location’.
File explorer should now open to the location of the .exe.
Right click the Code.exe file and select ‘properties’.
If you selected a Shortcut Icon you should see a screen like this:
If you navigated to the actual location in directory of VS Code it should look like this:
Now right click and copy the path.
In my case: C:\Users\tireilly\AppData\Local\Code\app-0.1.0
2. Edit our PowerShell profile
To edit our profile we need to find the Microsoft.PowerShell_profile.ps1 file.
My file is located here:
I open the file in notepad to make my edits:
Now that we have our profile open we’ll create an alias for labeled code followed by the path to our .exe
eg: Set-Alias code ‘C:\Users\user\AppData\Local\Code\app-0.1.0\Code.exe
noticed how I added the Code.exe to the path so the program will launch!
Here’s a photo of my current PowerShell profile for reference:
Now we can save and close this file and open a new PowerShell window!
3. Edit some files!
Let’s edit our PowerShell profile with Code this time!
Something oddly satisfying about getting exactly what you want with words.
And there we go. The brand new Code editor at your fingertips whenever you need it!
Let me know if you have any comment or questions!
Professor Chang Yun is an excellent man with an amazing imagine cup record. His mentorship has led teams to US finals for 8 years straight. With 6 teams making it into the World Finals.
I’m working with a raspberry pi and I’m learning how to collect and manage sensor data.
There is a lot of data and that’s awesome, but dealing with remote persistent storage has helped me understand some good ways of keeping stuff organized and safe.
For example: ‘Keys’ or ‘Tokens’ (Not to be confused with the Late English writer Tolkien)
These are the keys to the door of your data.
To access the contents behind these doors, you must establish that you are the key-holder. One of the ways to do this in software is to create a long complicated string that can passed to the database so you can input or output data.
As long as you keep this token secret nobody will be able to mess with your data, or use it without having to pay for it.
Now this sounds pretty simple, but what if you use that token in code that is stored in a public repository on GitHub? This is essentially like printing a bunch of keys to your house and leaving them all over town. Not safe or smart.
So…
1. We need to keep these keys separate from the rest of your code. 2. We need to keep the file that contains those keys away from your public repository.
To do this I created a separate file called tokens.py that contains the strings of my tokens and a function called getStorageKey() that returns those strings.
Cool, we’ve satisfied our first goal.
Now we need to keep this file away from our repository. To do this we create a .gitignore file. This is a special ‘git’ file that allows us to specify which files will not be added to the remote repository.
To create a .gitignore file simply enter:
touch .gitignore
touch is a Unix command to create a file and update the access data, but not make any edits. Its the same as opening and closing without saving any changes.
After you’ve created this file you can edit it with any editor you like. I do it with sublime or VS Code because it helps me keep track of the separation occurring between visual studio and GitHub.
All you need to do is add on a separate lines the files you don’t want GitHub or just git to include.
When I first started all my .gitignore file had was:
tokens.py
Its expanded to include…
tokens.py
*.pyc
# the asterisk* acts as a wild card and will match any files with .pyc at the end
# anything that starts with a # is a comment and will be ignored from .gitignore
I saved this file (.gitignore) then added and committed it to the repository.
Now any files I add that end in .pyc or match tokens.py will not be added to my repository. And you will not be prompted to add them when you check git status.
What about files we’ve already have added???
If we add the rule after we added the file we want to ignore… We need to remove it from tracking on GitHub using the rm –cache command.
This will not remove it from your computer, but it will stop if from being tracked by GitHub, and will essentially be treated as if it was removed from your repository.
To pull this off simply type this command
git rm --cached filename.py
In this case I’m removing a file named ‘filename.py’.
After checking our git status we’ll see that this file is going to be removed.
Commit the changes and push to your remote repo.
You’ll see the files are still in your local directory, but are no longer in the remote or local repository. Hooray for keeping things safe!
Please let me know if you have any questions or comments.
The documentation is excellent and I recommend you spend some time reading through the resources below to round out your learning:
touch (Unix) GitHub .gitignore
If you enter: ‘git ignore –help’ in your gitshell you can find more helpful documentation about using .gitignore
Setting up Code for Powershell
Setting up sublime for Powershell
Evangelists in their natural habitat. Behind a booth and with a computer.
I ran into some troubles migrating/configuring my tables for a new app in my Django project.
I’ve been following this excellent tutorial, and ran into a bump I thought needed some clarification/update. As I’m not sure if the guide is up to date with the current version of Django.
Things I searched:
no module named models
manage.py sqlall
django not creating db
models are not being created in sqlite3 Django
sqlite3 not creating table django
No migrations to apply
django sqlite3 unable to create tables
manage.py shell
sqlite3 python package
Do these correlate with what you’re having issues with?
If so this was my solution.
First Install SQLite3 and add it too your Environment Variables -> Path
Install Page — Select: Source Code -> Zip for windows machines
I extracted it to C:/.
Now I have SQLite.exe in my root directory.
This is what the end of my Path looks like:
C:\Program Files\Microsoft\Web Platform Installer\;C:\SQLITE3.EXE\;
Sweet, now we can use SQLite in Powershell.
Configuration of Visual Studio:
Create a new app by right clicking on your Project file.
Then “Add” -> Select “Django App”
In this case my app is named book.
Sweet, now we have another Python app.
Go into your settings file and add it to the INSTALLED_APPS tuple.
eg. ‘book’,
Okay, now we’re configured make sure you’re SQLiteDB is properly configured as well.
Next we’ll create a model.
Following along with the tutorial.
We go into: book -> models.py and create our models.
Eg:
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
Sweet. Model made. Let’s get it into our SQLite DB.
Alright now in DjangoProject1Hack (Where ‘ls’ will show db.sqlite3 among others)
We’ll be doing the migration.
1. Validate:
Run- C:\Python27\python.exe manage.py validate
2. makemigrations
Run- C:\Python27\python.exe manage.py makemigrations
Output:
Migrations for ‘book’:
0001_initial.py:
– Create model Author
– Create model Book
– Create model Publisher
– Add field publisher to book
3. Sync (This just makes sure we add what’s missing)
Run- C:\Python27\python.exe manage.py syncdb
Output:
Operations to perform:
Apply all migrations: book
Running migrations:
Applying book.0001_initial… OK
Sweeeeeeet!
Okay now we manage the db:
Run- C:\Python27\python.exe manage.py shell
Sample workflow in Shell:
What do the marketers think of you?
This is kinda spooky. Just enter your Zip Code to see what classification of consumer you are assumed to be because of you location.
Why should I look at this?
Ever wondered why you receive certain ads? So?
Its like a mirror into your materialist soul. What?
If you were a materialist you would be one of the types highlighted here. So?
Its interesting, and might make you smile. Fine, I’ll check it out.
At least take a look at 90210
What’s your lifestyle segmentation?
Bonus: Tesla’s Crazy new Model S.
I can’t wait to argue about this with Ryan Reilly.